Is there a way to output the previous git branch name?

If you only want the name, you can use:

git name-rev $(git rev-parse @{-1}) --name-only

So for example:

$ git checkout mybranch
$ git checkout master
$ git name-rev $(git rev-parse @{-1}) --name-only
> mybranch

It is preferable to the accepted answer because it does not show "heads" in the name, so it can be chained with other git commands.


Maybe this is what you want:

git describe --all $(git rev-parse @{-1})

From the git-describe man page:

--all

Instead of using only the annotated tags, use any ref found in refs/ namespace. This option enables matching any known branch, remote-tracking branch, or lightweight tag.

So for example, if I do this:

$ git checkout mybranch
$ git checkout master
$ git describe --all $(git rev-parse @{-1})

I see:

heads/mybranch