在Git中引用提交的孩子 [英] Referencing the child of a commit in Git
问题描述
如果您想将 HEAD
移至当前 HEAD
的父级,那很简单:
If you want to move the HEAD
to the parent of the current HEAD
, that's easy:
git reset --hard HEAD^
但是有什么简单的方法可以完成与此操作完全相反的操作,也就是将头部设置为当前头部的第一个子女提交?
But is there any simple way to do the exact opposite of this operation, that is, set the head to the current head's first child commit?
现在,我使用gitk作为解决方法(alt-tab,向上箭头,alt-tab,中键),但我想要一个更优雅的解决方案,也可以在gitk不可用时使用。 / b>
Right now, I use gitk as a workaround (alt-tab, up-arrow, alt-tab, middle-click), but I would like a more elegant solution, one that can also be used when gitk is not available.
推荐答案
#!/bin/bash
REV=$1
if [ x$REV == x ]; then
echo "Usage: git-get-child []"
exit
fi
HASH=$(git-rev-parse $REV)
NUM=$2
if [ x$NUM == x ]; then
NUM=1
fi
git rev-list --all --parents | grep " $HASH" | sed -n "${NUM}s/\([^ ]*\) .*$/\\1/p"
The git rev-list --all --parents
does exactly what I need: it iterates over all reachable commits, and prints the following line for each:
SHA1_commit SHA1_parent1 SHA1_parent2
等。
grep表达式确保只有那些行被发现的地方是问题的SHA1是父级。然后我们得到第n个孩子的第n行,并得到孩子的SHA1。
The space in the grep expression ensures that only those lines are found where the SHA1 in question is a parent. Then we get the nth line for the nth child and get the child's SHA1.
这篇关于在Git中引用提交的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!