401

I tried looking for a special Git command for this, but I couldn't find one. Is there anything shorter or faster than the following?

git branch | awk '/\*/ { print $2; }'
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ollie Saunders
  • 6,841
  • 3
  • 23
  • 37
  • 1
    i think this is the fastes possible way to get current branch – Eimantas Sep 13 '09 at 15:14
  • 2
    possible duplicate of [How to get current branch name in Git?](http://stackoverflow.com/questions/6245570/how-to-get-current-branch-name-in-git) – Chandrayya G K Sep 04 '14 at 11:43
  • @ChandrayyaGK: No, because the other question is about doing it from within your IDE. (Many of the answers are for command-line usage, so it's worth a look, but it is not a proper duplicate, and those answers should perhaps be migrated here instead, if there are any which add significant value to what's already here.) – tripleee Oct 07 '14 at 07:25
  • 5
    @Torek - here's another simple task made difficult by Git. – jww Jun 29 '16 at 02:50
  • The top answer to the link @ChandrayyaGK posted is far slower than this! – Colm Bhandal Oct 01 '18 at 16:36
  • Interesting curiosity; git was created in 2005 and 14 years later it is now possible to show current branch… Mercurial was also created in 2005 and 1 year later it was possible to show current branch (from [changelog](https://www.mercurial-scm.org/wiki/WhatsNew/Archive#Version_0.9.2_-_2006-12-10) of version 0.9.2 – _new branch and branches commands for managing named branches_). – Piotr Dobrogost Dec 20 '20 at 13:20

11 Answers11

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

This should work with Git 1.6.3 or newer.

earl
  • 35,833
  • 4
  • 54
  • 57
  • 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 Sep 13 '09 at 16:59
  • 1
    JasonWoof, works for me in 1.6.4.2, need to changelog to see when exactly did it happen ;-) – Michael Krelin - hacker Sep 13 '09 at 17:08
  • 7
    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 Sep 13 '09 at 23:55
  • 1
    I would really like to understand that, too. How does this actually work? Also, it looks like --abbrev-ref doesn't return anything for any other argument. It can't just possibly have HEAD as an argument. – Setafire Jan 25 '16 at 21:53
  • 8
    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 Mar 30 '18 at 11:16
  • doesn't work on a fresh new repo? is there any workaround? – tga Aug 05 '18 at 08:11
  • @wengwengweng I wouldn't expect it to work on a fresh repo. `git branch` also shows nothing, the message shows 'master' as 'current' but I suspect that is incorrect since there are no commits, and so nothing for there to be a branch for. – smaudet May 17 '19 at 15:27
  • 1
    `git branch --show-current` is the most current way as of Git 2.22? – vbp13 Oct 13 '20 at 23:08
142

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
bfontaine
  • 14,702
  • 12
  • 64
  • 87
dieresys
  • 1,651
  • 1
  • 11
  • 8
  • 8
    Better than the accepted answer IMO, because it works on repos with no commits – Jerome Dalbert Aug 02 '17 at 06:46
  • 3
    Getting the error `fatal: ref HEAD is not a symbolic ref` when running this as a part of a TravisCI build – kmanzana Sep 21 '17 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 Oct 28 '17 at 20:43
  • Works for me: git version 2.16.2.windows.1 – Tagc Mar 26 '18 at 15:07
  • 3
    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: https://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 – Alexander Bird Oct 23 '19 at 13:03
  • This is working for me as of `git version 2.10.1.windows.1` – Domn Werner Mar 24 '20 at 20:47
107

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.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • 20
    Finally, what seems like it should have been there from the start, has been added! – dtasev Mar 22 '19 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 May 02 '21 at 10:34
  • @Olivier Good point, merci beaucoup. I have included your comment in the answer for more visibility. – VonC May 02 '21 at 10:39
27

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.

Jay Wick
  • 8,418
  • 8
  • 48
  • 69
Michael Krelin - hacker
  • 122,635
  • 21
  • 184
  • 169
14

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 '*'
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
theruss
  • 1,434
  • 10
  • 17
  • 4
    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 Oct 07 '14 at 07:26
  • @tripleee can you enlighten me about why `grep '*'` is nominally a syntax error? – JK ABC Nov 10 '16 at 03:36
  • Just like the plural *s* in English has no useful meaning on its own, you cannot say "zero or more times" in isolation without something before it. (I thought I remembered that the GNU `grep` documentation specifically mentions that a lone `*` at beginning of string is interpreted literally, i.e. as `[*]` in general regex, but I cannot find this documented now.) – tripleee Nov 10 '16 at 04: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 Mar 16 '17 at 11:50
  • 3
    @johndodo thank you for the clarification, it makes sense to me now. I usually do it by `grep '\*'` – JK ABC Mar 20 '17 at 07:10
  • 3
    can only cut the branch name with `git branch | grep "*" | cut -d' ' -f2` – Fahad Siddiqui May 15 '17 at 06:27
  • @theruss git command works for me. Also, this command works as well **git branch | grep ^*** – Loozie Nov 22 '19 at 17:25
  • for grep lovers :) `git branch | grep -oP '(?<=^\* )(.*)$'` – Marinos An Apr 15 '20 at 08:47
  • @MarinosAn This works only when using `git branch --no-column`. `git branch | grep -oP '(?<=\* )(\S+)'` works with and without this option. – Piotr Dobrogost Nov 28 '20 at 20:41
  • `git branch --show-current` works for me on `git version 2.24.3 (Apple Git-128)` prints just the full branch name, nothing else. – sudo soul Nov 30 '20 at 20:42
6

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

git branch --contains HEAD
* master
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
urvish
  • 77
  • 1
  • 4
  • 5
    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*". – RomainValeri Mar 10 '19 at 14:53
3

I'm using

/etc/bash_completion.d/git

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
iny
  • 6,729
  • 2
  • 28
  • 35
  • How can this prompt be activated? – Alex Nov 08 '12 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 Nov 01 '16 at 10:08
2

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

git branch | awk -v FS=' ' '/\*/{print $NF}' | sed 's|[()]||g'
arn0n
  • 21
  • 2
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)"

PlagTag
  • 4,445
  • 2
  • 26
  • 42
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>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
tymtam
  • 20,472
  • 3
  • 58
  • 92
0

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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Kaliyug Antagonist
  • 3,172
  • 8
  • 40
  • 83