有没有办法 git checkout 以前的分支?
我有点想要相当于 git 的 cd - 。如果我在分支 master
中并签出 foo
,我希望能够输入类似 git checkout -
的内容以返回到 < code>master,并且能够再次键入它以返回到foo
。
有这样的事情存在吗?实施起来会不会很困难?
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
.
Does anything like this exist? Would it be hard to implement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
来自 1.6.2 发行说明
和
要查看之前结帐的列表:
From the release notes for 1.6.2
and
To see the list of previous checkouts:
现在最简单的方法是:
... 这是以下的别名:
如果您想了解更多信息,我在这里写了整篇文章:签出 Git 中的上一个分支。
The simplest way of doing this nowadays is:
... which is an alias of:
If you want to know more about this, I wrote an entire article about it here: Checkout The Previous Branch In Git.
Git 版本
2.23
引入了git switch
命令,您可以使用它来执行此操作(以及更多操作)。引用官方文档:在您的具体情况下,您可以发出 git switch - 返回到您之前所在的分支。您可以再次执行相同的命令以返回到第一个分支。
该命令不太令人困惑且对初学者友好,因为它解决了常见的 使用 git checkout 时出现的混乱。
Git version
2.23
introduced thegit switch
command which you can use to do that (and more). Quoting the official documentation: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.This command is less confusing and friendly-to-beginners as it addresses a common confusion that arises when using
git checkout
.正如 @Karl 所指出的,并来自 git checkout 手册:
因此,
git checkout -
和git checkout @{-1}
在这种情况下都可以工作我认为最接近的是使用git reflog
并解析最新的从branch1移动到branch2
和git checkoutbranch1
As @Karl points out and from
git checkout
manual:So both
git checkout -
andgit checkout @{-1}
would work in this caseClosest I believe is using thegit reflog
and parse the latestmoving from branch1 to branch2
andgit checkout branch1
我以同样的想法提出这个问题来检查我以前的分支。我在
Mac
中使用 ohmyz。下面的命令帮助了我。I landed to this question with the same thought to checkout my previous branch. I'm using ohmyz in
Mac
. Below command helped me.只需在前面的答案中添加一些更多细节即可了解 git checkout @{-N} 的工作机制。它会遍历 reflog 来检查结账历史记录,因此,如果您想自己实现类似的功能,您应该能够解析 git reflog 的输出,查找
checkout:
线。您可以在 git 源sha1_name.c
中检查实现,特别是函数interpret_nth_prior_checkout
。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 ofgit reflog
looking forcheckout:
lines. You can check the implementation in the git sourcesha1_name.c
, specifically the functioninterpret_nth_prior_checkout
.以下是 Git 文档中描述其他答案给出的
git checkout -
和git checkout @{-1}
解决方案的部分的指针:当指定任何命令的 Git 修订版,
@{-}
,例如@{-1}
意思是“在当前分支/提交之前签出的n个分支/提交”。git checkout
的文档 重申:“您可以使用@{-N}
语法来引用使用git checkout
操作签出的最后 N 个分支/提交。”对于
git checkout
的参数,“您还可以指定 '-
”,它与'@{-1}
'。”Here are pointers to the parts of Git’s documentation that describe the
git checkout -
andgit checkout @{-1}
solutions given by the other answers: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}
’.”最流行的解决方案是:
其中 N - 在结帐历史记录中向后移动的分支的步骤计数。
The most popular solution is:
Where N - step count of the branches to move back on the checkout history.
请参阅
git chekout
和git switch
有不同的用途。它更像是 typescript 和 javascript。请参阅 javascript 是 typescript 的一部分,类似地,git switch
具有git checkout
的一些功能。git checkout
主要用于提交和时间轴移动之间的切换。它也可以做git switch
可以做的所有事情。git switch
专门用于分支之间的切换,并完成与分支相关的所有工作。所以更好用
See
git chekout
andgit switch
have different purpose. Its more like typescript and javascript. See javascript is a part of typescript similarlygit switch
has some of the capabilities ofgit checkout
.git checkout
is majorly used for switch between commit and travelling in timeline. It can also do all the things whichgit switch
can.git switch
was specifically made for switch between branches and do all the work related to the branches.So better to use
就我而言,我已从 master 切换到 gh-pages,这导致我的所有组件消失并被文件名“static”和其他文件替换。
git checkout -m master 帮助了
in my case I had switched from master to gh-pages which cause all my components to disappear and get replaced by a file names "static" and others.
git checkout -m master helped