How to get the current branch name in Git?
To display the current branch you're on, without the other branches listed, you can do the following:
git rev-parse --abbrev-ref HEAD
Reference:
git branch
should show all the local branches of your repo. The starred branch is your current branch.
If you want to retrieve only the name of the branch you are on, you can do:
git rev-parse --abbrev-ref HEAD
or with Git 2.22 and above:
git branch --show-current
You have also git symbolic-ref HEAD
which displays the full refspec.
To show only the branch name in Git v1.8 and later (thank's to Greg for pointing that out):
git symbolic-ref --short HEAD
On Git v1.7+ you can also do:
git rev-parse --abbrev-ref HEAD
Both should give the same branch name if you're on a branch. If you're on a detached head answers differ.
Note:
On an earlier client, this seems to work:
git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"
– Darien 26. Mar 2014