Home > Software engineering >  How to search for a Git commit in submodules?
How to search for a Git commit in submodules?

Time:11-30

I have a repo with multiple submodules. I have a commit's hash but do not know to which submodule it belongs, how I could find it's exact submodule?

CodePudding user response:

git submodule status will give you a list of the currently checkout out commit hash id for each submodule. You can parse that and find out the name of the submodule corresponding to the commit hash:

$ commit_id="14f4e19f1c"
$ git submodule status | awk -v commit_id="$commit_id" '$0 ~ commit_id {print $2}'

(this assumes that you are working with the latest commit hash from that submodule)

CodePudding user response:

Run git show in all submodules, recursively; hide error messages, ignore errors; report the submodule when the hash is found (git show doesn't return error):

git submodule foreach -q --recursive 'git show -q $hash 2>/dev/null && echo $name || :'

See the docs.

  • Related