有没有办法 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
Is there any way to git checkout previous branch?
提问by Matt Briggs
I sort of want the equivalent of cd -
for git. If I am in branch master
and 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}
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}
, andgit 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 forgit 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}
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 checkout
manual:
正如@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 reflog
and parse the latest moving from branch1 to branch2
and git checkout branch1
我相信最近的是使用git reflog
并解析最新的moving from branch1 to branch2
和git 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 reflog
looking 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.23
introduced the git switch
command 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
回答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 forgit checkout <branch>
reiterates: “You can use the@{-N}
syntax to refer to the N-th last branch/commit checked out usinggit checkout
operation.”For the
<branch>
argument ofgit 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}
'。”