如何递归地检出旧的 git commit 包括所有子模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15124430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to checkout old git commit including all submodules recursively?
提问by Ben Baron
I have a git repo with multiple submodules. One of those submodules has multiple submodules of it's own. All I'm looking to do is check out an old commit on the master repo and have it checkout the appropriate commits from all submodules to get the correct state of the code at that time.
我有一个带有多个子模块的 git repo。其中一个子模块有多个自己的子模块。我要做的就是检查主存储库上的旧提交,并让它从所有子模块中检查适当的提交,以获取当时代码的正确状态。
I know that git contains the information necessary as the ls-tree
command can tell me which commit each submodule was on. However, I have to manually check out each one, which is painfully time consuming.
我知道 git 包含必要的信息,因为ls-tree
命令可以告诉我每个子模块在哪个提交上。但是,我必须手动检查每一个,这非常耗时。
I'm looking for something like git checkout --recursive
but such a command doesn't seem to exist.
我正在寻找类似的东西,git checkout --recursive
但似乎不存在这样的命令。
Is there anyway to do this?
有没有办法做到这一点?
回答by Chronial
You need two commands to achieve this:
您需要两个命令来实现此目的:
git checkout *oldcommit*
git submodule update --recursive
Update:This answer is outdated as of 2018 – see VonC's answer belowfor more current information.
更新:截至 2018 年,此答案已过时 -有关更多最新信息,请参阅下面的 VonC 答案。
回答by VonC
Note: if you have multiple submodules (and submodules inside submodules), Git 2.14 (Q3 2017) will help (more recent that the OP from 2013)
注意:如果您有多个子模块(以及子模块内的子模块),Git 2.14(2017 年第三季度)会有所帮助(比 2013 年的 OP 更近)
git checkout --recurse-submodules
Using
--recurse-submodules
will update the content of all initialized submodules according to the commit recorded in the superproject.
If local modifications in a submodule would be overwritten the checkout will fail unless-f
is used.
using
--recurse-submodules
将根据超级项目中记录的提交更新所有已初始化子模块的内容。
如果子模块中的本地修改将被覆盖,除非-f
使用,否则检出将失败。
"git checkout --recurse-submodules
" did not quite work with a
submodule that itself has submodules. It will with Git 2.14.
" git checkout --recurse-submodules
" 不太适用于本身具有子模块的子模块。它将与 Git 2.14 一起使用。
Note: with Git 2.19 (Q3 2018), git checkout --recurse-submodules another-branch
is more robust.
Before, it did not report in which submodule it failed to update the working tree, which resulted in an unhelpful error message.
注意:使用 Git 2.19(2018 年第三季度),git checkout --recurse-submodules another-branch
更加健壮。
之前,它没有报告它在哪个子模块中更新工作树失败,从而导致无用的错误消息。
See commit ba95d4e(20 Jun 2018) by Stefan Beller (stefanbeller
).
(Merged by Junio C Hamano -- gitster
--in commit 392b3dd, 24 Jul 2018)
请参阅Stefan Beller ( )提交的 ba95d4e(2018 年 6 月 20 日)。(由Junio C Hamano合并-- --在commit 392b3dd,2018 年 7 月 24 日)stefanbeller
gitster
submodule.c
: report the submodule that an error occurs inWhen an error occurs in updating the working tree of a submodule in
submodule_move_head
, tell the user which submodule the error occurred in.The call to
read-tree
contains a super-prefix, such that theread-tree
will correctly report any path related issues, but some error messages do not contain a path, for example:~/gerrit$ git checkout --recurse-submodules origin/master ~/gerrit$ fatal: failed to unpack tree object 07672f31880ba80300b38492df9d0acfcd6ee00a
Give the hint which submodule has a problem.
submodule.c
: 报告发生错误的子模块当更新子模块的工作树发生错误时
submodule_move_head
,告诉用户错误发生在哪个子模块。调用
read-tree
包含一个超级前缀,这样read-tree
将正确报告任何与路径相关的问题,但一些错误消息不包含路径,例如:~/gerrit$ git checkout --recurse-submodules origin/master ~/gerrit$ fatal: failed to unpack tree object 07672f31880ba80300b38492df9d0acfcd6ee00a
提示哪个子模块有问题。
回答by Thomas
Depending on whether or not there are more submodules in the old checkout, you might have to do the following to initialize submodules that are not there any more in new commits:
根据旧检出中是否有更多子模块,您可能必须执行以下操作来初始化新提交中不再存在的子模块:
git checkout *oldcommit*
git submodule init
git submodule update --recursive