ref^指的是在ref之前的提交,那么在ref之后的提交呢?

例如,如果我签出12345,我如何签出下一次提交?

是的,Git是一个DAG节点指针结构树。我如何找到这个之后的提交?


当前回答

两个实用的答案:

一个孩子

根据@Michael的回答,我在.gitconfig中修改了子别名。

它在默认情况下按预期工作,而且是通用的。

# Get the child commit of the current commit.
# Use $1 instead of 'HEAD' if given. Use $2 instead of curent branch if given.
child = "!bash -c 'git log --format=%H --reverse --ancestry-path ${1:-HEAD}..${2:\"$(git rev-parse --abbrev-ref HEAD)\"} | head -1' -"

缺省情况下,它通过跟随祖先一步到当前分支的顶端(除非另一个类似提交的参数作为第二个参数)来给HEAD的子节点(除非给出另一个类似提交的参数)。

如果需要短散列形式,请使用%h而不是%h。

多个孩子

使用分离的HEAD(没有分支)或获取所有子结点,而不考虑分支:

# For the current (or specified) commit-ish, get the all children, print the first child 
children = "!bash -c 'c=${1:-HEAD}; set -- $(git rev-list --all --not \"$c\"^@ --children | grep $(git rev-parse \"$c\") ); shift; echo $1' -"

将$1更改为$*以打印所有子节点。

您还可以更改—全部更改为一个提交,只显示作为该提交的祖先的子节点—换句话说,只显示给定提交“方向”的子节点。这可以帮助您将输出从多个子节点缩小到一个子节点。

其他回答

Tomas Lycken在使用Git提交来驱动实时编码会话中给出了一种简洁的方法,即在提交堆栈的末尾创建定义良好的标记。本质上

git config --global alias.next '!git checkout `git rev-list HEAD..demo-end | tail -1`'

“demo-end”是最后一个标签。

终端:

$ git log --format='%H %P' --all --reflog | grep -F " [commit-hash]" | cut -f1 -d' '

或者在.gitconfig中,section [alias]:

children = "!f() { git log --format='%H %P' --all --reflog | grep -F \" $1\" | cut -f1 -d' '; }; f"

我用下面的方法找到了下一个孩子:

git log --reverse --children -n1 HEAD (where 'n' is the number of children to show)

这显示了当前HEAD的所有子元素的列表。

git rev-list --parents --all | awk -v h="$(git rev-parse HEAD)" 'index($0,h)>1{print$1}'

它只打印所有以HEAD为父文件的提交。

你可以稍微加快一下速度,把^HEAD放在|之前,那么HEAD的祖先就不会被搜索。

如果你想打印另一个提交或分支的子文件,只需把它放在HEAD的位置(在更快的版本中,在两个HEAD位置)。

我尝试过许多不同的解决方案,但没有一个对我有效。我得自己想办法。

查找下一个提交

function n() {
    git log --reverse --pretty=%H master | grep -A 1 $(git rev-parse HEAD) | tail -n1 | xargs git checkout
}

查找之前的提交

function p() {
    git checkout HEAD^1
}