简体   繁体   English

如何找到在git中检出的最后一个分支

[英]How to find the last branch checked out in git

We can checkout the last branch using git checkout - , but is there a way to just find out what was last branch and not check it out? 我们可以使用git checkout -签出最后一个分支git checkout -但是有没有办法找出最后一个分支是什么而不是检查出来?

EDIT: I already found that I could use: 编辑:我已经发现我可以使用:

git reflog | grep -i "checkout: moving"|head -1|cut -d' ' -f6

But I wanted to know if there is a direct simpler command. 但我想知道是否有一个直接简单的命令。 I am updating the question to reflect this need. 我正在更新问题以反映这一需求。 Sorry about not being clear enough 抱歉不够清楚

Your sample output (as produced by git reflog | ... ) makes it sufficiently clear. 您的示例输出(由git reflog | ... )使其足够清晰。

The git rev-parse command can be combined with the reference lookup syntax to do this in one go: git rev-parse命令可以与参考查找语法结合使用,一次完成:

$ git rev-parse --symbolic-full-name @{-1}
refs/heads/stash-exp
$ git rev-parse --abbrev-ref @{-1}
stash-exp

Note that the gitrevisions documentation describes the @{- N } syntax. 请注意, gitrevisions文档描述了@{- N }语法。 Note as well that if there is no N'th previous branch, rev-parse silently prints nothing at all: 还要注意,如果没有第N个前一个分支,则rev-parse默默地打印任何内容:

$ git rev-parse --abbrev-ref @{-2} && echo ok || echo fail
master
ok
$ git rev-parse --abbrev-ref @{-3} && echo ok || echo fail
ok

And, of course, in most places where you might need the name, you can just use the @{-1} syntax directly. 当然,在您可能需要名称的大多数地方,您可以直接使用@{-1}语法。

git reflog is what you want to use. git reflog是你想要使用的。 In this, you'll find: 在这里,你会发现:

  • What you've last committed (denoted by "commit") 你最后提交的内容(用“提交”表示)
  • What you've checked out (denoted by "checkout") 你检查了什么(用“结账”表示)
  • When you've pulled (denoted by "pull") 当你拉(用“拉”表示)

What you've checked out is important here; 你在这里检查的内容非常重要; the reflog will use a format denoted with "checkout" and specify "moving from A to B" when looking at specific branches. reflog将使用以“checkout”表示的格式,并在查看特定分支时指定“从A移动到B”。

A simple grep for "checkout: moving" will give you a list of the branches you've visited; 一个简单的grep for“checkout:moving”会给你一个你去过的分支列表; the last one is at the top. 最后一个是在顶部。

Full command for completeness: 完整命令:

git reflog | grep -i "checkout: moving"

使用grep -m来限制结果:

git reflog | grep -i “checkout: moving” -m 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM