Get previous branch name

与世无争的帅哥 提交于 2020-06-27 09:50:32

问题


I really like git checkout - to move to my previous branch.

But sometimes I only need to know what my previous branch name is. How could I ask Git that?

For example, if git checkout - moves to branch "prev", I want the command to get just "prev".


回答1:


git checkout - is shorthand for git checkout @{-1} (see here):

You can use the @{-N} syntax to refer to the N-th last branch/commit checked out using "git checkout" operation. You may also specify - which is synonymous to @{-1}.

You can pass this same reference to rev-parse to get the commit or branch in question:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
$ git checkout not-master
Switched to branch 'not-master'
Your branch is up to date with 'origin/not-master'.
$ git rev-parse --symbolic-full-name @{-1}
refs/heads/master


来源:https://stackoverflow.com/questions/61128146/get-previous-branch-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!