493

Is there a Git command equivalent to:

git branch | awk '/\*/ { print $2; }'
4
  • 1
    i think this is the fastes possible way to get current branch
    – Eimantas
    Commented Sep 13, 2009 at 15:14
  • 5
    possible duplicate of How to get current branch name in Git? Commented Sep 4, 2014 at 11:43
  • 6
    @Torek - here's another simple task made difficult by Git.
    – jww
    Commented Jun 29, 2016 at 2:50
  • The top answer to the link @ChandrayyaGK posted is far slower than this! Commented Oct 1, 2018 at 16:36

12 Answers 12

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

This should work with Git 1.6.3 or newer.

10
  • 2
    Doesn't work for me either, with git-1.6.2.5. git rev-parse --abbrev-ref HEAD => --abbrev-ref 311172491a9a667f9321bdf1c4fe5e22cc6e2c08 (ie rev-parse does not accept --abbrev-ref (not in the man page either))
    – JasonWoof
    Commented Sep 13, 2009 at 16:59
  • 1
    JasonWoof, works for me in 1.6.4.2, need to changelog to see when exactly did it happen ;-) Commented Sep 13, 2009 at 17:08
  • 10
    As far as I can tell from the Git logs, this feature was merged in 2009-04-20 and was released with version 1.6.3.
    – earl
    Commented Sep 13, 2009 at 23:55
  • 13
    Note that if you are in a detached symbolic reference (might means that you are in a branch, but checked out previous commit), this command will only return HEAD, not expected master
    – unifreak
    Commented Mar 30, 2018 at 11:16
  • 13
    git branch --show-current is the most current way as of Git 2.22?
    – vbp13
    Commented Oct 13, 2020 at 23:08
238

With Git 2.22 (Q2 2019), you will have a simpler approach: git branch --show-current.

See commit 0ecb1fc (25 Oct 2018) by Daniels Umanovskis (umanovskis).
(Merged by Junio C Hamano -- gitster -- in commit 3710f60, 07 Mar 2019)

branch: introduce --show-current display option

When called with --show-current, git branch will print the current branch name and terminate.
Only the actual name gets printed, without refs/heads.
In detached HEAD state, nothing is output.

Intended both for scripting and interactive/informative use.
Unlike git branch --list, no filtering is needed to just get the branch name.

See the original discussion on the Git mailing list in Oct. 2018, and the actual patch.


Warning: as mentioned in the comments by Olivier:

This does not work in every situation!
When you are for instance in a submodule, it does not work.
'git symbolic-ref --short HEAD' always works.

8
  • 28
    Finally, what seems like it should have been there from the start, has been added!
    – dtasev
    Commented Mar 22, 2019 at 11:43
  • Warning, this does not work in every situation! When you are for instance in a submodule, it does not work. 'git symbolic-ref --short HEAD' always works
    – Olivier
    Commented May 2, 2021 at 10:34
  • @Olivier Good point, merci beaucoup. I have included your comment in the answer for more visibility.
    – VonC
    Commented May 2, 2021 at 10:39
  • @crypdick That is possible, but what is your, err... current git version?
    – VonC
    Commented Sep 4, 2021 at 17:25
  • 1
    @carlo.milanesi You need to redirect stderr to /dev/null, as done here.
    – VonC
    Commented Feb 16, 2023 at 11:40
169

In Git 1.8.1 you can use the git symbolic-ref command with the "--short" option:

$ git symbolic-ref HEAD
refs/heads/develop
$ git symbolic-ref --short HEAD
develop
8
  • 10
    Better than the accepted answer IMO, because it works on repos with no commits Commented Aug 2, 2017 at 6:46
  • 5
    Getting the error fatal: ref HEAD is not a symbolic ref when running this as a part of a TravisCI build
    – kmanzana
    Commented Sep 21, 2017 at 16:50
  • did not seem to work in GIT 1.9.1 ``` git version 1.9.1 fatal: ref HEAD is not a symbolic ref ```
    – Richard
    Commented Oct 28, 2017 at 20:43
  • Works for me: git version 2.16.2.windows.1
    – Tagc
    Commented Mar 26, 2018 at 15:07
  • 4
    For those getting "symbolic ref" error: it's probably because you technically don't have a branch checked out, and are in a 'detached' state: stackoverflow.com/questions/10228760/fix-a-git-detached-head . So, if you need the command to exit successfully in detached head state, use the "rev-parse" command in the other answer Commented Oct 23, 2019 at 13:03
31

You may be interested in the output of

git symbolic-ref HEAD

In particular, depending on your needs and layout you may wish to do

basename $(git symbolic-ref HEAD)

or

git symbolic-ref HEAD | cut -d/ -f3-

and then again there is the .git/HEAD file which may also be of interest for you.

6
  • 2
    You can shorten git rev-parse --symbolic-full-name to git symbolic-ref.
    – earl
    Commented Sep 13, 2009 at 15:36
  • 2
    You don't need to use basename or cut; use BR=${BR#refs/heads/} (where BR is name of variable you saved output of git symbolic-ref HEAD). Commented Sep 13, 2009 at 15:48
  • Jakub, of course not, provided you have the output in variable. Commented Sep 13, 2009 at 17:06
  • 8
    can do git symbolic-ref --short HEAD also Commented May 15, 2017 at 6:28
  • 5
    This will break if you have slashes in your branch names ("task/foo", "feature/bar"). A bunch of my buildscripts started failing when colleagues decided that slashes were cool...
    – vacri
    Commented Mar 22, 2018 at 5:24
19

From what I can tell, there is no way to natively show just the current branch in Git, so I have been using:

git branch | grep '*'
10
  • 5
    While that works with GNU coreutils, grep '*' is nominally a syntax error. You probably want git branch | sed -n 's/^\* //p' anyway. Or actually, what the OP posted in the first place, which amounts to the same thing.
    – tripleee
    Commented Oct 7, 2014 at 7:26
  • 1
    @tripleee can you enlighten me about why grep '*' is nominally a syntax error?
    – JK ABC
    Commented Nov 10, 2016 at 3:36
  • 1
    @JKABC: what @tripleee meant is that '*' is a regular expression and as such it is invalid. You probably want to use '[*]' (that is, character * instead of operator "zero or more times").
    – johndodo
    Commented Mar 16, 2017 at 11:50
  • 3
    @johndodo thank you for the clarification, it makes sense to me now. I usually do it by grep '\*'
    – JK ABC
    Commented Mar 20, 2017 at 7:10
  • 3
    can only cut the branch name with git branch | grep "*" | cut -d' ' -f2 Commented May 15, 2017 at 6:27
7

I guess this should be quick and can be used with a Python API:

git branch --contains HEAD
* master
1
  • 6
    This does not output the current branch. It outputs the list of branches which happen to point at the commit HEAD points to. And yes, it can overlap, but this could lead to misunderstandings. Create a new branch from where you are and retry your line : two branches. Question asks for "just the current branch". Commented Mar 10, 2019 at 14:53
3

This is not shorter, but it deals with detached branches as well:

git branch | awk -v FS=' ' '/\*/{print $NF}' | sed 's|[()]||g'
3

I'm using

/etc/bash_completion.d/git

It came with Git and provides a prompt with branch name and argument completion.

2
  • How can this prompt be activated?
    – Alex
    Commented Nov 8, 2012 at 11:04
  • In ubuntu, $ source /etc//bash_completion.d/git-prompt File may be named differently on different systems. (Note: source keyword is the same as just . (dot) in bash.)
    – michael
    Commented Nov 1, 2016 at 10:08
2

For those liking aliases: Put the following to your .zshrc so you get easier git command flow:

alias gpsu="git push --set-upstream origin $(git symbolic-ref --short HEAD)"

2
  • Exactly what I was looking for. Beautiful Commented Apr 5, 2021 at 19:34
  • 1
    git push -u origin HEAD works; there is no need to specify the branch name exactly and overcomplicate it. Commented Sep 9, 2023 at 20:14
1

Someone might find this (git show-branch --current) helpful. The current branch is shown with a * mark.

host-78-65-229-191:idp-mobileid user-1$ git show-branch --current
! [CICD-1283-pipeline-in-shared-libraries] feat(CICD-1283): Use latest version of custom release plugin.
 * [master] Merge pull request #12 in CORES/idp-mobileid from feature/fix-schema-name to master
--
+  [CICD-1283-pipeline-in-shared-libraries] feat(CICD-1283): Use latest version of custom release plugin.
+  [CICD-1283-pipeline-in-shared-libraries^] feat(CICD-1283): Used the renamed AWS pipeline.
+  [CICD-1283-pipeline-in-shared-libraries~2] feat(CICD-1283): Point to feature branches of shared libraries.
-- [master] Merge pull request #12 in CORES/idp-mobileid from feature/fix-schema-name to master
1
  • 2
    git branch --show-current works for me on git version 2.24.3 (Apple Git-128)
    – sudo soul
    Commented Nov 30, 2020 at 20:41
0

For completeness, echo $(__git_ps1), on Linux at least, should give you the name of the current branch surrounded by parentheses.

This may be useful is some scenarios as it is not a Git command (while depending on Git), notably for setting up your Bash command prompt to display the current branch.

For example:

/mnt/c/git/ConsoleApp1 (test-branch)> echo $(__git_ps1)
(test-branch)
/mnt/c/git/ConsoleApp1 (test-branch)> git checkout master
Switched to branch 'master'
/mnt/c/git/ConsoleApp1 (master)> echo $(__git_ps1)
(master)
/mnt/c/git/ConsoleApp1 (master)> cd ..
/mnt/c/git> echo $(__git_ps1)

/mnt/c/git>
1
  • 2
    the item is an alias and should be present for interactive shell scripts. its probably absent for any sort of scripts. Commented Feb 12, 2020 at 9:30
0

I'm using

git name-rev HEAD

get

HEAD remotes/origin/branch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.