How to get the current branch name in Git?
Olivia Zamora
I'm from a Subversion background and, when I had a branch, I knew what I was working on with "These working files point to this branch".
But with Git I'm not sure when I am editing a file in NetBeans or Notepad++, whether it's tied to the master or another branch.
There's no problem with git in bash, it tells me what I'm doing.
46 Answers
12 NextTo display the current branch you're on, without the other branches listed, you can do the following:
git rev-parse --abbrev-ref HEADReference:
27git branchshould 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 HEADor with Git 2.22 and above:
git branch --show-current 17 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 HEADOn Git v1.7+ you can also do:
git rev-parse --abbrev-ref HEADBoth should give the same branch name if you're on a branch. If you're on a detached head answers differ.
8Note:
On an earlier client, this seems to work:
git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"– Darien 26. Mar 2014
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)
6As of version 2.22 of git you could just use:
git branch --show-currentAs per man page:
8Print the name of the current branch. In detached HEAD state, nothing is printed.
One more alternative:
git name-rev --name-only HEAD 9 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} 1 You can just type in command line (console) on Linux, in the repository directory:
$ git statusand 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.
git symbolic-ref -q --short HEADI 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.
3write the following command in terminal:
git branch | grep \*or
git branch --show-currentor
git branch --show To get the current branch in git use,
git branch --show-current 3 git branch | grep -e "^*" | cut -d' ' -f 2will show only the branch name
2git 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 3 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!
A less noisy version for git status would do the trick
git status -bsunoIt prints out
## branch-name 1 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/HEADfile is a symbolic link (a very rare case, from the ancient history of Git), it usesgit symbolic-ref HEAD 2>/dev/nullElse, it reads
.git/HEADfile. 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/HEADis 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.
4Sorry 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
#!/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 -bit 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).
0Over 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 statusworks, but only If there are any local changes
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-devgit branch --show-current
>>> sid-dev3.> git name-rev –name-only HEAD
git name-rev –name-only HEAD
>>> HEAD sid-devNotes:
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
HEADrevision of the current branch
4.> In the above examples,
sid-devis the name of my branch.
There is various way to check the current branch of Git but I prefer :
git branch --showEven git branch also shows the current branch name along with all existing branch name list.
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.
1What about this?
{ git symbolic-ref HEAD 2> /dev/null || git rev-parse --short HEAD 2> /dev/null } | sed "s#refs/heads/##" 4 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 6 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://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
* masterThis 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 HEADThis is a short version of @dmaestro12's answer and without tag support.
1 12 Next