2

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?

1
  • 2
    What's the problem with using git executables? The git tools are specifically written to handle these sort of thing for you. Not using the supplied tools really means reimplementing them. (personally, I do not like to reinvent to wheel) Commented Feb 21, 2012 at 22:05

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.