How can I determine if someone is one of the descendants of another commit?

With Git, how can I determine if any commit in my branch is a descendant of another commit?

+126
git
Jun 09 '10 at
source share
7 answers

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

Using git rev-list as in VonC answer is also possible.




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

+47
Jun 09 '10 at 13:25
source share

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

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

On the man page:

- this is the ancestor

Check if the first is the ancestor of the second, and exit with status 0 if true, or status 1 if not. Errors are signaled by a nonzero status that is not equal to 1.

For example:

 git merge-base --is-ancestor origin/master master; echo $? 
+233
Nov 23
source share

This kind of operation is based on the concept of the revision range described in detail in the SO question: " The difference is 'git log origin / master vs' git log origin / master .. ".

git rev-list should be able to return from commit to another, if possible.

Therefore, I would try:

 git rev-list --boundary 85e54e2408..0815fcf18a 0815fcf18a19441c1c26fc3495c4047cf59a06b9 8a1658147a460a0230fb1990f0bc61130ab624b2 -85e54e240836e6efb46978e4a1780f0b45516b20 

(Boundary commits have a prefix - )

If the last commit is displayed in the same way as the first commit in the git rev-list command, then this commit is reachable from the second commit.

If the first commit is not available for the second, git rev-list should not return anything.

 git rev-list --boundary A..B 

will end in A if A reachable from B
This is the same as:

 git rev-list --boundary B --not A 

with B a positive link and A a negative link .
It will start at B and go through the schedule until it encounters the revision available from A
I would say that if A is directly accessible from B , it will meet (and display because of the --boundary option) A

+15
Jun 09 '10 at 13:10
source share

Another way would be to use git log and grep.

git log --pretty=format:%H abc123 | grep def456

This will result in a single line of output if commit def456 is the ancestor of commit abc123, otherwise the output will fail.

You can usually get away with the argument "--pretty", but this is not the case if you want to make sure that you only look at the actual hash commits and not the comments on the log, etc.

+10
Sep 22 2018-11-11T00:
source share

https://stackoverflow.com/a/16614864/enabled-statement-in-javascript/3646323#6326253

 git-is-ancestor() ( if git merge-base --is-ancestor "$1" "$2"; then echo 'ancestor' elif git merge-base --is-ancestor "$2" "$1"; then echo 'descendant' else echo 'unrelated' fi ) alias giia='git-is-ancestor' 
+3
Sep 29 '16 at 14:57
source share

git show-branch branch-sha1 commit-sha1

Where:

  • branch-sha1: sha1 in your branch you want to check
  • commit-sha1: sha1 of the commit you want to check
+1
Nov 11 '15 at 16:19
source share

Set up a response on itub if you need to do this for all tags in the repository:

 for i in `git tag` ; do echo -ne $i "\t" ; git log --pretty=format:%H $i | (grep <commit to find> || echo ""); done 
-one
Jan 19 '15 at 9:14
source share



All Articles