检索Git中特定提交的子提交列表 [英] Retrieve the list of child commits of an specific commit in Git
问题描述
我想知道是否有一种有效的方法来检索给定提交的子代。
尽管在中讨论了一种方法,在Git中,这是非常低效的。
我认为这将是在Git中完成的一件简单的事情,但显然它不是。 p>
Regards。
git rev-list - 全部 - 不是$ COMMIT ^ @ --children | grep^ $ COMMIT
这应该输出一行看起来像
$ COMMIT $ child1 $ child2 $ child3 ...
为方便起见,您可以添加将命令转换为git别名:
children =!f (){git rev-list --all - not $ 1 ^ @ --children | grep $(git rev-parse $ 1);}; f#ref
语法 $ COMMIT ^ @
可能混乱,所以我会解释它。希望 $ COMMIT
不言自明。接下来是 ^ @
,它扩展到所引用提交的所有父项。因此 $ COMMIT ^ @
表示所有父母 $ COMMIT
。因为这跟在 - 不是
标志之后,这指示 rev-list
在它碰到 $ COMMIT
。这基本上只是一种优化,因为从 $ COMMIT
可达到的任何提交都不可能是一个孩子。
注意:此答案的以前版本表示 tail -1
而不是 grep^ $ COMMIT
。这可能工作在一个简单的测试回购(这就是我最初说的原因),但不能保证git rev-list最后会发布 $ COMMIT
,如果您有任何不包含 $ COMMIT
的分行。
I was wondering whether there is an efficient way to retrieve the children of a given commit. Although a method was discussed in Referencing the child of a commit in Git, it is very inefficient.
I thought this would be a straightforward thing to be done in Git, but apparently, it is not.
Regards.
git rev-list
can show children, but these children have to be reachable from the commits you provide. Assuming you want to show all children reachable from all branches in your repo, you can use something like
git rev-list --all --not $COMMIT^@ --children | grep "^$COMMIT"
This should output a line that looks like
$COMMIT $child1 $child2 $child3 ...
For convenience, you can add turn the command into a git alias:
children = "!f() { git rev-list --all --not $1^@ --children | grep $(git rev-parse $1); }; f" # reachable children of a ref
The syntax $COMMIT^@
might be confusing, so I'll explain it. Hopefully $COMMIT
is self-explanatory. This is then followed by ^@
, which expands to all parents of the referenced commit. So $COMMIT^@
means "all parents of $COMMIT
". Since this follows the --not
flag, this instructs rev-list
to stop processing after it hits any parent of $COMMIT
. This is basically just an optimization, because any commit reachable from $COMMIT
cannot possibly be a child.
Note: a previous version of this answer said tail -1
instead of grep "^$COMMIT"
. This may work in a simple test repo (which is why I initially said it), but there's no guarantee that git rev-list will emit $COMMIT
last, if you have any branches that do not contain $COMMIT
.
这篇关于检索Git中特定提交的子提交列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!