Jordan Savant # Software Engineer

1) Find branches the commit is on:

git branch --contains <commit>

This will tell you all branches which have the given
commit in their history.


2) Search the reflogs:

If you are working in the repository in which the
commit was made, you can search the reflogs for the
line for that commit.
Reflogs older than 90 days are pruned by git-gc, so
if the commit's too old, you won't find it.
That said, you can do this:

git reflog show --all | grep a871742

to find commit a871742. The output should be
something like this:

a871742 refs/heads/completion@{0}: commit (amend): mpc-completion: total rewrite

indicating that the commit was made on the branch
"completion". The default output shows abbreviated
commit hashes, so be sure not to search for the full
hash or you won't find anything.

"git reflog show" is actually just an alias for "git
log -g --abbrev-commit --pretty=oneline", so if you
want to fiddle with the output format to make
different things available to grep for, that's your
starting point!

If you're not working in the repository where the
commit was made, the best you can do in this case is
examine the reflogs and find when the commit was first
introduced to your repo; with any luck, you fetched
the branch it was committed to. This is a bit more
complex, because you can't walk both the commit tree
and reflogs simultaneously. You'd want to parse the
reflog output, examining each hash to see if it
contains the desired commit or not.


3) Find a subsequent merge commit:

This is workflow-dependent, but with good workflows,
commits are made on development branches which are
then merged in. You could do this:

git log --merges <commit>

to see merge commits that have the given commit as an
ancestor. (If the commit was only merged once, the
first one should be the merge you're after; otherwise
you'll have to examine a few, I suppose.) The merge
commit message should contain the branch name that was
merged.

to see merge commits that have the given commit as an ancestor. (If the commit was only merged once, the first one should be the merge you're after; otherwise you'll have to examine a few, I suppose.) The merge commit message should contain the branch name that was merged.
to see merge commits that have the given commit as an ancestor. (If the commit was only merged once, the first one should be the merge you're after; otherwise you'll have to examine a few, I suppose.) The merge commit message should contain the branch name that was merged.