How to programmatically determine the current checked out Git branch

GitBashShell

Git Problem Overview


In a Unix or GNU scripting environment (e.g. a Linux distro, Cygwin, OSX), what is the best way to determine which Git branch is currently checked out in a working directory?

One use of this technique would be automatically labeling a release (like svnversion would do with Subversion).

Please also see my related question: https://stackoverflow.com/questions/1593188/how-to-programmatically-determine-whether-the-git-checkout-is-a-tag-and-if-so-wh">How to programmatically determine whether a Git checkout is a tag, and if so what is the tag name?

Git Solutions


Solution 1 - Git

The correct solution is to take a peek at [contrib/completions/git-completion.bash][] does that for bash prompt in __git_ps1. Removing all extras like selecting how to describe detached HEAD situation, i.e. when we are on unnamed branch, it is:

branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
branch_name="(unnamed branch)"     # detached HEAD

branch_name=${branch_name##refs/heads/}

[git symbolic-ref][git-symbolic-ref] is used to extract fully qualified branch name from symbolic reference; we use it for HEAD, which is currently checked out branch.

Alternate solution could be:

branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
branch_name=${branch_name:-HEAD}

where in last line we deal with the detached HEAD situation, using simply "HEAD" to denote such situation.

[contrib/completions/git-completion.bash]: http://git.kernel.org/?p=git/git.git;a=blob;f=contrib/completion/git-completion.bash;hb=HEAD "git.kernel.org - git/git.git/blob - contrib/completion/git-completion.bash" [git-symbolic-ref]: http://www.kernel.org/pub/software/scm/git/docs/git-symbolic-ref.html "git-symbolic-ref - Read and modify symbolic refs"


Added 11-06-2013

Junio C. Hamano (git maintainer) blog post, Checking the current branch programatically, from June 10, 2013 explains whys (and hows) in more detail.

Solution 2 - Git

Does anyone see anything wrong with just asking Git to describe the branch you are on?

git rev-parse --symbolic-full-name --abbrev-ref HEAD

That can be used within $() and passed easily in Bash, Powershell, Perl, etc. It isn't fooled if you have several branches on the commit you are on, and if you currently aren't on a branch, it simply replies with "HEAD".

Alternatively, you can use

git symbolic-ref --short -q HEAD

Which will give you the same output, but it won't return anything at all if you are detached. This one is useful if you want an error when detached though, just remove the -q.

Solution 3 - Git

you can use git name-rev --name-only HEAD

Solution 4 - Git

From this answer: https://stackoverflow.com/a/1418022/605356 :

$ git rev-parse --abbrev-ref HEAD
master

Apparently works with Git 1.6.3 or newer.

Solution 5 - Git

Try with:

 git symbolic-ref --short -q HEAD

Or you try with git branch with --no-color force simple plain string the output:

 git branch  --no-color

With grep in regex mode(-E) you can check if exists the character '*':

 git branch  --no-color  | grep -E '^\*' 

The results its similar to:

* currentBranch

You can use the next options:

sed 's/\*[^a-z]*//g'
cut -d ' ' -f 2
awk '{print $2}'

for example:

 git branch  --no-color  | grep -E '^\*' | sed 's/\*[^a-z]*//g'
 git branch  --no-color  | grep -E '^\*' | sed cut -d ' ' -f 2
 git branch  --no-color  | grep -E '^\*' | awk '{print $2}'

if exists a error you cant use an default value:

  cmd || echo 'defualt value';

All into in a bash function:

function get_branch() {
      git branch --no-color | grep -E '^\*' | awk '{print $2}' \
        || echo "default_value"
      # or
      # git symbolic-ref --short -q HEAD || echo "default_value";
}

Use:

branch_name=`get_branch`;
echo $branch_name;

Solution 6 - Git

adapting the accepted answer to windows powershell:

Split-Path -Leaf (git symbolic-ref HEAD)

Solution 7 - Git

This one worked for me in the bash file.

git branch | grep '^*' | sed 's/* //'  


################bash file###################
#!/bin/bash
BRANCH=$(git branch | grep '^*' | sed 's/* //' )
echo $BRANCH

Solution 8 - Git

Here is what I do:

git branch | sed --quiet 's/* \(.*\)/\1/p'

The output would look like this:

$ git branch | sed --quiet 's/* \(.*\)/\1/p'
master
$

Solution 9 - Git

This one works for me. The --no-color part is, or can be, important if you want a plain string back.

git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'

Solution 10 - Git

Using --porcelain gives a backwards-compatible output easy to parse:

git status --branch --porcelain | grep '##' | cut -c 4-

From the documentation:

The porcelain format is similar to the short format, but is guaranteed not to change in a backwards-incompatible way between Git versions or based on user configuration. This makes it ideal for parsing by scripts.

https://git-scm.com/docs/git-status

Solution 11 - Git

I found two really simple ways to do that:

$ git status | head -1 | cut -d ' ' -f 4

and

$ git branch | grep "*" | cut -d ' ' -f 2

Solution 12 - Git

I'm trying for the simplest and most self-explanatory method here:

git status | grep "On branch" | cut -c 11-

Solution 13 - Git

Someone mentioned doing it in bash with less than three assignments... how about some messy control flow like this:

branch_name="$(b=$(git symbolic-ref -q HEAD); { [ -n "$b" ] && echo ${b##refs/heads/}; } || echo HEAD)"

Solution 14 - Git

If you're using the old NT command line, you can use the following:

@for /f "usebackq" %i in (`git symbolic-ref -q HEAD`) do @echo %~ni

To use in a batch file, you'll have to double the %'s:

@for /f "usebackq" %%i in (`git symbolic-ref -q HEAD`) do @echo %%~ni

Solution 15 - Git

Here's my solution, suitable for use in a PS1, or for automatically labeling a release

If you are checked out at a branch, you get the branch name.

If you are in a just init'd git project, you just get '@'

If you are headless, you get a nice human name relative to some branch or tag, with an '@' preceding the name.

If you are headless and not an ancestor of some branch or tag you just get the short SHA1.

function we_are_in_git_work_tree {
    git rev-parse --is-inside-work-tree &> /dev/null
}

function parse_git_branch {
    if we_are_in_git_work_tree
    then
    local BR=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD 2> /dev/null)
    if [ "$BR" == HEAD ]
    then
        local NM=$(git name-rev --name-only HEAD 2> /dev/null)
        if [ "$NM" != undefined ]
        then echo -n "@$NM"
        else git rev-parse --short HEAD 2> /dev/null
        fi
    else
        echo -n $BR
    fi
    fi
}

You can remove the if we_are_in_git_work_tree bit if you like; I just use it in another function in my PS1 which you can view in full here: https://stackoverflow.com/questions/4133904/ps1-line-with-git-current-branch-and-colors

Solution 16 - Git

Same results as accepted answer in a one-line variable assignment:

branch_name=$((git symbolic-ref HEAD 2>/dev/null || echo "(unnamed branch)")|cut -d/ -f3-)

Solution 17 - Git

That's one solution. If you add it to your .bashrc, it'll display the current branch in the console.

# git branch
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'
}
$PS1="\$(parse_git_branch)$PS1"

However it's pretty limited. But there is a great project called git sh, which is doing exactly that (and much more).

Solution 18 - Git

I found that calling git is rather slow (any of the subcommands), especially for updating the prompt. Time varies between .1 and .2 seconds within the root dir of a repo, and over .2 seconds outside a repo, on a top notch machine (raid 1, 8 gb ram, 8 hardware threads). It does run Cygwin, though.

Therefore I wrote this script for speed:

#!/usr/bin/perl

$cwd=$ENV{PWD}; #`pwd`;
chomp $cwd;

while (length $cwd)
{
        -d "$cwd/.git" and do {
                -f "$cwd/.git/HEAD" and do {
                        open IN, "<", "$cwd/.git/HEAD";
                        $_=<IN>;
                        close IN;
                        s@ref: refs/heads/@@;
                        print $_;
                };
                exit;
        };

        $cwd=~s@/[^/]*$@@;
}

May need some tweaking.

Solution 19 - Git

If you are using gradle,


    def gitHash = new ByteArrayOutputStream()    
    project.exec {
                    commandLine 'git', 'rev-parse', '--short', 'HEAD'
                    standardOutput = gitHash
                }
    
    def gitBranch = new ByteArrayOutputStream()   
    project.exec {
                    def gitCmd = "git symbolic-ref --short -q HEAD || git branch -rq --contains "+getGitHash()+" | sed -e '2,\$d'  -e 's/\\(.*\\)\\/\\(.*\\)\$/\\2/' || echo 'master'"
                    commandLine "bash", "-c", "${gitCmd}"
                    standardOutput = gitBranch
                }

Solution 20 - Git

If you are on a detached head (i.e. you've checked out a release) and have an output from git status such as

HEAD detached at v1.7.3.1

And you want the release version, we use the following command...

git status --branch | head -n1 | tr -d 'A-Za-z: '

This returns 1.7.3.1, which we replace in our parameters.yml (Symfony) with

# RevNum=`svn status -u | grep revision | tr -d 'A-Za-z: '`  # the old SVN version
RevNum=`git status --branch | head -n1 | tr -d 'A-Za-z: '` # Git (obvs)

sed -i "/^    app_version:/c\    app_version:$RevNum" app/config/parameters.yml

Hope this helps :) Obviously if you have non-numerics in your branch name, you'll need to alter the arguments to the tr command.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJasonSmithView Question on Stackoverflow
Solution 1 - GitJakub NarębskiView Answer on Stackoverflow
Solution 2 - GitMichael EricksonView Answer on Stackoverflow
Solution 3 - GitjonnyView Answer on Stackoverflow
Solution 4 - GitJohnny UtahhView Answer on Stackoverflow
Solution 5 - GitfitorecView Answer on Stackoverflow
Solution 6 - GittjbView Answer on Stackoverflow
Solution 7 - Gitmuhammed basilView Answer on Stackoverflow
Solution 8 - GitJasonSmithView Answer on Stackoverflow
Solution 9 - GitAugust LilleaasView Answer on Stackoverflow
Solution 10 - GitTony BaguetteView Answer on Stackoverflow
Solution 11 - GitsilvanskyView Answer on Stackoverflow
Solution 12 - GittonywoodeView Answer on Stackoverflow
Solution 13 - GitqneillView Answer on Stackoverflow
Solution 14 - GitPeter HartView Answer on Stackoverflow
Solution 15 - Gitpolypus74View Answer on Stackoverflow
Solution 16 - GitKim TaylorView Answer on Stackoverflow
Solution 17 - GitDamien MATHIEUView Answer on Stackoverflow
Solution 18 - GitKenneyView Answer on Stackoverflow
Solution 19 - GitravikanthView Answer on Stackoverflow
Solution 20 - GitSteve ChildsView Answer on Stackoverflow