Question

Is it possible to checkout the master branch in each submodule if and only if the tip of master is the checked out commit?

It would be great if this could be in some hook so that I don't have to do it manually.

The reason I need this is that I can push the changed submodules content to origin and other remotes afterwards.

Was it helpful?

Solution

Generally speaking you can use git-submodule foreach to do stuff iteratively to each submodule. You could run your foreach, and do something like this to compare HEAD to master

$(git rev-parse --abbrev-ref HEAD) == $(git rev-parse --abbrev-ref master)

However, you may also be interested to know that you can tell submodules not to detach their HEADs when updating by defining the update property in .gitmodules. Find the submodule relevant submodule in .gitmodules and add an update = rebase or update = merge under it. This will mean when when you a submodule update, git will go into the submodule and instead of just checking out the commit directly (which detaches HEAD), it will perform either a pull or rebase.

Documentation is a bit shotty, but take a look at submodule.$name.update option, the --merge and --rebase update flags, and the --branch flag.

https://www.kernel.org/pub/software/scm/git/docs/git-submodule.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top