World map

 

BM: Baca dalam Bahasa Melayu

World 10: Deep Git

Refs, the HEAD file and reftable

You will learn: what a branch really is inside .git, and where Git 3.0 will keep it.

The problem

You make a branch in a huge project. It appears at once. No copying, no waiting. So what is a branch, inside .git?

Pick one answer.

Watch

cat .git/refs/heads/main prints one 40-character id. git rev-parse main prints the same id. Terminal output: $ cat .git/refs/heads/main 9e8ddf8fb3a185e7920bc21542abe85b09f07e21 $ git rev-parse main 9e8ddf8fb3a185e7920bc21542abe85b09f07e21

A ref: A name that points to a commit, like a branch or a tag. is a name for a commit. Branch main is a tiny file in .git/refs/heads. Inside is one commit id.

Try it

.git/HEAD says where you are. Read it. Then detach HEAD, and read it again.

c02eb601a02b3e9e8ddf8

Predict

You make a new commit on main. What changes in .git/refs/heads/main?

Pick one answer.

Try it

Thousands of tiny ref files get slow. git pack-refs --all puts them in one file: packed-refs: One file in .git that holds many refs together, instead of one small file each.. Pack them, then read it.

Check

reftable: A newer, faster way to store refs in a few compact tables. Git 3.0 plans it for new repos. is a newer way. Refs live in a few compact tables. Which format fits each fact?

Drag a card into a box. Or tap the card, then tap the box.

      Check

      Your repo uses reftable. Which command still shows the commit id of main?

      Pick one answer.

      You did it!

      You can now read refs and the HEAD file, and know reftable.

      Go deeper (optional)

      git show-ref lists every ref with its id. Want reftable today? git init --ref-format=reftable makes a new repo with it. The setting init.defaultRefFormat does it for every new repo. In scripts, ask Git (rev-parse, show-ref). Don’t read .git files.

      git show-ref lists refs/heads/main and refs/tags/v1.0, each with its id. Terminal output: $ git show-ref 9e8ddf8fb3a185e7920bc21542abe85b09f07e21 refs/heads/main 283233da77bacbc7cda8523d5950f99a305cc3f0 refs/tags/v1.0