如何在git中提交之间前后移动? [英] How do I move forward and backward between commits in git?
问题描述
我正在做一个 git bisect
并且在到达有问题的提交之后,我现在试着向前/向后迈出一步,以确保我处于正确的位置。
我知道 HEAD ^
会在历史中倒退,但还有另一条捷径让我前进未来的特定提交)如下所示:
A - B - C(HEAD) - D - E - F
我知道我的目标是 F ,并且我想从 C 转换为 D 。
注意: git:如何在提交之间来回移动,我的问题稍有不同,并且没有在那里回答
$ b $ b我已经尝试了一下,这看起来有助于向前浏览:
git checkout $(git rev-list --topo-order HEAD..towards | tail -1)
其中指向
是提交的SHA1或标签。
解释:
-
$()
表示:获取当前HEAD
和向
commit的所有提交(不包括HEAD
),并按照优先顺序对它们进行排序(例如默认情况下在git log
中 - 而不是按时间顺序排序这是奇怪的rev-list
)的默认值,然后取最后一个(tail
),即我们想去。 - 这是在子shell中计算的,并传递给
git checkout
来执行结帐。 / ul>
您可以定义一个可作为参数访问的函数 - 在您的
.profile
文件中预期为别名,以向前导航提交:
#在Git提交层次结构中,向特定的提交
#前进使用:
#gofwd v1.2.7
#当参数没有被指定时什么也不做。
gofwd(){
git checkout $(git rev-list --topo-order HEAD ..$ *| tail -1)
}
#回到Git commit层次结构
#用法:
#goback
alias goback ='git checkout HEAD〜'
I am doing a
git bisect
and after arriving to the problematic commit, I am now trying to get a step forward/backward to make sure I am in the right one.I know of
HEAD^
to go backwards in history but is there another shortcut to get me forward (towards a specific commit in the future) like so:A - B - C(HEAD) - D - E - F
I know that my target is F and I want to move from C to D.
NOTE: this is not a duplicate of Git: How to move back and forth between commits, my question is slightly different and is not answered there
解决方案I've experimented a bit and this seems to do the trick to navigate forwards:
git checkout $(git rev-list --topo-order HEAD..towards | tail -1)
where
towards
is a SHA1 of the commit or a tag.Explanation:
- the command inside
$()
means: get all the commits between currentHEAD
andtowards
commit (excludingHEAD
), and sort them in the precedence order (like ingit log
by default -- instead of the chronological order which is weirdly the default forrev-list
), and then take the last one (tail
), i.e. the one we want to go to. - this is evaluated in the subshell, and passed to
git checkout
to perform a checkout.
You can define a function accessible as a parameter-expecting alias in your
.profile
file to navigate forward towards the particular commit:# Go forward in Git commit hierarchy, towards particular commit # Usage: # gofwd v1.2.7 # Does nothing when the parameter is not specified. gofwd() { git checkout $(git rev-list --topo-order HEAD.."$*" | tail -1) } # Go back in Git commit hierarchy # Usage: # goback alias goback='git checkout HEAD~'
这篇关于如何在git中提交之间前后移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- the command inside