ref^指的是在ref之前的提交,那么在ref之后的提交呢?
例如,如果我签出12345,我如何签出下一次提交?
是的,Git是一个DAG节点指针结构树。我如何找到这个之后的提交?
ref^指的是在ref之前的提交,那么在ref之后的提交呢?
例如,如果我签出12345,我如何签出下一次提交?
是的,Git是一个DAG节点指针结构树。我如何找到这个之后的提交?
当前回答
Tomas Lycken在使用Git提交来驱动实时编码会话中给出了一种简洁的方法,即在提交堆栈的末尾创建定义良好的标记。本质上
git config --global alias.next '!git checkout `git rev-list HEAD..demo-end | tail -1`'
“demo-end”是最后一个标签。
其他回答
我需要一个命令来快速签出下一次提交。 我最终得到了这个别名:
[alias]
next = "!f() { CMIT=$(git log --ancestry-path --format=%H ${commit}..${1} | tail -1) && git checkout $CMIT; }; f"
你需要指定你想要遍历的分支,例如:
git next master
您可以通过直接编辑它来将其添加到.gitconfig文件中。
输出示例:
~/home/myRepo | dd9e66ee git next master
Previous HEAD position was dd9e66e Update README
HEAD is now at d71c74b Rename project; update to JUnit5
通过运行这个命令,你进入分离的头部模式,当然:)
《Hudson》(现在的Jenkins)的创造者Kohsuke Kawaguchi(2013年11月)写道: Kohsuke / git-children-of:
给定一个提交,找到该提交的直接子节点。
#!/bin/bash -e
# given a commit, find immediate children of that commit.
for arg in "$@"; do
for commit in $(git rev-parse $arg^0); do
for child in $(git log --format='%H %P' --all | grep -F " $commit" | cut -f1 -d' '); do
git describe $child
done
done
done
正如这个线程所说明的,在一个基于DAG(有向无环图)表示的历史的VCS中,没有“一个父”或“一个子”。
C1 -> C2 -> C3
/ \
A -> B E -> F
\ /
D1 -> D2 ----/
提交的顺序是通过“拓扑顺序”或“日期顺序”来完成的(参见GitPro书籍)。
但是从Git 1.6.0开始,你可以列出提交的子对象。
git rev-list --children
git log --children
注意:对于父提交,你有同样的问题,后缀^ to一个修订参数表示该提交对象的第一个父。^<n>表示<n>的第1个父节点(即rev^等同于rev^1)。
如果你在分支foo上,并发出“git merge bar”,那么foo将是第一个父节点。 例如:第一个父节点是你合并时所在的分支,第二个父节点是你合并的分支上的提交。
我用下面的方法找到了下一个孩子:
git log --reverse --children -n1 HEAD (where 'n' is the number of children to show)
两个实用的答案:
一个孩子
根据@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更改为$*以打印所有子节点。
您还可以更改—全部更改为一个提交,只显示作为该提交的祖先的子节点—换句话说,只显示给定提交“方向”的子节点。这可以帮助您将输出从多个子节点缩小到一个子节点。
终端:
$ 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"