Categories
branch git git-branch

How do I get the current branch name in Git?

3520

How do I get the name of the current branch in Git?

9

3674

git branch

should show all the local branches of your repo. The starred branch is your current branch.


To retrieve only the name of the branch you are on:

git rev-parse --abbrev-ref HEAD

or with Git 2.22 and above:

git branch --show-current

17

  • 1

    But that doesnt help me with Notepad++ and Netbeans. Just git bash (and Probobly Vim) and I mentioned that. I’m tring to work with other Ide’s and text editors that arent command line.

    – mike628

    Jun 5, 2011 at 20:30

  • @mike628 Actually they are helping you except you want something accessible through GUI. Correct?

    – Tadeck

    Jun 5, 2011 at 20:34

  • 2

    If you’re willing to work in Eclipse, there is a program called “eGit” that has a GUI that will tell you the current branch for all repos in it. Otherwise, I don’t know.. you would be at the mercy of the creator of whatever plugin you’d want to use that’s compatible with your choice of program (if there are any).

    Jun 5, 2011 at 20:51

  • 4

    after doing a git checkout --orphan foo then git branch failed to show branch foo. Whereas git symbolic-ref HEAD as suggested another answer worked.

    Sep 11, 2016 at 20:13


  • 1

    What does git rev-parse --abbrev-ref HEAD do exactly? I was previously using git describe --contains --all HEAD but that breaks sometimes and i’m not quite sure why.

    – Schneems

    Sep 10, 2020 at 15:23

3674

git branch

should show all the local branches of your repo. The starred branch is your current branch.


To retrieve only the name of the branch you are on:

git rev-parse --abbrev-ref HEAD

or with Git 2.22 and above:

git branch --show-current

17

  • 1

    But that doesnt help me with Notepad++ and Netbeans. Just git bash (and Probobly Vim) and I mentioned that. I’m tring to work with other Ide’s and text editors that arent command line.

    – mike628

    Jun 5, 2011 at 20:30

  • @mike628 Actually they are helping you except you want something accessible through GUI. Correct?

    – Tadeck

    Jun 5, 2011 at 20:34

  • 2

    If you’re willing to work in Eclipse, there is a program called “eGit” that has a GUI that will tell you the current branch for all repos in it. Otherwise, I don’t know.. you would be at the mercy of the creator of whatever plugin you’d want to use that’s compatible with your choice of program (if there are any).

    Jun 5, 2011 at 20:51

  • 4

    after doing a git checkout --orphan foo then git branch failed to show branch foo. Whereas git symbolic-ref HEAD as suggested another answer worked.

    Sep 11, 2016 at 20:13


  • 1

    What does git rev-parse --abbrev-ref HEAD do exactly? I was previously using git describe --contains --all HEAD but that breaks sometimes and i’m not quite sure why.

    – Schneems

    Sep 10, 2020 at 15:23

576

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

8

  • 10

    As all other answers, this doesn’t work when you are in a ‘detached HEAD’ state

    Jan 30, 2013 at 10:09

  • 63

    @CarlosCampderrós: if you’re in detached HEAD state, there is no such thing as a current branch. After all, the commit that you are in might be reachable by zero, one or more branches.

    – Flimm

    Jan 7, 2014 at 14:07

  • 2

    this makes problems in empty git repositories when there is no HEAD

    – Arne

    Mar 31, 2015 at 20:57

  • 12

    With git version 2.4.4 git rev-parse --abbrev-ref HEAD shows HEAD when you’re on detached head.

    – peterhil

    Jul 11, 2015 at 13:37

  • 1

    The best answer is still git symbolic-ref HEAD | sed -e "s/^refs\/heads\///" since it will display a string like HEAD detached at a63917f when in a detached state, unlike the other answers which show either nothing or HEAD. This is important.

    – Bernard

    Jan 15, 2016 at 6:12