如何将 Git 子模块指针恢复到存储在包含存储库中的提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7882603/
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 revert a Git Submodule pointer to the commit stored in the containing repository?
提问by Smashery
I have a git submodule in my main git repo. As I understand it, the main repo stores a SHA value (somewhere...), pointing to the specific commit of the submodule that it is "linked to".
我的主 git 存储库中有一个 git 子模块。据我了解,主存储库存储一个 SHA 值(某处......),指向它“链接到”的子模块的特定提交。
I went in to my submodule and typed git checkout some_other_branch
. I have no idea which commit I came from.
我进入我的子模块并输入git checkout some_other_branch
. 我不知道我来自哪个提交。
I would like to revert that pointer so that the main repo and the submodule are in sync again.
我想恢复该指针,以便主 repo 和子模块再次同步。
My first (probably naive) instinct was to say git reset --hard
- that seems to work for everything else. To my surprise, it did not work for this scenario.
我的第一个(可能是天真的)直觉是说git reset --hard
- 这似乎适用于其他一切。令我惊讶的是,它不适用于这种情况。
So I've figured out that I can type git diff
, note the SHA ID that the submodule pointer used to have, and then head into the submodule and git checkout [SHA ID]
... but surely there must be an easier way?
所以我发现我可以输入git diff
,注意子模块指针曾经拥有的SHA ID,然后进入子模块git checkout [SHA ID]
......但肯定有更简单的方法吗?
As I'm still learning about git submodules, please feel free to correct my terminology if there are words for concepts that I don't know.
由于我仍在学习 git 子模块,如果有我不知道的概念词,请随时更正我的术语。
回答by Brian Riehman
You want to update your submodule so it is in sync with what the parent repository believes it should be. This is what the update command is for:
您想更新您的子模块,使其与父存储库认为它应该是同步的。这是更新命令的用途:
From the submodule manpage:
从子模块联机帮助页:
Update the registered submodules, i.e. clone missing submodules and checkout the commit specified in the index of the containing repository. This will make the submodules HEAD be detached unless --rebase or --merge is specified or the key submodule.$name.update is set to rebase or merge.
Run this and all should be well:
运行这个,一切都应该很好:
git submodule update
回答by Adam Dymitruk
To change the commit that a submodule points to, you need to checkout that version in the submodule, then go back to the containing repo, add and commit that change.
要更改子模块指向的提交,您需要在子模块中签出该版本,然后返回包含的存储库,添加并提交该更改。
Or, if you want the submodule to be on the version the top repo points to, do git submodule update --recursive
. Add --init
if you've just cloned.
或者,如果您希望子模块位于顶级存储库指向的版本上,请执行git submodule update --recursive
. --init
如果您刚刚克隆,请添加。
Also, git submodule
without a submodule command will show you the commit you are pointing to. There will be a - or a + in front of the commit if it's not in sync.
此外,git submodule
没有子模块命令将显示您指向的提交。如果提交不同步,则在提交前会有一个 - 或 + 。
If you look at a tree with a submodule in it, you can see that the submodule is marked as a commit
as opposed to the rest that are blobs or trees.
如果您查看其中包含子模块的树,您会发现该子模块被标记为 a commit
,而不是其余部分是 blob 或树。
to see what a particular commit points wrt to submodules you can:
要查看特定提交指向子模块的内容,您可以:
git ls-tree <some sha1, or branch, etc> Submodule/path
you can then see the commit or anything else if you like by passing that into log, etc (the git-dir
option at the git command level allows you to skip having to cd down to the submodule):
然后,您可以通过将其传递到日志等中来查看提交或其他任何内容(git-dir
git 命令级别的选项允许您跳过 cd 到子模块):
git --git-dir=Submodule/path log -1 $(<the above statement>)
回答by Benjamin Noffsinger
Another case I just ran into is if there is an unstaged change in the submodule that you want to discard. git submodule update will not remove that change, nor will git reset --hard on the parent directory. You need to go to the submodule directory and do a git reset --hard. So if I want to fully discard unstaged changes in both my parent and submodule, I do the following:
我刚刚遇到的另一种情况是,如果您要丢弃的子模块中有未分阶段的更改。git submodule update 不会删除该更改,也不会删除父目录上的 git reset --hard 。您需要转到子模块目录并执行 git reset --hard。因此,如果我想完全丢弃父模块和子模块中的未暂存更改,我会执行以下操作:
In Parent:
在家长中:
git reset --hard
git submodule update
In Submodule:
在子模块中:
git reset --hard
回答by Dan Cruz
Use git ls-tree HEAD
in the "superproject" folder to see what commit your submodule was originally at. Then change into the submodule directory and use git log --oneline --decorate
to see what branch the original commit is on. Finally, git checkout original-commit-branch
.
使用git ls-tree HEAD
中的“上层项目”文件夹,看看有什么提交你的子模块原是。然后切换到子模块目录并使用git log --oneline --decorate
查看原始提交所在的分支。最后,git checkout original-commit-branch
。
Using some test directories I set up, here's what the commands might look like:
使用我设置的一些测试目录,命令可能如下所示:
$ git --version
git version 1.7.4.1
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: sm2 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git ls-tree HEAD
100644 blob 76813a07ae558db274cefc6d903ec24323fdeb0d .gitmodules
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 main
160000 commit 7c5889497938cd5699a9234a98ee93947e52b1ed sm1
160000 commit f68bed61cba6f94cef57554f2cf46a45a4a0d337 sm2
$ cd sm2
$ git log --oneline --decorate
5b8d48f (HEAD, foo1) foo1.1
f68bed6 (origin/master, origin/HEAD, master) Initial commit.
$ git checkout master
Switched to branch 'master'
$ cd ..
$ git status
# On branch master
nothing to commit (working directory clean)
The "superproject" shows the sm2 submodule at commit f68bed6
but sm2 has it's HEAD at 5b8d48f
. The submodule commit f68bed6
has three branches on it that may be used for checkout in the submodule directory.
“超级项目”在提交时显示 sm2 子模块,f68bed6
但 sm2 在5b8d48f
. 子模块提交f68bed6
有三个分支,可用于子模块目录中的检出。
回答by MichaelChan
The answer here somehow didn't solve my specific issue with the submodule so in case it happens to you too, try the following...
这里的答案不知何故没有解决我与子模块的具体问题,所以如果它也发生在你身上,请尝试以下...
git submodule foreach git reset --hard
https://kalyanchakravarthy.net/blog/git-discard-submodule-changes/
https://kalyanchakravarthy.net/blog/git-discard-submodule-changes/
回答by Rajesh Goel
I wanted to disregard any changes in submodule and in my module also
我想忽略子模块和我的模块中的任何更改
The below command helped me:
以下命令帮助了我:
git submodule update --init --recursive