How to get the name of the current git branch into a variable in a shell script?

This question already has an answer here:

  • How to programmatically determine the current checked out Git branch

    19 answers

  • bash script to check if the current git branch = “x”
  • Git-commit unable to open vim
  • How do I configure git to inherit my specific $PATH environment variable?
  • Script bash that restart PlayFramework 2 instance after a push to git?
  • What's the Windows command shell equivalent of bash's `true` command?
  • GIT_EDITOR not working with macvim
  • git-ignoring files based on remote?
  • Change git remote origin
  • git push fails - what am I doing wrong?
  • How can I completely remove/destroy commits from a git repository?
  • What does it mean when git says a file “needs update”?
  • Combine or rebase an arbitrarily large number of commits
  • 6 Solutions collect form web for “How to get the name of the current git branch into a variable in a shell script?”

    The * is expanded, what you can do is use sed instead of grep and get the name of the branch immediately:

    branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')

    And a version using git symbolic-ref, as suggested by Noufal Ibrahim

    branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

    To elaborate on the expansion, (as marco already did,) the expansion happens in the echo, when you do echo $test with $test containing “* master” then the * is expanded according to the normal expansion rules. To suppress this one would have to quote the variable, as shown by marco: echo "$test". Alternatively, if you get rid of the asterisk before you echo it, all will be fine, e.g. echo ${test:2} will just echo “master”. Alternatively you could assign it anew as you already proposed:

    echo $branch

    This will echo “master”, like you wanted.

    Expanding on Noufal Ibrahim’s answer, use the --short flag with git-symbolic-ref, no need to fuss with sed.

    I’ve been using something like this in hooks and it works well:

    branch=$(git symbolic-ref --short HEAD)
    echo "**** Running post-commit hook from branch $branch"

    That outputs “**** Running post-commit hook from branch master”

    Note that git-symbolic-ref only works if you’re in a repository. Luckily .git/HEAD, as a leftover from Git’s early days, contains the same symbolic ref. If you want to get the active branch of several git repositories, without traversing directories, you could use a bash one-liner like this:

    for repo in */.git; do branch=$(cat $repo/HEAD); echo ${repo%/.git} :  ${branch##*/}; done

    Which outputs something like:

    repo1 : master  
    repo2 : dev  
    repo3 : issue12

    If you want to go further, the full ref contained in .git/HEAD is also a relative path to a file containing the SHA-1 hash of the branch’s last commit.

    I would use the git-symbolic-ref command in the git core. If you say git-symbolic-ref HEAD, you will get the name of the current branch.

    I use this
    git describe --contains --all HEAD
    in my git helper scripts


    branchname=$(git describe --contains --all HEAD)
    git pull --rebase origin $branchname

    I have that in a file called gpull in ~/scripts

    The problem relies on:

    echo $test

    In fact the variable test contains a wildcard which is expanded by the shell. To avoid that just protect $test with double quotes:

    echo "$test"

    disable subshell glob expansion,

    test=$(set -f; git branch)
    Git Baby is a git and github fan, let's start git clone.