ephemerr
ephemerr

Reputation: 1983

How to search for a Git commit in submodules?

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?

Upvotes: 1

Views: 226

Answers (2)

Paolo
Paolo

Reputation: 26074

git submodule status will give you a list of the currently checked 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)

Upvotes: 2

phd
phd

Reputation: 94473

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.

Upvotes: 1

Related Questions