【问题标题】:git bare repo: deletions commited after --mirror vs deletions not staged for commit after deleting all except .gitgit bare repo:在 --mirror 之后提交的删除与删除除 .git 之外的所有内容后未暂存的删除
【发布时间】:2022-01-13 18:31:00
【问题描述】:

最终,到目前为止,我想要一种干净的方式来返回紧凑的(例如裸机)存储库,以便随时检查(好吧,对于迂腐的:在附加 core.bare false 之后)到任何分支。我已经阅读了How to convert a normal Git repository to a bare one? 的热门答案。使用 clone 会丢失 cmets 中提到的配置条目,下面是尝试使用接受的答案后的问题。也许有一个简单的微不足道的解决方法,这就是为什么我在 cmets 中找不到它。

TL;DR

我正在尝试理解与裸 git repos 相关的信息。

  1. 克隆所有分支: How to clone all remote branches in Git?:

执行并成功:

git clone --mirror https://github.com/vmatare/thinkfan.git path/to/dest/.git
cd path/to/dest
git config --bool core.bare false
git checkout master # checkout devel also works with freshly cloned repo

Man git-clone:

   --mirror
       Set up a mirror of the source repository. This implies --bare.
       Compared to --bare, --mirror not only maps local branches of the
       source to local branches of the target, it maps all refs (including
       remote-tracking branches, notes etc.) and sets up a refspec
       configuration such that all these refs are overwritten by a git
       remote update in the target repository.

现在在git checkout anybranch 之前,我只有.git 文件夹和:

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    .github/workflows/ccpp.yml
    deleted:    CMakeLists.txt
    deleted:    COPYING
    deleted:    README.md
    ...

“已删除”输出为绿色。也就是说,删除在索引中并准备好提交(根据输出并在https://unix.stackexchange.com/questions/458354/git-status-coloring-deleted-files 中解释)。

  1. 转换为裸: https://stackoverflow.com/a/2200662/14557599

执行:

cd repo
mv .git ../repo.git # renaming just for clarity
cd ..
rm -fr repo
cd repo.git
git config --bool core.bare true

即删除除.git 之外的所有内容并将core.bare 配置值更改为true
之后

git config --bool core.bare false

顺便说一句

git config --bool core.bare true
git config --bool core.bare false

等于没有或某些内部状态发生了变化?无论如何,两者都意味着我遵循了被接受的赞成答案来制作裸回购。并且做clone --mirror 我也做了裸回购。但是现在“deleted”是红色的,输出是“Changes not staged for commit”:

git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    deleted:    .github/workflows/ccpp.yml
    deleted:    CMakeLists.txt
    deleted:    COPYING
    deleted:    README.md
    ...
    no changes added to commit (use "git add" and/or "git commit -a")

为什么原来克隆的repo和再转bare有这么大的区别?

我已尝试阅读 cmets 的答案以制作裸仓库,但没有注意到提及该问题。

如果现在我做git add *,那么显然状态会变得和我刚用--mirror克隆时一样:

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    .github/workflows/ccpp.yml
    deleted:    CMakeLists.txt
    deleted:    COPYING
    deleted:    README.md

但是在比之后结帐不起作用。

$ git checkout devel
error: Your local changes to the following files would be overwritten by checkout:
    CMakeLists.txt
Please commit your changes or stash them before you switch branches.
Aborting

如何在clone --mirror 之后和删除除.git 之外的所有内容之后查看repo "status" 之间的区别,看看为什么checkout 在第一种情况下有效,而在第二种情况下无效?

【问题讨论】:

    标签: git version-control


    【解决方案1】:

    不支持将 core.baretrue 更改为 false。不要指望任何东西都能奏效。

    如果您了解 Git 的内部结构,就可以让它发挥作用。 (其他 SO 答案中的说明至少部分是由那些知道如何使其工作的人编写的。)但是,您就会知道为什么 将其与 --mirror 结合起来。

    运行git checkout 后,您在裸存储库中创建了一个索引(也称为暂存区)。您需要删除索引 (rm .git/index)。这将解决问题,至少是暂时的。但是不要像这样将裸机转换为非裸机并返回:不支持。

    考虑让裸存储库保持裸露:您可以使用git archive 从裸存储库获取您想要的任何提交。有关详细信息,请参阅the git archive documentationgit branchgit log 命令(以及许多其他命令)在裸存储库中工作,允许您找到要提供给 git archive 的哈希 ID。

    【讨论】:

    • 如果“不支持将core.bare从true更改为false”,如何在clone --mirror / clone --bare w/out之后使用repo(“致命:此操作必须在工作树”,否则)?它可以让之后结帐,我不明白在这种情况下“不支持”是什么意思,我会理解例如“未定义的行为”。
    • rm .git/index 确实有助于仅恢复为 .git,但在不支持您编写的 clone --mirror 之后,我仍然需要使用 git config --bool core.bare false,因为没有它 checkout 失败。
    • 要在不使用git checkout 的情况下查看 提交,请考虑使用git archive 制作一个tarball 或zip 文件,您可以在其他地方进行扩展(某个容易删除的地方)。
    • 分支名称只是指针(每个名称存储一个哈希 ID)。 git fetch origin "+refs/heads/*:refs/heads/*" 表示在源头调用 Git,列出它们的分支名称和哈希 ID,确保我们有这些提交,并在我的存储库中创建/更新分支名称以匹配。分支不 hold 提交。分支 find 提交。分支不保存文件:提交保存文件。克隆存储库会复制提交(通常是所有提交,但有时更少);如果没有--mirror,它会复制no 分支名称,然后运行最后一个git checkout,它创建 一个分支名称。
    • 如果你想要一个裸存储库(你永远不会在其中做任何工作),使用--mirror 很好,如果你想使用git archive而不是git checkout检查提交,一个裸克隆就可以很好地工作。所以这可能是这里的方法:制作一个镜像(裸)克隆,不要检查任何东西。
    猜你喜欢
    • 2016-10-09
    • 2012-08-11
    • 1970-01-01
    • 2014-01-02
    • 2017-12-31
    • 2017-10-04
    • 2015-12-01
    • 2017-08-06
    • 1970-01-01
    相关资源
    最近更新 更多