Git contains between arbitrary commits

I was wondering if there is a generic method for determining if a commit is the parent of another.

git branch --contains <hash>


I almost want to. It lists all the branches containing a commit, but I want to know if an arbitrary commit "contains" another commit.

My temporary hack is to create a branch in a commit and then check if it is in the list, but that seems sloppy.

git branch __temp_branch__ <hash1>
git branch --contains <hash2> # check if __temp_branch__ is in output
git branch -d __temp_branch__



source to share

3 answers

If commitA is the ancestor of commitB, then git merge-base commitA commitB

- commitA

. For example:

if [ "$(git merge-base $commitA $commitB)" = "$commitA" ]; then ...


This is significantly more efficient than grepping the output git rev-list

if that matters to you.



git rev-list <childSHA> | grep <parentSHA>

This will return <parentSHA>

if he was a parent<childSHA>



One possibility is possible:

git log --ancestry-path <parentSHA> <childSHA>


If it doesn't return anything, you know parentSHA is not the parent.



All Articles