Easily get current branch name in git

kakty3 profile image kakty3 ・1 min read

Sometimes I miss the features from Mercurial which Git does not have. One of them is hg outgoing which "Show changesets not found in the specified destination repository or the default push location". Similar behavior could be achieved with git log branchA..branchB, e.g to see outgoing commits: git log origin/master..feature-branch.

One day I used that command for the branch with really long name. I just typed a few fisrt letters, pressed tab and branch name was auto-completed (I personally prefer fish shell, but it should also work for all other modern shells). I asked myself: "What if I am using bash and I don't have the auto-completion, but I also don't want to use git branch and copy-and-paste branch name?".

The answer is to use git rev-parse --abbrev-ref HEAD:

> git rev-parse --abbrev-ref HEAD

I've created the alias for it:

git config --global alias.cb 'rev-parse --abbrev-ref HEAD'

where cb stands for current branch.

Now I can do following

git log origin/master..$(git cb)

Suddenly I found out that passing remote-branch is not needed and previous command could be simplified:

git log origin/master..


Editor guide
lucasreeh profile image
Lucas Reeh

Thanks for the Info. I am using Oh my zsh based on zsh where you see the current branch directly in your prompt. It is very easy to install (it is a git repo itself with install script on github), so I even install it in some dev docker containers.

my favorite theme:
Agnoster theme

andy profile image
Andy Zhao (he/him)

I'm tempted to use zsh with Oh my zsh (along with Vim), but haven't quite decided to switch over yet. My bash has something similar, where the branch is in the prompt:

bash branches

lucasreeh profile image
Lucas Reeh

without trying to sound like a salesman ;) (it is open-source anyway): i foremost like the completion from history (works with spaces too). e.g. type: git commit + arrow up and you get all your recent commit commands. :)

sabareeshk1991 profile image

me too:)

mgtitimoli profile image
Mauro Gabriel Titimoli

I was used to do the following

git branch | grep -e "^\*" | cut -d " " -f 2

This alternative, as it happens a lot with git, doesn't expose its purpuse clearly, but at least it provides a way to get the current branch, so I guess I will move to this approach instead.

Thanks for sharing!

pratyaypandey profile image
Pratyay Pandey

If still you wish to use git branch without copy and paste you can probably try this

git branch | while read -r line ; do; if [[ ${line:0:1} == "*" ]]; then; echo $line | awk '{print $2}'; fi; done

hawkinjs profile image
Josh Hawkins

While not a fancy one-liner or anything, I tend to just use git status to see "On branch ____" and just copy that as needed. Not great for scripting but easy to remember

taylor profile image
Taylor D. Edmiston

This is what I do too. Or in Sublime using the Git plugin, activate the command palette then type "br" which jumps to "Git: Change Branch" with an asterisk next to the current branch.

plutov profile image
Alex Pliutau

Alias for bash to show current branch in folder:

parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
export PS1="\w$(parse_git_branch)"