Is there a Git command equivalent to:
git branch | awk '/\*/ { print $2; }'
Is there a Git command equivalent to:
git branch | awk '/\*/ { print $2; }'
$ git rev-parse --abbrev-ref HEAD
master
This should work with Git 1.6.3 or newer.
HEAD
, not expected master
–
Fenian 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. –
Minutely git branch --show-current
is the most current way as of Git 2.22? –
Conventionality 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, withoutrefs/heads
.
In detached HEAD state, nothing is output.Intended both for scripting and interactive/informative use.
Unlikegit 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.
fatal: not a git repository (or any of the parent directories): .git
. How can I avoid such an annoying message? –
Ernaernald stderr
to /dev/null
, as done here. –
Quilmes 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
fatal: ref HEAD is not a symbolic ref
when running this as a part of a TravisCI build –
Mortmain git version 2.10.1.windows.1
–
Superheterodyne 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.
git rev-parse --symbolic-full-name
to git symbolic-ref
. –
Eyestalk basename
or cut
; use BR=${BR#refs/heads/}
(where BR is name of variable you saved output of git symbolic-ref HEAD
). –
Tessy git symbolic-ref --short HEAD
also –
Soong basename
? That's right, probably you need to cut
or use Jakub's suggestion if you have it in variable. –
Aspasia 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 '*'
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. –
Custommade grep '*'
is nominally a syntax error? –
Ironworks 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.) –
Custommade '*'
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"). –
Kaiserism grep '\*'
–
Ironworks git branch | grep "*" | cut -d' ' -f2
–
Soong git branch | grep -oP '(?<=^\* )(.*)$'
–
Conference git branch --no-column
. git branch | grep -oP '(?<=\* )(\S+)'
works with and without this option. –
Leonoreleonsis git branch --show-current
works for me on git version 2.24.3 (Apple Git-128)
prints just the full branch name, nothing else. –
Sixpence I guess this should be quick and can be used with a Python API:
git branch --contains HEAD
* master
I'm using
/etc/bash_completion.d/git
It came with Git and provides a prompt with branch name and argument completion.
$ 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.) –
Orlando This is not shorter, but it deals with detached branches as well:
git branch | awk -v FS=' ' '/\*/{print $NF}' | sed 's|[()]||g'
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)"
git push -u origin HEAD
works; there is no need to specify the branch name exactly and overcomplicate it. –
Horsehide 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
git branch --show-current
works for me on git version 2.24.3 (Apple Git-128)
–
Sixpence Since Git v 2.22 you can:
git branch --show-current
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>
I'm using
git name-rev HEAD
get
HEAD remotes/origin/branch
© 2022 - 2024 — McMap. All rights reserved.