有没有办法 git checkout 上一个分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7206801/
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:42:19  来源:igfitidea点击:

Is there any way to git checkout previous branch?

gitgit-checkout

提问by Matt Briggs

I sort of want the equivalent of cd -for git. If I am in branch masterand I checkout foo, I would love to be able to type something like git checkout -to go back to master, and be able to type it again to return to foo.

我有点想要cd -git的等价物。如果我在分行master并且结帐时foo,我希望能够输入类似git checkout -返回的内容master,并且能够再次输入以返回foo

Does anything like this exist? Would it be hard to implement?

这样的东西存在吗?实施起来会不会很困难?

回答by Karl Bielefeldt

From the release notes for 1.6.2

来自1.6.2发行说明

@{-1}is a way to refer to the last branch you were on. This is
accepted not only where an object name is expected, but anywhere a branch name is expected and acts as if you typed the branch name.
E.g. git branch --track mybranch @{-1}, git merge @{-1}, and
git rev-parse --symbolic-full-name @{-1}would work as expected.

@{-1}是一种引用您所在的最后一个分支的方式。这
不仅在需要对象名称的地方被接受,而且在任何需要分支名称的地方都被接受,并且就像您输入了分支名称一样。
例如git branch --track mybranch @{-1}, git merge @{-1}, 并且
git rev-parse --symbolic-full-name @{-1}会按预期工作。

and

git checkout -is a shorthand for git checkout @{-1}.

git checkout -是 的简写git checkout @{-1}

回答by marcgg

The simplest way of doing this nowadays is:

现在最简单的方法是:

git checkout -

... which is an alias of:

...这是一个别名:

git checkout @{-1}

git checkout minus

git checkout 减去

If you want to know more about this, I wrote an entire article about it here: Checkout The Previous Branch In Git.

如果你想了解更多,我在这里写了一篇关于它的完整文章:Checkout The Previous Branch In Git

回答by manojlds

As @Karl points out and from git checkoutmanual:

正如@Karl 从git checkout手册中指出的那样:

As a special case, the "@{-N}" syntax for the N-th last branch checks out the branch (instead of detaching). You may also specify - which is synonymous with "@{-1}".

作为一种特殊情况,第 N 个最后一个分支的“@{-N}”语法检查分支(而不是分离)。您还可以指定 - 与“@{-1}”同义。

So both git checkout -and git checkout @{-1}would work in this case

因此,无论git checkout -并且git checkout @{-1}在这种情况下会工作

Closest I believe is using the git reflogand parse the latest moving from branch1 to branch2and git checkout branch1

我相信最近的是使用git reflog并解析最新的moving from branch1 to branch2git checkout branch1

回答by ddd

Just adding some more detail to the previous answers to understand the mechanism by which git checkout @{-N}works. It walks the reflog to inspect the checkout history, so if you wanted to implement something similar on your own you should be able to parse the output of git refloglooking for checkout:lines. You can check the implementation in the git source sha1_name.c, specifically the function interpret_nth_prior_checkout.

只需在之前的答案中添加更多细节即可了解其git checkout @{-N}工作机制。它遍历 reflog 以检查结帐历史记录,因此如果您想自己实现类似的东西,您应该能够解析git reflog查找checkout:行的输出。您可以在 git 源代码中检查实现sha1_name.c,特别是函数interpret_nth_prior_checkout

回答by Mike

Git version 2.23introduced the git switchcommand which you can use to do that (and more). Quoting the official documentation:

Git 版本2.23引入了git switch可用于执行此操作(以及更多)的命令。引用官方文档:

Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch.

切换到指定的分支。更新工作树和索引以匹配分支。所有新提交都将添加到此分支的尖端。

In your specific case you can issue git switch -to go back to the branch you were previously on. You can execute the same command again to return to the first branch.

在您的特定情况下,您可以发出git switch -返回到您之前所在的分支的命令。您可以再次执行相同的命令以返回到第一个分支。

P.S. It's not that you don't already know how to do that (I mean it's been 7 years since you asked this question), but this command is less confusing and friendly to beginners as it addresses a common confusionthat arises when using git checkout.

PS这并不是说你已经不知道该怎么做(我的意思是已经7年,因为你问到这个问题),但该命令减少混乱和友好的初学者,因为它解决了一个共同的困惑使用时产生的git checkout

回答by Venkat.R

I landed to this question with the same thought to checkout my previous branch. I'm using ohmyzin Mac. Below command helped me.

我带着同样的想法登陆了这个问题,以查看我以前的分支。我使用ohmyzMac。下面的命令帮助了我。

$ gco -
$ git checkout -

回答by Hymankobec

The most popular solution is:

最流行的解决方案是:

git checkout @{-N}

Where N - step count of the branches to move back on the checkout history.

其中 N - 分支的步数以返回结帐历史记录。

回答by Rory O'Kane

Here are pointers to the parts of Git's documentation that describe the git checkout -and git checkout @{-1}solutions given by the other answers:

以下是指向Git的文档中描述的部分git checkout -,并git checkout @{-1}通过其他的答案给出解决方案:

  • When specifying a Git revision for any command, @{-<n>}, e.g. @{-1}means “the nth branch/commit checked out before the current one.” The documentation for git checkout <branch>reiterates: “You can use the @{-N}syntax to refer to the N-th last branch/commit checked out using git checkoutoperation.”

  • For the <branch>argument of git checkout, “you may also specify ‘-' which is synonymous to ‘@{-1}'.”

  • 当为任何命令指定 Git 修订版时@{-<n>},例如@{-1}表示“在当前分支/提交之前签出的第n个分支/提交”。文档git checkout <branch>重申:“您可以使用@{-N}语法来引用使用git checkout操作检出的第 N 个最后一个分支/提交。”

  • 对于 的<branch>参数git checkout,“您还可以指定与 ' -' 同义的 ' @{-1}'。”