All posts
Article · 1 min ·

Git: What is HEAD?

You see HEAD everywhere in Git. What is it?

The Pointer

HEAD is simply a file inside .git/ that points to the current branch you are working on.

cat .git/HEAD
# ref: refs/heads/main

And what is refs/heads/main? It's a file containing the SHA-1 hash of the last commit.

cat .git/refs/heads/main
# a1b2c3d4...

Detached HEAD

If you checkout a commit directly (git checkout a1b2c3), HEAD points directly to the hash, not a branch. You are in "Detached HEAD" state.
If you commit now, no branch tracks your new commit. If you switch away, the Garbage Collector will eventually eat your work.

Conclusion

Git is not a black box. It's just files and pointers. Don't fear the HEAD.

Related posts