如何判断一个提交是否是另一个提交的祖先(反之亦然)? [英] How can I tell if one commit is an ancestor of another commit (or vice-versa)?

查看:178
本文介绍了如何判断一个提交是否是另一个提交的祖先(反之亦然)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Git是一个 DAG 快照,图上的每个节点都代表提交。每个提交可以有'n'个父提交。



给出任何两个提交,是否有一个简单的方法来辨别这两个DAG的顺序。 git rev-list 似乎是最有希望的,但我似乎无法找到正确的咒语。



理想情况下,我有类似以下的内容:

$ $ g $相关的hash1 hash2 $ b $ hash1是hash2的祖先

OR

  hash2是hash1的继承者

OR

  hash1与hash2无关

OR

  hash1等于hash2 


解决方案

使用 git merge-base --is-ancestor< commit1> < commit2>



有多种方法可以找到答案。最简单的是使用

  git merge-base --is-ancestor< commit> <提交> 

git merge-base


   - 是 -  ancestor 

检查第一个< commit> 是第二个< commit> 的祖先,如果为true,或者状态1,否则。错误通过不为1的非零状态发出。




其他选项



git log with triple dot ... 符号



另一个选择是使用 git log 并使用三点符号 ... 来告诉Git输出子提交的集合并减去集合交集。基本上,它告诉你一组提交是如何相互分离的:

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

以上命令将显示可从 commit1 commit2 ,但不能同时存在,即C1 UNION C2 - C1 INTERSECTION C2,以设置操作为单位。



如果两个提交都不是另一个的父代,那么您将看到两个子提交,但如果其中一个是另一个的祖先,则只会看到后代提交的输出,因为祖先是包含在后代的路径中,因此被排除在输出之外。



您可以阅读更多关于 git log 和三点符号从以下资源:


  1. git-log(1)

  2. gitrevisions(1):指定范围

  3. 修订选择



git branch --contains option



git-rev-list(1)似乎可以用来回答这个问题。另一种方法是将临时分支标签附加到要测试的提交上,然后对 - 包含选项。 gs / git-branch.htmlrel =noreferrer> git branch

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

输出将是包含提交树中某处的所有分支,因此通过使用临时你可以看到你正在测试的提交是否是一个祖先。



从文档中:


   - 包含[< commit>] 

仅列出包含指定提交的分支(如果未指定,则为HEAD)。



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

解决方案

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 <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).

这篇关于如何判断一个提交是否是另一个提交的祖先(反之亦然)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆