Dan Rosenstark
Dan Rosenstark

Reputation: 69757

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

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).

Upvotes: 0

Views: 454

Answers (1)

redhotvengeance
redhotvengeance

Reputation: 27866

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.

Upvotes: 1

Related Questions