Skip to content

Git refs

Resources

Refs live under .git/refs. There are two kinds of refs: heads and tags.

find .git/refs -type d
# .git/refs
# .git/refs/heads
# .git/refs/tags

Using refs via plumbing commands

From a-plumbers-guide-to-git.

Updating/Creating a ref

git update-ref refs/heads/master 092eb0b62c3eb4466ece5ff6cc9c98b1cf6da0a8
cat .git/refs/heads/master
# 092eb0b62c3eb4466ece5ff6cc9c98b1cf6da0a8

Refs under the heads directory are called branches.

Creating another branch

git log --oneline
092eb0b (HEAD -> master) new line in d.txt
3e6e455 initial commit
git update-ref refs/heads/dev 3e6e455

This creates a ref at .git/refs/heads/dev. That’s all a branch is – a pointer file.

HEAD ref

HEAD is a special ref that lives in .git/HEAD.

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

Changing HEAD:

git symbolic-ref HEAD refs/heads/dev

Using Tags via refs

git update-ref refs/tags/v0.1 dev

git log --oneline
092eb0b (HEAD -> master) new line in d.txt
3e6e455 (tag: v0.1, dev) initial commit
cat .git/refs/tags/v0.1
# 3e6e45584241f2eb95a7ba2460281a1ee01d3573

Similar to branches, a tag is just a lightweight pointer.

Overall view

Here's how refs fit in the picture with commits, trees, and blobs.

refs-commits-trees-blobs