Reputation: 49311
How do I get the name of the current branch in Git?
Upvotes: 4209
Views: 3575753
Reputation: 7592
If you're using a detached HEAD but there is a branch pointing to the same commit as HEAD, here was the only method I found that would work to find a branch and properly ignore tags.
git for-each-ref --format="%(refname:short)" --points-at HEAD refs/heads
The refs/heads
filter is to only list local branches, and excludes remote branches as well as any tags.
Upvotes: 0
Reputation: 864
Short answer:
git branch --show-current
To put it in a variable in a bash script for example:
current_branch=$(git branch --show-current);
or in Powershell script:
$currentBranch = $(git branch --show-current)
Upvotes: 24
Reputation: 660
You can get the current git branch name by using the following command:
git branch --show-current
It will display the current branch. If you want to copy the current branch name, you can use the following command:
git branch --show-current | pbcopy
Upvotes: 4
Reputation: 3043
I would try one of the following:
git symbolic-ref --short HEAD
sid-dev
git branch --show-current
sid-dev
git name-rev --name-only HEAD
HEAD sid-dev
git symbolic-ref --short HEAD
displays the short symbolic reference to the current branch’s HEAD. This is the current branch name.
git branch --show-current
is also a simple and efficient way to print the current branch name.
git name-rev --name-only HEAD
gives the symbolic name for HEAD
revision of the current branch
In the above examples, sid-dev
is the name of my branch.
Upvotes: 23
Reputation: 327
git branch --show-current
it worked for me all the time and it shows only the current branch
git branch
will show the list of the branches with *your_branch_name
Hope it will help.
Upvotes: 1
Reputation: 1116
Use git branch --contains HEAD | tail -1
it also works for "detached HEAD" state.
Upvotes: 7
Reputation: 75
Tried above answers but there is always 'detached HEAD' issue that comes with several commands. This ones works for me so far. It will correctly output the branch name even if the branch name contains /
in the name
git describe --all --exact-match | sed 's/heads\///'
Upvotes: 0
Reputation: 46503
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
Upvotes: 4441
Reputation: 1593
I wanted a single line that I could parse in a Windows CMD shell (most of the answers here use unix commands). Most of the answers also had problems with sparse checkouts and detached heads.
git branch
gives a list of branches that are available in the working copy, with an * in front of the checked out one. This is good for a quick look but not for parsing.
git rev-parse --abbrev-ref HEAD
just showed HEAD as the branch when using it on a detached HEAD checkout. Similarly when using
git symbolic-ref HEAD
I got fatal: ref HEAD is not a symbolic ref when using a detached head that was from a tag.
git describe --contains --all HEAD
seems to work most of the time and was close to what I want, but with some repos just returned a blank line.
git status
works except it lists all differences in the repo which makes it a bit difficult to parse.
I ended up settling on the following;
git status |findstr/n ^^|findstr "^[1]: ^[0]:"
This gives output like the following;
1:HEAD detached at 2.30.20
or
1:On branch master
Upvotes: 6
Reputation: 89
if you run in Jenkins, you can use GIT_BRANCH variable as appears here: https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin
The git plugin sets several environment variables you can use in your scripts:
GIT_COMMIT
- SHA of the current
GIT_BRANCH
- Name of the branch currently being used, e.g. "master"
or "origin/foo"
GIT_PREVIOUS_COMMIT
- SHA of the previous built commit from the same
branch (the current SHA on first build in branch)
GIT_URL
- Repository remote URL
GIT_URL_N
- Repository remote URLs when there are more than 1
remotes, e.g. GIT_URL_1, GIT_URL_2
GIT_AUTHOR_EMAIL
- Committer/Author Email
GIT_COMMITTER_EMAIL
- Committer/Author Email
Upvotes: 8
Reputation: 5861
write the following command in terminal :
git branch | grep \*
or
git branch --show-current
or on Git 2.22 and above:
git branch --show
Upvotes: 92
Reputation: 51
git branch -l
This will list all your local branches with your current branch stared and printed in green
Upvotes: 2
Reputation: 178
Just the name of the current branch if on a branch, but if detached then print the current commit id:
git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD
The first part will return an error if detached, and the second part will always print the current commit id.
Upvotes: 4
Reputation: 56666
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
Upvotes: 5591
Reputation: 2015
A simple hack could be
git branch|grep "*"
Output:
* <current branch>
EDIT:
Another way to know current branch
git status|head -1
On branch <current branch name>
Upvotes: 9
Reputation: 369
git log
This command will display the commits list with the current branch name at the top.
Upvotes: 1
Reputation: 490
I've been battling with CircleCI and git tags and this is what I ended up with:
if [[ -n $(git branch --show-current) ]]; then
git branch --show-current
else
git branch -a --contains $(git rev-parse --short HEAD) \
| sed '/HEAD/d' \
| sed 's/remotes\/origin\///g' \
| sed 's/\*//g' | sed 's/ *//g' \
| awk '!_[$0]++'
fi
While it's a bit ugly, it does
--show-current
flag and if that worked we need to look no further; both locally and on the CI containerCaveats:
dev
and from there via staging
to production
, the original commit from dev
will appear on all three branches, thereby breaking this code. But as long as you only ever tag the merge/PR commitgit fetch -a
to be run beforehand since the CI will only check out the default branch and the tagged commit so if the correct branch is neither of these it won't workSome more explanation:
pipeline.git.branch
variable is not set nor can I easily get the branch name from git as the head is detached (as pointed out in many of the other questions). This means grepping the current branch (using the *
prefix) doesn't work either as on the CI, the current branch will be the detached head one.Upvotes: 4
Reputation: 1255
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.
Upvotes: 20
Reputation: 37600
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
Upvotes: 601
Reputation: 13328
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.
Upvotes: 282
Reputation: 8046
In case your CI server does not have environment variable with branch name and you have a dockerized build without git
binary inside of container, you can just use:
cat .git/HEAD | awk -F '/' '{print $NF}'
Upvotes: 4
Reputation: 75
You can also see name of current branch in your .git directory of current project.
type command in terminal: open .git/HEAD output file contains the name of current branch ref: refs/heads/{current_working_branch}
Upvotes: 0
Reputation: 19975
You can do this with a single grep instruction, using Perl mode and \K
to reset the match buffer, so you get only the branch name.
$ git branch | grep -oP "^\*\s+\K\S+$"
master
Upvotes: 1
Reputation: 4916
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)
Upvotes: 335
Reputation: 1212
I know this has been answered already, but in the most recent version of Git the command git branch
opens a list of your branches in some kind of prompt that you have to quit out of. Which annoys me to no end!
Here's my fix: Open your bash profile and type this in:
#!/bin/bash
git() {
if [[ $@ == "branch" ]] then
command git branch -a | grep -v 'remotes'
else
command git "$@"
fi
}
Now open Terminal and test it out by typing the following commands in a git repo:
source ~/.zshrc
git branch
And voila! A list of your local branches is printed out in your terminal.
The code you're writing to your bashrc file overwrites the default function for git branch
and replaces it with a much longer command that lists all local branches via the -a
argument. Then we grep
out the extra not needed business and print it out. If you exclude the grep
command you'll still get the annoying prompt. If you're not familiar with writing bash commands checkout this explanation: About .bash_profile, .bashrc, and where should alias be written in?
Upvotes: -1
Reputation: 797
git status
will also give the branch name along with changes.
e.g.
>git status
On branch master // <-- branch name here
.....
Upvotes: 17
Reputation: 339
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
Upvotes: 29