简体   繁体   中英

How can I find out the current Git branch name from code, without using git itself? .git/HEAD file?

I am writing some jazzy code which needs to determine the current branch name programatically. I don't want to take a dependency on any git executables, I just want to look in the .git directory.

Can I just inspect the .git/HEAD file?

假设您的git仓库始终在分支上,并且从未签出特定的SHA,是的。

Yes you can use .git/HEAD file. Mine says that rel-8.0 is the current branch:

$ cat .git/HEAD
ref: refs/heads/rel-8.0

If I checkout a tag it just gives me the hash:

$ git checkout 7.5
$ cat .git/HEAD
2a6a92d1a34af5cf229097cac63ae5b3ea0c3747

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