Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I've run into 2 different ways for git to tell me the current branch:
git rev-parse --abbrev-ref HEAD
git symbolic-ref --short HEAD
Ehh.. what do both do exactly and when would they differ, if at all?
They behave differently in detached HEAD state (when there is no current named branch). In such a situation HEAD
is not a symbolic ref and git symbolic-ref
exits with an error, whereas git rev-parse --abbrev-ref
resolves HEAD
to itself.
$ git checkout HEAD^
Note: checking out 'HEAD^'.
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.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at bb2d3b1... fix layout with extra newline
$ git symbolic-ref --short HEAD
fatal: ref HEAD is not a symbolic ref
$ git rev-parse --abbrev-ref HEAD
You might also want to check out the git command name-rev. git tries its best to figure out which branch you're on, even in a detached state
$ git name-rev --name-only HEAD
master~1
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.