Search code examples
gitbranchgit-branch

How do I get the current branch name in Git?


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


Solution

  • 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
    

    Version 2.22 adds the --show-current option to ”print the name of the current branch”. The combination also works for freshly initialized repositories before the first commit:

    git branch --show-current