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:
ID : 156
viewed : 190
Tags : gitbranchgit-branchgit
99
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:
85
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
77
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
69
For my own reference (but it might be useful to others) I made an overview of most (basic command line) techniques mentioned in this thread, each applied to several use cases: HEAD is (pointing at):
Results:
git branch | sed -n '/\* /s///p'
master
(detached from origin/master)
(detached from origin/feature-foo)
(detached from v1.2.3)
(HEAD detached at 285f294)
(detached from 285f294)
git status | head -1
# On branch master
# HEAD detached at origin/master
# HEAD detached at origin/feature-foo
# HEAD detached at v1.2.3
# HEAD detached at 285f294
# HEAD detached at 285f294
git describe --all
heads/master
heads/master
(note: not remotes/origin/master
)remotes/origin/feature-foo
v1.2.3
remotes/origin/HEAD
v1.0.6-5-g2393761
cat .git/HEAD
: ref: refs/heads/master
cat: .git/HEAD: Not a directory
git rev-parse --abbrev-ref HEAD
master
HEAD
git symbolic-ref --short HEAD
master
fatal: ref HEAD is not a symbolic ref
(FYI this was done with git version 1.8.3.1)
53
As of version 2.22 of git you could just use:
git branch --show-current
As per man page:
Print the name of the current branch. In detached HEAD state, nothing is printed.