如何在 git 中的提交之间向前和向后移动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6759791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:22:51  来源:igfitidea点击:

How do I move forward and backward between commits in git?

git

提问by Kostas

I am doing a git bisectand 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.

我正在做一个git bisect,在到达有问题的提交之后,我现在试图向前/向后迈出一步,以确保我是正确的。

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:

我知道HEAD^要在历史上倒退,但是否有另一种捷径可以让我前进(朝向未来的特定提交),如下所示:

A - B - C(HEAD) - D - E - F

I know that my target is Fand I want to move from Cto D.

我知道我的目标是F并且我想从C移动到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

注意:这不是Git: How to move to move between commits的副本,我的问题略有不同,没有在那里回答

采纳答案by jakub.g

I've experimented a bit and this seems to do the trick to navigate forwards (edit: it works well only when you have a linear history without merge commits):

我已经进行了一些实验,这似乎可以起到向前导航的作用(编辑:它仅在您拥有没有合并提交的线性历史记录时才有效):

git checkout $(git rev-list --topo-order HEAD..towards | tail -1)

where towardsis a SHA1 of the commit or a tag.

哪里towards是提交或标签的 SHA1。

Explanation:

解释:

  • the command inside $()means: get all the commits between current HEADand towardscommit (excluding HEAD), and sort them in the precedence order (like in git logby default -- instead of the chronological order which is weirdly the default for rev-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 checkoutto perform a checkout.
  • 里面的命令$()意味着:获取当前HEADtowards提交之间的所有提交(不包括HEAD),并按优先顺序对它们进行排序(就像git log默认情况下一样——而不是按时间顺序排列,这很奇怪,这是默认的rev-list),然后取最后一个( tail),即我们要去的那个。
  • 这在子shell中进行评估,并传递git checkout给执行结帐。

You can define a function accessible as a parameter-expecting alias in your .profilefile to navigate forward towards the particular commit:

您可以在.profile文件中定义一个可作为参数期望别名访问的函数,以向前导航到特定的提交:

# 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~'

回答by d3day

All you need to get clear, not detached head state is to reset, not checkout.

所有你需要清楚,而不是分离的头部状态是重置,而不是结帐。

git reset HEAD@{1}

回答by w0utert

I believe you can do:

我相信你可以做到:

git reset HEAD@{1}

To go one commit forward in time. To go forward multiple commits, use HEAD@{2}, HEAD@{3}, etc.

及时进行一次提交。要进行多次提交,请使用 HEAD@{2}、HEAD@{3} 等。

回答by M K

This is what I'm using to navigate back and forth.

这就是我用来来回导航的。

moving to next commit

移动到下一次提交

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

moving to previous commit

移动到上一个提交

function p() {
    git checkout HEAD^1
}

回答by Useless

Say F is the latest commit on trunk(insert your own branch name here) ... you can refer to it as trunk~0(or just trunk), E as trunk~1, D as trunk~2etc.

假设 F 是最新提交trunk(在此处插入您自己的分支名称)……您可以将其称为trunk~0(或仅trunk称为 )、E 为trunk~1、Dtrunk~2等。

Take a look in your reflogfor yet more ways to name commits.

查看您的reflog以了解更多命名提交的方法。

回答by Dziamid

Traversing backward is trivial since you are moving down the tree, and there's always one way to go

向后遍历是微不足道的,因为您正在沿着树向下移动,并且总有一种方法可以走

  function git_down
        git checkout HEAD^
  end

When traversing forward you are moving up the tree, so you need to be explicit which branch you are targeting:

当向前遍历时,您正在向上移动树,因此您需要明确您的目标分支:

  function git_up 
        git log --reverse --pretty=%H $argv | grep -A 1 (git rev-parse HEAD) | tail -n1 | xargs git checkout
  end

Usage: git down, git up <branch-name>

用法:git down,git up <branch-name>

回答by dimpiax

If you want to see ahead, you can do this trick, as Git doesn't have strict command for it.

如果你想提前看,你可以做这个技巧,因为 Git 没有严格的命令。

git log --reverse COMMIT_HASH..

Example

例子

List of log history hashes:

日志历史哈希列表:

A
B
C -> put this
D

using command git log --reverse C.., in output you will see Band A.

使用 command git log --reverse C..,在输出中你会看到BA

回答by user1322977

I just did a test on this. say for example you are in master branch Then do:

我刚刚对此进行了测试。比如说你在 master 分支然后做:

git checkout HEAD@{3}

So head gets detached, and you can then try it again to go to any other commit:

因此 head 被分离,然后您可以再次尝试以转到任何其他提交:

git checkout HEAD@{4}

Once you are done looking around, you can go back to your original state just by checking out to that branch. In my example: master branch

环顾四周后,只需签出该分支即可返回原始状态。在我的例子中: master 分支

git checkout master

If you don't want to go to original state, and want so keep one of the commits as your head and continue from there, then you need to branch out from there. for example after "git checkout HEAD@{4}" , you can issue

如果您不想进入原始状态,并且希望将其中一个提交作为您的头并从那里继续,那么您需要从那里分支出来。例如在 "git checkout HEAD@{4}" 之后,您可以发出

git checkout -b MyNewBranch

回答by bb-generation

Probably not the nicest way but you can use git logto view the list of commits and then use git checkout [sha1 of D]to move to D.

可能不是最好的方法,但您可以使用git log查看提交列表,然后使用git checkout [sha1 of D]移动到 D。

回答by Patrizio Bertoni

As a workaround, you can just return to HEAD with

作为一种解决方法,您可以使用以下命令返回 HEAD

git checkout <branch>

And then move to the commit you'd like to, with

然后转到您想要的提交,使用

git checkout HEAD~<offset>