How can I tell if one commit is a descendant of another commit?

If you want to check this programmatically (e.g. in script), you can check if git merge-base A B is equal to git rev-parse --verify A (then A is reachable from B), or if it is git rev-parse --verify B (then B is reachable from A). git rev-parse is here needed to convert from commit name to commit SHA-1 / commit id.

Using git rev-list like in VonC answer is also possibility.

Edit: in modern Git there is explicit support for this query in the form of git merge-base --is-ancestor.


If one of commits you are asking about is a branch tip, then git branch --contains <commit> or git branch --merged <commit> might be better non-programmatic solution.


From Git 1.8.0, this is supported as an option to merge-base:

git merge-base --is-ancestor <maybe-ancestor-commit> <descendant-commit>

From the man page:

--is-ancestor

Check if the first is an ancestor of the second , and exit with status 0 if true, or with status 1 if not. Errors are signaled by a non-zero status that is not 1.

For example:

git merge-base --is-ancestor origin/master master; echo $?

Tags:

Git