I have a repo which has a submodule. For a given SHA of the submodule, I want to find commits in the repo where this submodule exists and has the given SHA.
How would I do this with the most recent version of git?
You can use git ls-tree
to read that gitlink (a special entry in the index) to read the SHA1 of a submodule:
git ls-tree HEAD mysubmodule
160000 commit c0f065504bb0e8cfa2b107e975bb9dc5a34b0398 mysubmodule
That will include the SHA1 at which the submodule is recorded in the parent repo.
Knowing that, you can combine it with a git filter-branch
, using an index filter (to avoid having to checkout the parent repo for each commit)
git filter-branch --prune-empty --index-filter 'git ls-tree mysubmodule' -- --all|grep <yourSHA1>
A shell approach combining git rev-list
and git ls-tree
is probably cleaner:
#!/bin/sh
for r in $(git rev-list FIRST_REV..LAST_REV)
do
git ls-tree $r mysubmodule
done | grep <yourSHA1>
With git 2.7, you will soon get:
git for-each-ref --contains <SHA1>
That should be easier.