How do I get the name of the current branch in Git?
51 Answers
To display only the name of the current branch you're on:
git rev-parse --abbrev-ref HEAD
Reference: Show just the current branch in Git
-
109Good one, sadly it does not work if you are in a 'detached HEAD' state (it just outputs 'HEAD', which is totally useless). Jan 30, 2013 at 9:38
-
48I guess by the git internals if you are in a 'detached HEAD' state there is no tracking of the branch it belongs to, because
git branch
shows* (no branch)
, which is also useless... Jan 30, 2013 at 9:40 -
83"git symbolic-ref --short HEAD" also works for this same purpose Mar 22, 2013 at 23:30
-
26
git rev-parse --abbrev-ref HEAD 2>/dev/null
The /dev/null part prevents you from seeing an error if you just created a new repository that has not yet HEAD. Jun 14, 2013 at 14:57 -
18
git symbolic-ref --short HEAD
seems to be more versatile.git rev-parse --abbrev-ref HEAD
yieldsHEAD
if the repo is a just-initialized one. Mar 5, 2016 at 21:33
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
-
2But 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.– mike628Jun 5, 2011 at 20:30
-
@mike628 Actually they are helping you except you want something accessible through GUI. Correct?– TadeckJun 5, 2011 at 20:34
-
2If 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
-
4after doing a
git checkout --orphan foo
thengit branch
failed to show branchfoo
. Whereasgit symbolic-ref HEAD
as suggested another answer worked. Sep 11, 2016 at 20:13 -
1What does
git rev-parse --abbrev-ref HEAD
do exactly? I was previously usinggit describe --contains --all HEAD
but that breaks sometimes and i'm not quite sure why.– SchneemsSep 10, 2020 at 15:23
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
-
11As all other answers, this doesn't work when you are in a 'detached HEAD' state Jan 30, 2013 at 10:09
-
65@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.– FlimmJan 7, 2014 at 14:07
-
2
-
14With git version 2.4.4
git rev-parse --abbrev-ref HEAD
showsHEAD
when you’re on detached head.– peterhilJul 11, 2015 at 13:37 -
1The best answer is still
git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"
since it will display a string likeHEAD detached at a63917f
when in a detached state, unlike the other answers which show either nothing or HEAD. This is important.– BernardJan 15, 2016 at 6:12
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):
- local branch (master)
- remote tracking branch, in sync with local branch (origin/master at same commit as master)
- remote tracking branch, not in sync with a local branch (origin/feature-foo)
- tag (v1.2.3)
- submodule (run inside the submodule directory)
- general detached head (none of the above)
Results:
git branch | sed -n '/\* /s///p'
- local branch:
master
- remote tracking branch (in sync):
(detached from origin/master)
- remote tracking branch (not in sync):
(detached from origin/feature-foo)
- tag:
(detached from v1.2.3)
- submodule:
(HEAD detached at 285f294)
- general detached head:
(detached from 285f294)
- local branch:
git status | head -1
- local branch:
# On branch master
- remote tracking branch (in sync):
# HEAD detached at origin/master
- remote tracking branch (not in sync):
# HEAD detached at origin/feature-foo
- tag:
# HEAD detached at v1.2.3
- submodule:
# HEAD detached at 285f294
- general detached head:
# HEAD detached at 285f294
- local branch:
git describe --all
- local branch:
heads/master
- remote tracking branch (in sync):
heads/master
(note: notremotes/origin/master
) - remote tracking branch (not in sync):
remotes/origin/feature-foo
- tag:
v1.2.3
- submodule:
remotes/origin/HEAD
- general detached head:
v1.0.6-5-g2393761
- local branch:
cat .git/HEAD
:- local branch:
ref: refs/heads/master
- submodule:
cat: .git/HEAD: Not a directory
- all other use cases: SHA of the corresponding commit
- local branch:
git rev-parse --abbrev-ref HEAD
- local branch:
master
- all the other use cases:
HEAD
- local branch:
git symbolic-ref --short HEAD
- local branch:
master
- all the other use cases:
fatal: ref HEAD is not a symbolic ref
- local branch:
(FYI this was done with git version 1.8.3.1)
-
7
-
8This was quite helpful for me:
git describe --all --exact-match 2>/dev/null | sed 's=.*/=='
was the best solution for me (good names for tags and branch heads, no output for random detached heads. May 18, 2015 at 9:31 -
1However, I just discovered that using
git describe
has a serious failing when there are multiple branches referencing the same commit, e.g. right aftergit checkout -b foo
- it uses one of them arbitrarily (seems like maybe the most recently created one). I will change my strategy to use filtered output fromgit branch
and only usegit describe
if the result is something about a detached head. May 20, 2015 at 7:31 -
2"current branch" (set by
checkout foo
) and "current commit" are 2 distinct concepts.symbolic-ref
only looks at active branch.describe
only looks at a commit, and chooses heuristically from all branches/tags pointing to it (or near it). DWIM commands likebranch
andstatus
use current branch when defined, but all of them choose heuristically in all "detached HEAD" situations. Apr 22, 2020 at 18:52 -
1Can you add the results of
git branch --show-current
to this list? The behavior is that when a local branch is checked out, it prints the branch short name (ex: "master"), and otherwise, it prints nothing, returns success. Aug 5, 2021 at 15:35
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.
-
2Yes, I mentioned that last March in the comments of that page: stackoverflow.com/questions/6245570/…. And in stackoverflow.com/a/55088865/6309.– VonCJun 7, 2019 at 22:37
-
10At least mention this does not work in detached HEAD state. Just lost hours getting git 2.22 to compile for nothing ... Dec 16, 2019 at 4:02
-
Available since Git version 2.22 : github.com/git/git/blob/master/Documentation/RelNotes/…– LenormjuJul 1, 2020 at 8:26
-
1
-
2@DavidDeprost but it is working. When in a detached head state, you aren't on a branch. So showing nothing is a very reasonable response to that. What did you expect to happen?– ShadowOct 20, 2022 at 0:08
One more alternative:
git name-rev --name-only HEAD
-
it also can be retrieved with
echo ${$(git symbolic-ref --quiet HEAD)#refs/heads/}
– AntoineMar 16, 2016 at 14:24 -
15It does not work if HEAD is the same for master and feature branch (e.g. during merge). It returns 'master' even if executed on the feature branch. Apr 6, 2016 at 9:05
-
1
git checkout master && git name-rev --name-only HEAD # ac-187
It does not work as expected May 19, 2016 at 13:41 -
I save this into a variable just before merging and also in cases my HEAD might get de-attached if I checkout a particular commit. In these case this works fine. Nov 12, 2016 at 17:16
-
6I'm doing this from a Jenkins pipeline. So this appears to be for the time being the best answer for me. Doing
git branch --list
just says* (HEAD detached at 7127db5)
. Doinggit rev-parse --abbrev-ref HEAD
just saysHEAD
and so on.– Mig82Jun 19, 2017 at 11:53
Well simple enough, I got it in a one liner (bash)
git branch | sed -n '/\* /s///p'
(credit: Limited Atonement)
And while I am there, the one liner to get the remote tracking branch (if any)
git rev-parse --symbolic-full-name --abbrev-ref @{u}
-
11Too many slashes! :)
sed -n 's/\* //p'
does the trick. Although I tend toward the paranoid so I would anchor it withsed -n 's/^\* //p'
. Aug 4, 2014 at 14:45
write the following command in terminal :
git branch | grep \*
or
git branch --show-current
or on Git 2.22 and above:
git branch --show
You can just type in command line (console) on Linux, in the repository directory:
$ git status
and you will see some text, among which something similar to:
...
On branch master
...
which means you are currently on master
branch. If you are editing any file at that moment and it is located in the same local repository (local directory containing the files that are under Git version control management), you are editing file in this branch.
-
3Based on what you want to do, you can use
git status
and get only the first line of output withgit status | head -1
which yields something like# On branch master
. I'm sure version differences will needed to be accounted for as well. Dec 31, 2011 at 20:43 -
@JoshPinter: You can also use
git status | grep 'On branch'
, which should have the same effect (should, does not mean it will if your version of Git displays it differently). Orgit branch | grep '*'
, which will show the name of the branch with a star at the beginning of it.– TadeckJan 1, 2012 at 0:38 -
Yep, that works as well and might be more flexible. My final result for showing just the branch name in a dev Rails app was:
<tick>git status | head -1<tick>.gsub('# On branch ', '')
Jan 4, 2012 at 19:45 -
3
git status
can take a long time to return a value if there are a lot of files being managed. May 9, 2016 at 23:53 -
Yeah, if you want to print the branch on a webpage, for example,
git status
may tank the whole page's generation time. Jul 3, 2019 at 18:06
git symbolic-ref -q --short HEAD
I use this in scripts that need the current branch name. It will show you the current short symbolic reference to HEAD, which will be your current branch name.
-
1Thanks, works great! - I'm also adding "-C path_to_folder" in my script with this.– TonyAug 6, 2015 at 10:02
-
1This is a nice solution because with the -q option it returns an error code in the "detached HEAD" state but does not print anything to stderr. Mar 31, 2019 at 16:26
-
this is the only solution that worked for me on a fresh repo without any commits– niryoApr 14, 2019 at 11:55
To get the current branch in git use,
git branch --show-current
-
1you have to finally edit this comment and remove the problemmatic dash, people copypaste answers– SychDec 14, 2021 at 13:42
-
2Hello @sych Sorry -- What is problemmatic with the dash in this command? This works fine copy-pasted on my machine (Linux, mint flavour, git version 2.25.1) Dec 29, 2021 at 8:40
-
1
git branch | grep -e "^*" | cut -d' ' -f 2
will show only the branch name
-
If your branch shows somethig like this "* (HEAD detached at SUM_BRANCH_01)", then try this "git branch | grep -e "^*" | cut -d' ' -f 5 | cut -d ')' -f 1" Aug 4, 2016 at 21:42
-
I just created this exact same script to get the current branch name. I figured it might help with diffs.– tggagneAug 9, 2016 at 4:57
git branch
show current branch name only.
While git branch will show you all branches and highlight the current one with an asterisk, it can be too cumbersome when working with lots of branches.
To show only the branch you are currently on, use:
git rev-parse --abbrev-ref HEAD
-
-
-
1@DylanNicholson
git branch --contains
sometimes lists more than one branch. Aug 3, 2018 at 11:21
Found a command line solution of the same length as Oliver Refalo's, using good ol' awk:
git branch | awk '/^\*/{print $2}'
awk
reads that as "do the stuff in {}
on lines matching the regex". By default it assumes whitespace-delimited fields, so you print the second. If you can assume that only the line with your branch has the *, you can drop the ^. Ah, bash golf!
Sorry this is another command-line answer, but that's what I was looking for when I found this question and many of these answers were helpful. My solution is the following bash shell function:
get_branch () {
git rev-parse --abbrev-ref HEAD | grep -v HEAD || \
git describe --exact-match HEAD 2> /dev/null || \
git rev-parse HEAD
}
This should always give me something both human-readable and directly usable as an argument to git checkout
.
- on a local branch:
feature/HS-0001
- on a tagged commit (detached):
v3.29.5
- on a remote branch (detached, not tagged): SHA1
- on any other detached commit: SHA1
-
1Thanks for posting this, none of the other answers seemed to care about always producing something usable as an argument to
git checkout
.– zwolDec 9, 2018 at 16:43
A less noisy version for git status would do the trick
git status -bsuno
It prints out
## branch-name
-
2
-
No way, it takes a few minutes to refresh index, then some time just hanging, and finally spits out several screens of file paths. Considering there's even no explanation on how it works and why it may be better than other suggestion, I'm giving it a -1– RAM237Aug 14, 2022 at 20:22
Why not use git-aware shell prompt, which would tell you name of current branch? git status
also helps.
How git-prompt.sh from contrib/
does it (git version 2.3.0), as defined in __git_ps1
helper function:
First, there is special case if rebase in progress is detected. Git uses unnamed branch (detached HEAD) during the rebase process to make it atomic, and original branch is saved elsewhere.
If the
.git/HEAD
file is a symbolic link (a very rare case, from the ancient history of Git), it usesgit symbolic-ref HEAD 2>/dev/null
Else, it reads
.git/HEAD
file. Next steps depends on its contents:If this file doesn't exist, then there is no current branch. This usually happens if the repository is bare.
If it starts with
'ref: '
prefix, then.git/HEAD
is symref (symbolic reference), and we are on normal branch. Strip this prefix to get full name, and striprefs/heads/
to get short name of the current branch:b="${head#ref: }" # ... b=${b##refs/heads/}
If it doesn't start with
'ref: '
, then it is detached HEAD (anonymous branch), pointing directly to some commit. Usegit describe ...
to write the current commit in human-readable form.
I hope that helps.
-
4And if you are developing a git-aware shell prompt, which of the answers here should you use? Turtles all the way down.– tripleeeFeb 18, 2015 at 11:52
-
1@tripleee: Borrow ideas from github.com/git/git/blob/master/contrib/completion/git-prompt.sh Feb 18, 2015 at 12:00
-
4Which for the record appears to be doing
git describe --contains --all HEAD
which I don't currently see elsewhere on this page. As I'm sure you know, link-only answers are not recommended on StackOverflow.– tripleeeFeb 18, 2015 at 12:11 -
2@tripleee: I have added an explanation how
git-prompt.sh
(aka__git_ps1
) does it... Feb 20, 2015 at 17:51
There is various way to check the current branch of Git
but I prefer :
git branch --show
Even git branch
also shows the current branch name along with all existing branch name list.
#!/bin/bash
function git.branch {
br=`git branch | grep "*"`
echo ${br/* /}
}
git.branch
you can use git bash on the working directory command is as follow
git status -b
it will tell you on which branch you are on there are many commands which are useful some of them are
-s
--short Give the output in the short-format.
-b --branch Show the branch and tracking info even in short-format.
--porcelain[=] Give the output in an easy-to-parse format for scripts. This is similar to the short output, but will remain stable across Git versions and regardless of user configuration. See below for details.
The version parameter is used to specify the format version. This is optional and defaults to the original version v1 format.
--long Give the output in the long-format. This is the default.
-v --verbose In addition to the names of files that have been changed, also show the textual changes that are staged to be committed (i.e., like the output of git diff --cached). If -v is specified twice, then also show the changes in the working tree that have not yet been staged (i.e., like the output of git diff).
git status
will also give the branch name along with changes.
e.g.
>git status
On branch master // <-- branch name here
.....
I would try one of the following:
1.> git symbolic-ref --short HEAD
git symbolic-ref --short HEAD
>>> sid-dev
git branch --show-current
>>> sid-dev
3.> git name-rev –name-only HEAD
git name-rev –name-only HEAD
>>> HEAD sid-dev
Notes:
1.> git symbolic-ref --short HEAD displays the short symbolic reference to the current branch’s HEAD. This is the current branch name.
2.> git branch --show-current is also a simple and efficient way to print the current branch name.
3.> git name-rev –name-only HEAD gives the symbolic name for
HEAD
revision of the current branch
4.> In the above examples,
sid-dev
is the name of my branch.
Over time, we might have a really long list of branches.
While some of the other solutions are great, Here is what I do (simplified from Jacob's answer):
git branch | grep \*
Now,
git status
works, but only If there are any local changes
I recommend using any of these two commands.
git branch | grep -e "^*" | cut -d' ' -f 2
OR
git status | sed -n 1p | cut -d' ' -f 3
OR (more verbose)
git status -uno -bs| cut -d'#' -f 3 | cut -d . -f 1| sed -e 's/^[ \t]//1'| sed -n 1p
In Netbeans, ensure that versioning annotations are enabled (View -> Show Versioning Labels). You can then see the branch name next to project name.
-
With versioning annotations enabled, then all you need to do is hover your mouse over the Project (or File, or Favorite) folder to see the current branch.– idclaarSep 30, 2015 at 0:06
What about this?
{ git symbolic-ref HEAD 2> /dev/null || git rev-parse --short HEAD 2> /dev/null } | sed "s#refs/heads/##"
-
1
-
2
-
@Pat There's no need to spawn a separate subshell for this, as ( ) would do. { } is fine, except you do need to add a ; or newline before the }. Actually, you could just leave off the { } entirely unless you needed to group the commands.– aijFeb 28, 2015 at 15:51
-
1Doesn't the
symbolic-ref
part also need--short
to avoid prefixing the branchname withrefs/heads/
?– rjpJan 2, 2018 at 15:41
The following shell command tells you the branch that you are currently in.
git branch | grep ^\*
When you don't want to type that long command every time you want to know the branch and you are using Bash, give the command a short alias, for example alias cb
, like so.
alias cb='git branch | grep ^\*'
When you are in branch master and your prompt is $
, you will get * master
as follows.
$ cb
* master
-
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review Nov 16, 2016 at 21:36
-
1
-
you should comment and describe your post for the OP, in that way it will be easier to understand your post. Nov 16, 2016 at 21:46
-
1
-
1if you use zsh you'll need to wrap the grep regex in single quotes:
git branch | grep '^\*'
– aaaarrghMar 6, 2017 at 19:49
You can permanently set up your bash output to show your git-branch name. It is very handy when you work with different branches, no need to type $ git status
all the time.
Github repo git-aware-prompt
.
Open your terminal (ctrl-alt-t) and enter the commands
mkdir ~/.bash
cd ~/.bash
git clone git://github.com/jimeh/git-aware-prompt.git
Edit your .bashrc with sudo nano ~/.bashrc
command (for Ubuntu) and add the following to the top:
export GITAWAREPROMPT=~/.bash/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"
Then paste the code
export PS1="\${debian_chroot:+(\$debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "
at the end of the same file you pasted the installation code into earlier. This will give you the colorized output:
I have a simple script called git-cbr
(current branch) which prints out the current branch name.
#!/bin/bash
git branch | grep -e "^*"
I put this script in a custom folder (~/.bin
). The folder is in $PATH
.
So now when I'm in a git repo, I just simply type git cbr
to print out the current branch name.
$ git cbr
* master
This works because the git
command takes its first argument and tries to run a script that goes by the name of git-arg1
. For instance, git branch
tries to run a script called git-branch
, etc.
Returns either branch name or SHA1 when on detached head:
git rev-parse --abbrev-ref HEAD | grep -v ^HEAD$ || git rev-parse HEAD
This is a short version of @dmaestro12's answer and without tag support.
-
8better:
git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD
– blueyedNov 10, 2017 at 11:57
.git/HEAD
, possibly in a parent directory