简体   繁体   中英

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. Ie, 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.)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM