Git: Find out which branch contains a commit and check it out?

匆匆过客 提交于 2020-01-17 12:36:14

问题


From what I've learned here about submodules, I can now roll my submodules back to the right commit by using

git submodule update --recursive

however, now the submodules are never (rarely?) in a branch. I think that which branch the submodule needs is not stored. Anyway, I can use

git branch --contains HEAD

in the submodule and figure out the branch and then switch to it. Is there a built-in way to this? My goal is to get back to a branch that contains the commit (if there is one).


回答1:


By default, Git doesn't track submodules by branches, it tracks them by commits. So git submodule update will result in the submodules being in a detached HEAD state (similar to if you git checkout a specific commit hash). Before doing any work, you'll have to make sure to actually check out a branch, otherwise things could get messy.

As of Git 1.8.2, you can now have submodules track branches. This option needs to be specified when you add the submodule:

git submodule add -b master <git repo url>

If you track submodules via branches, you'll want to add --remote to the update call:

git submodule update --remote

You can read more about tracking submodules via branches here and here.



来源:https://stackoverflow.com/questions/15891704/git-find-out-which-branch-contains-a-commit-and-check-it-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!