I have a problem when working on too many branches, it takes a lot of time to find which is the current branch I am working on right now. Is there a way to list the current branch on top of the list that shows up when using the git branch command?
- 1See Also: How to get the current branch name in Git?KyleMit– KyleMit ♦2022-01-02 01:53:50 +00:00Commented Jan 2, 2022 at 1:53
2 Answers
If you just want the checked-out branch, use git branch --show-current.
2 Comments
--show-current requires Git 2.22.0+ (not available by default on, e.g., Debian 10)git branch | grep '*'If the git branch --show-current command is not available with your Git version, you could use one of these commands instead:
$ git checkout master $ git rev-parse --symbolic-full-name HEAD refs/heads/master $ git rev-parse --abbrev-ref HEAD master $ git symbolic-ref HEAD refs/heads/master $ git symbolic-ref --short HEAD master cf. the official doc:
git rev-parse --symbolic-full-nameThis is similar to --symbolic, but it omits input that are not refs (i.e. branch or tag names; or more explicitly disambiguating "heads/master" form, when you want to name the "master" branch when there is an unfortunately named tag "master"), and show them as full refnames (e.g. "refs/heads/master").
git rev-parse --abbrev-ref[=(strict|loose)]A non-ambiguous short name of the objects name. […]
git symbolic-ref <name>Given one argument, reads which branch head the given symbolic ref refers to and outputs its path, relative to the
.git/directory. Typically you would giveHEADas the<name>argument to see which branch your working tree is on.git symbolic-ref --short <name>When showing the value of as a symbolic ref, try to shorten the value, e.g. from
refs/heads/mastertomaster.
and note that the git rev-parse solution is "compatible" with detached HEAD mode, that is, if no branch is currently checked-out, and HEAD merely points to a SHA1 reference, the two git rev-parse commands considered above will just output "HEAD":
$ git checkout 56e23844e80e6d607ad5fa56dfeedcd70e97ee70 Note: checking out '56e23844e80e6d607ad5fa56dfeedcd70e97ee70'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. […] $ git rev-parse --symbolic-full-name HEAD HEAD $ git rev-parse --abbrev-ref HEAD HEAD $ git rev-parse HEAD 56e23844e80e6d607ad5fa56dfeedcd70e97ee70 5 Comments
git symbolic-ref --short HEAD.'git checkout HEAD^{commit} && git symbolic-ref --short HEAD → fatal: ref HEAD is not a symbolic refHEAD as the <name> argument to see which branch your working tree is on." So it could indeed be viewed as yet another alternative for the accepted answer, for older versions of Git that don't support git branch --show-current. BTW, feel free to propose an edit of my answer if you want.git symbolic-ref (and links to git-scm docs), thanks @RandomDSdevel for having suggested this.