Yeshwanth Venkatesh Yeshwanth Venkatesh - 11 months ago 55
Git Question

Check if the submodule commit is the same as what the repo points to

We're using git submodules to manage a large projects. We have around 20 submodules inside this large project and the submodules don't get updated often.

Before executing a

git submodule update
command which tells git to check out the commit already specified in the index of the superproject, I want to check if the commit exist in the submodule?

Something like
git branch --contains $SHA
but for submodules.

If it doesn't exist then I would like to do a
git fetch
inside the submodule which doesn't containt the commit or
git submodule foreach git pull

Ideally I would like to avoid doing
git submodule foreach git pull
for all the 20 submodules and do a git fetch if the submodule commit is different from what the repo points to.

Answer Source
git submodule foreach --quiet \
'git branch --contains <sha1> 2>/dev/null || echo $(basename $(pwd)) no such commit'

Will print the information about current commit if it is in current branch ( or rather in current tree ) and print that there's no such commit otherwise ( the part after ||).

Part after || can be used to run git fetch instead of priniting the information.