Show just the current branch in Git
GitBranchGit BranchGit Problem Overview
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; }'
Git Solutions
Solution 1 - Git
$ git rev-parse --abbrev-ref HEAD
master
This should work with Git 1.6.3 or newer.
Solution 2 - Git
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
Solution 3 - Git
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.
Solution 4 - Git
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.
Solution 5 - Git
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 '*'
Solution 6 - Git
I guess this should be quick and can be used with a Python API:
git branch --contains HEAD
* master
Solution 7 - Git
I'm using
/etc/bash_completion.d/git
It came with Git and provides a prompt with branch name and argument completion.
Solution 8 - Git
This is not shorter, but it deals with detached branches as well:
git branch | awk -v FS=' ' '/\*/{print $NF}' | sed 's|[()]||g'
Solution 9 - Git
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)"
Solution 10 - Git
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
Solution 11 - Git
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>