ChatGPT解决这个技术问题 Extra ChatGPT

Show just the current branch in Git

Is there a Git command equivalent to:

git branch | awk '/\*/ { print $2; }'
i think this is the fastes possible way to get current branch
@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.)
@Torek - here's another simple task made difficult by Git.
The top answer to the link @ChandrayyaGK posted is far slower than this!

e
earl
$ git rev-parse --abbrev-ref HEAD
master

This should work with Git 1.6.3 or newer.


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, works for me in 1.6.4.2, need to changelog to see when exactly did it happen ;-)
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.
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
git branch --show-current is the most current way as of Git 2.22?
V
VonC

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.


Finally, what seems like it should have been there from the start, has been added!
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 Good point, merci beaucoup. I have included your comment in the answer for more visibility.
@crypdick That is possible, but what is your, err... current git version?
@VonC good question, but I already hotfixed a solution and spun down the cluster ¯_(ツ)_/¯
b
bfontaine

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

Better than the accepted answer IMO, because it works on repos with no commits
Getting the error fatal: ref HEAD is not a symbolic ref when running this as a part of a TravisCI build
did not seem to work in GIT 1.9.1 ``` git version 1.9.1 fatal: ref HEAD is not a symbolic ref ```
Works for me: git version 2.16.2.windows.1
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
J
Jay Wick

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.


You can shorten git rev-parse --symbolic-full-name to git symbolic-ref.
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).
Jakub, of course not, provided you have the output in variable.
can do git symbolic-ref --short HEAD also
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...
P
Peter Mortensen

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 '*'

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 can you enlighten me about why grep '*' is nominally a syntax error?
@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 thank you for the clarification, it makes sense to me now. I usually do it by grep '\*'
can only cut the branch name with git branch | grep "*" | cut -d' ' -f2
P
Peter Mortensen

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

git branch --contains HEAD
* master

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".
a
arn0n

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

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

P
Peter Mortensen

I'm using

/etc/bash_completion.d/git

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


How can this prompt be activated?
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.)
P
PlagTag

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)"


Exactly what I was looking for. Beautiful
P
Peter Mortensen

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)
P
Peter Mortensen

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>

the item is an alias and should be present for interactive shell scripts. its probably absent for any sort of scripts.