mike628
mike628

Reputation: 49311

How do I get the current branch name in Git?

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

Upvotes: 4209

Views: 3575753

Answers (30)

MerickOWA
MerickOWA

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

YazanGhafir
YazanGhafir

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

Shahmir Jadoon
Shahmir Jadoon

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

Siddharth Satpathy
Siddharth Satpathy

Reputation: 3043

I would try one of the following:

  1. git symbolic-ref --short HEAD

     git symbolic-ref --short HEAD
     sid-dev
    
  2. git branch --show-current

     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.

Upvotes: 23

Ajay Vishwakarma
Ajay Vishwakarma

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

glisu
glisu

Reputation: 1116

Use git branch --contains HEAD | tail -1 it also works for "detached HEAD" state.

Upvotes: 7

Dinuka Kavinda
Dinuka Kavinda

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

roberttdev
roberttdev

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

Niket Singh
Niket Singh

Reputation: 199

git branch -a | grep -i "*"

Upvotes: 2

ehambright
ehambright

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

user3405314
user3405314

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:

  1. GIT_COMMIT - SHA of the current

  2. GIT_BRANCH - Name of the branch currently being used, e.g. "master" or "origin/foo"

  3. GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (the current SHA on first build in branch)

  4. GIT_URL - Repository remote URL

  5. GIT_URL_N - Repository remote URLs when there are more than 1 remotes, e.g. GIT_URL_1, GIT_URL_2

  6. GIT_AUTHOR_EMAIL - Committer/Author Email

  7. GIT_COMMITTER_EMAIL - Committer/Author Email

Upvotes: 8

vidur punj
vidur punj

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

ofspain
ofspain

Reputation: 51

git branch -l

This will list all your local branches with your current branch stared and printed in green enter image description here

Upvotes: 2

codemutation
codemutation

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

Jistanidiot
Jistanidiot

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

Shakeel
Shakeel

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

Francisco Baralle
Francisco Baralle

Reputation: 369

git log

This command will display the commits list with the current branch name at the top.

Upvotes: 1

casualcoder
casualcoder

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

  • work for regular commits: it just uses git's --show-current flag and if that worked we need to look no further; both locally and on the CI container
  • work for tags: when the head is detached, it will get the remote branch name instead.

Caveats:

  • This works fine as long as the tagged commits only appear on one branch. So if you're normally merging to say 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 commit
  • This requires to run git 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 work

Some more explanation:

  • I wanted to get the branch name, even when a CI build was triggered by a tag. In that case, the CircleCI 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.
  • The idea is to get all branches that include this commit (including remote ones); then remove the detached head result, get rid of all bits that aren't the actual branch name and de-duplicate

Upvotes: 4

DARK_C0D3R
DARK_C0D3R

Reputation: 2257

On Shell, You can do the following

git branch | grep '*'

Upvotes: 3

Prabhu Nandan Kumar
Prabhu Nandan Kumar

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

Rose
Rose

Reputation: 696

To get the current branch in git use,

git branch --show-current

Upvotes: 39

Wernight
Wernight

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

Max
Max

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

Kirill
Kirill

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

manisha sharma
manisha sharma

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

Joseph Lust
Joseph Lust

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

Stefaan
Stefaan

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):

  • 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)
  • 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
  • git describe --all
    • local branch: heads/master
    • remote tracking branch (in sync): heads/master (note: not remotes/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
  • 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
  • git rev-parse --abbrev-ref HEAD
    • local branch: master
    • all the other use cases: HEAD
  • git symbolic-ref --short HEAD
    • local branch: master
    • all the other use cases: fatal: ref HEAD is not a symbolic ref

(FYI this was done with git version 1.8.3.1)

Upvotes: 335

okTalk
okTalk

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

Satheesh Kumar
Satheesh Kumar

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

Lawrence Paje
Lawrence Paje

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

Related Questions