Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
812 views
in Technique[技术] by (71.8m points)

git - How can I tell if one commit is an ancestor of another commit (or vice-versa)?

Git is a DAG of snapshots, with each node on the graph representing a commit. Each commit can have 'n' parent commits.

Given any two commits, is there a single, concise way to discern the "order" of these two in the DAG. git rev-list seems to be the most promising, but I can't seem to find the right incantation.

Ideally, I'd have something like the following

$ git related hash1 hash2
hash1 is ancestor of hash2

OR

hash2 is ancestor of hash1

OR

hash1 unrelated to hash2

OR

hash1 is equal to hash2
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use git merge-base --is-ancestor <commit1> <commit2>

There is more than one way to find the answer to this. The simplest is to use

git merge-base --is-ancestor <possible-ancestor-commit> <commit>

From the documentation for git merge-base:

--is-ancestor

Check if the first <commit> is an ancestor of the second <commit>, 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.

Other options

git log with triple dot ... notation

Another option is to use git log and use triple dot notation ... to tell Git to output the set union of the child commits, minus the set intersection. Basically, it tells you how a set of commits have diverged from each other:

$ git log --oneline --graph --left-right 
--first-parent --decorate <commit1>...<commit2>

The above command will show you commits that are reachable from commit1 or commit2, but not both, i.e. C1 UNION C2 - C1 INTERSECTION C2, in terms of set operations.

If neither commit is a parent of the other, you'll see the child commits of both, but if one is an ancestor of the other, you'll only see the output for the descendant commit, since the ancestor is contained in the path of the descendant, and is thus excluded from the output.

You can read more about git log and triple dot notation from the following resources:

  1. git-log(1).
  2. gitrevisions(1): Specifying Ranges.
  3. Revision Selection.

git branch --contains option

git-rev-list(1) seems like it could be used to answer this. Another way is to simply attach temporary branch labels to the commits you want to test, and then use the --contains option for git branch:

git branch --contains <commit-to-test>

The output will be all branches that contain the commit somewhere in their commit tree, so by using a temporary branch on the other commit, you can see if the commit you're testing is an ancestor.

From the documentation:

--contains [<commit>]

Only list branches which contain the specified commit (HEAD if not specified).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...