20

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?

jschreiner
  • 4,840
  • 2
  • 12
  • 16
Duilio
  • 876
  • 1
  • 10
  • 15
  • 1
    **See Also**: [How to get the current branch name in Git?](https://stackoverflow.com/q/6245570/1366033) – KyleMit Jan 02 '22 at 01:53

2 Answers2

34

If you just want the checked-out branch, use git branch --show-current.

chepner
  • 497,756
  • 71
  • 530
  • 681
10

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-name

    This 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 give HEAD as 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/master to master.

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
ErikMD
  • 13,377
  • 3
  • 35
  • 71
  • Another option is '`git symbolic-ref --short HEAD`.' – RandomDSdevel Nov 26 '20 at 17:40
  • 2
    @RandomDSdevel indeed, but that command seems less "robust" in case of a detached HEAD: [`git checkout HEAD^{commit}`](https://git-scm.com/docs/gitrevisions#Documentation/gitrevisions.txt-emltrevgtlttypegtemegemv0998commitem) `&& git symbolic-ref --short HEAD` → `fatal: ref HEAD is not a symbolic ref` – ErikMD Nov 26 '20 at 18:50
  • True, but, for the OP's use case, they naturally already know they're on a branch they've checked out. – RandomDSdevel Dec 21 '20 at 22:56
  • OK @RandomDSdevel: [its doc](https://git-scm.com/docs/git-symbolic-ref) even says: "Typically you would give `HEAD` as the 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. – ErikMD Dec 21 '20 at 23:03
  • 1
    I've just refactored my answer to also mention `git symbolic-ref` (and links to git-scm docs), thanks @RandomDSdevel for having suggested this. – ErikMD Jan 01 '21 at 19:04