How to find the last branch checked out in git
git, git-checkout
Solution
Your sample output (as produced by `git reflog | ...`) makes it sufficiently clear.
The `git rev-parse` command can be combined with the reference lookup syntax to do this in one go:
$ 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. Note as well that if there is no N'th previous branch, `rev-parse` silently prints nothing at all:
$ 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.
Problem
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? 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