mcintyre321
mcintyre321

Reputation: 13306

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?

Upvotes: 2

Views: 150

Answers (2)

ralphtheninja
ralphtheninja

Reputation: 132978

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

Upvotes: 0

Roy Truelove
Roy Truelove

Reputation: 22446

Assuming that your git repository is always on a branch and has never checked out a specific SHA, yes.

Upvotes: 1

Related Questions