我们如何指代提交的孩子? [英] How can we refer to a child of a commit?

查看:45
本文介绍了我们如何指代提交的孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出分支名称或标签name,我们可以通过name~1引用其父级.我们可以以某种方式提及它的一个孩子吗?

Given a branch name or a tag name, we can refer to its parent by name~1. Can we refer to one of its children in some way?

谢谢.

推荐答案

正如 chepner所说,该信息并非直接可用,因为Git仅存储向后链接.创建子项时,父提交始终存在,但是创建父项时,子提交不存在.一旦创建了提交,就不能对其进行任何更改,因此无法将其连接到其子项.

As chepner said, the information is not directly available, as Git only stores backwards links. A parent commit always exists when its child is created, but its children do not exist when the parent is created. Once a commit has been created, nothing about it can ever be changed, so there's no way to hook it up to its children.

尽管如此,如果您有一个方向的想法,您可以找到在该方向上提交的孩子.这就是我的意思:假设图形如下所示:

Nonetheless, if you have a direction in mind, you can find the children of some commit in that direction. Here's what I mean: suppose the graph looks like this:

          B--D--E   <-- branch1
         /
...--o--A
         \
          C--F--G   <-- branch2

并且,假设您当前在爪子中具有提交A的名称或哈希ID.您可以通过在分支1"方向上从A搜索来找到提交B的哈希ID,并且可以通过在分支2"方向上从A搜索来找到提交C的哈希ID.为此,请使用:

And, suppose you have the name or hash ID of commit A in your paws at the moment. You can find the hash ID of commit B by searching from A in the "branch1" direction, and the hash ID of commit C by searching from A in the "branch2" direction. To do this, use:

git rev-list --reverse --topo-order --ancestry-path A..branch1

,它将按顺序列出提交BDE的哈希ID.将branch1替换为branch2,以依次获取CFG.

which will list out the hash IDs for commits B, D, and E in that order. Replace branch1 with branch2 to get C, F, and G in that order.

请注意,如果图形如下所示:

Be aware that if the graph looks like this:

          B--E   <-- branch1
         /  /
...--o--A--D
         \
          C   <-- branch2

您可以首先在A..branch1方向上获得 B D.要检查这种情况,请代替(或除了)使用--topo-order和/或--reverse,沿此祖先路径收集所有 all 提交,对于每个此类提交,请检查所有提交父母的人:

you could get either B or D first, in the A..branch1 direction. To check for such situations, instead of (or in addition to) using --topo-order and/or --reverse, collect all the commits along this ancestry path, and for each such commit, check all that commit's parents:

ahash=$(git rev-parse A^{commit}) # in case A is a branch or tag name, for instance
for hash in $(git rev-list --ancestry-path $ahash..branch1); do
    if git rev-parse $hash^@ | grep -q $ahash; then
        echo $hash is a child of $ahash
    fi
done

(此代码未经测试).

这篇关于我们如何指代提交的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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