gexicide
gexicide

Reputation: 40048

Git: Easiest way to get the branch to which HEAD points

I need a git command that returns the name of the branch to which HEAD points. I.e., when I am on master, it should return master. In detached HEAD state, it can return either nothing or something that I will not confuse with a branch name. What is the easiest command to achieve this?

(Sorry, the question seems to be rather trivial and was probably asked a gazillion of times, but searching for things like "get branch to which HEAD points" didn't give me a satisfying result.)

Upvotes: 1

Views: 465

Answers (2)

hspandher
hspandher

Reputation: 16733

Run this command to add an alias to git config.

git config --global alias.current "!git branch|grep '*'"

git current # should return the name of current branch.

Note: You can use git symbolic-ref --short HEAD too instead of grep to find the current branch as @MrTux suggested.

Upvotes: 0

MrTux
MrTux

Reputation: 33993

You are probaply looking for:

  • git symbolic-ref --short HEAD
  • git rev-parse --abbrev-ref HEAD

But it's also possible to manually read .git/HEAD

Upvotes: 3

Related Questions