简体   繁体   中英

How to get current state of HEAD in git?

For example, if I'm on a branch, I want to get the branch name. But if I'm in detached HEAD state, I want to get the commit id.

Basically, I'm looking for an interface to .git/HEAD file. I know I could just cat it, but maybe there's a command that will do that for me (so that I won't have to handle edge cases like GIT_DIR not being inside GIT_WORK_TREE etc.)

If you are using git completion https://github.com/git/git/blob/master/contrib/completion/git-completion.bash . Which is usually automatically installed.

You could call

echo $(__git_ps1 '%s')

Otherwise use.

git symbolic-ref --short HEAD 2> /dev/null || git rev-parse HEAD

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