Better Git: How to find out what submodules there are in a different branch

Generally speaking Git is fantastic and easy to use – one of the few pain points is where submodules butt heads with branches.

Far from solving all the problems in related to this – I’ve found an easy way to find out what submodules are in a branch, commit, or tag without having to check that reference out. This method also shows you the commits that each submodule is currently pointing to.


#Where <ref> should be replaced with a branch name, tag name, or commit hash.
git ls-tree -rt <reference> | grep 'commit [0-9A-z]'

The ls-tree command is fairly simple, its like running the ls unix command but includes git information – like the type of object each item is and the hash reference for that object. That command can also be used on a specific directory also by using <reference>:<path>. The -r flag makes it recursive so that is searches through all the subdirectories in the branch.

The second part of this is a simple grep command, that filters the results by those of type “commit”, which is how ls-tree represents submodules. I’ve included a pattern to match the word commit followed by the SHA1 hash – to avoid getting items that have the word commit as part of a folder or filename.

Hope this helps someone out there – it took me a while to figure out how to do this.



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s