Linux and Git commands · Lesson 22 of 23

Inside .git: blobs, trees, commits, refs and HEAD

Open the hidden .git folder and see that Git is a simple content-addressed store of four object types plus a few pointer files.

6 min readFree

On this page
  1. Why this matters
  2. The layout of .git
  3. The object database
  4. Refs: names for commits
  5. The index and packing
  6. What this explains
  7. Try it

Everything Git knows about your project lives in one hidden folder, .git. It looks mysterious, but it is built from a small number of simple ideas. Looking inside once makes the earlier lessons click: why branches are cheap, why commits have hashes, why a deleted branch can be recovered, and why a commit is a snapshot.

Why this matters#

You will rarely touch .git by hand. But when something odd happens, such as a detached HEAD, a corrupted repository, or a deploy that behaves differently from your laptop, knowing what is really stored lets you reason about it instead of guessing. It is also a satisfying lesson, because the whole thing fits in your head.

Every command below was run in a fresh practice repository with two commits. The commit hashes shown are the ones we got; yours will differ, except for the hash of identical content, which is the same everywhere.

The layout of .git#

A newly created repository contains this:

Shell
git init
ls .git
Text
HEAD
branches
config
description
hooks
info
objects
refs

The parts that matter for this lesson:

  • objects/ is the database that stores all your content and history.
  • refs/ holds pointers (branches and tags) to commits.
  • HEAD says what you currently have checked out.
  • config holds the repository's settings, such as your remotes.
  • index, which appears after your first git add, is the staging area from lesson 13.
  • hooks/ holds optional scripts that run on events such as a commit or push.

The object database#

Git stores everything as objects, in a key-value database. The key is a hash of the content, the value is the content. This is called a content-addressable store. There are four object types, and only three matter day to day: blob, tree and commit. The fourth is the annotated tag.

Blobs: file contents#

A blob stores the contents of one file, with no name and no permissions. Create one directly with git hash-object:

Shell
echo 'hello' | git hash-object -w --stdin
find .git/objects -type f
git cat-file -t ce013625030ba8dba906f756967f9e9ca394464a
git cat-file -p ce013625030ba8dba906f756967f9e9ca394464a
Text
ce013625030ba8dba906f756967f9e9ca394464a
.git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
blob
hello

The -w flag wrote the object. Its key is a 40-character hash, and the file is stored under objects/, in a folder named after the first two characters. git cat-file -t prints the type and -p prints the content.

The hash is computed from the content with a small header. You can reproduce it yourself, which shows there is no magic:

Shell
printf 'blob 6\0hello\n' | sha1sum | cut -c1-40
Text
ce013625030ba8dba906f756967f9e9ca394464a

The header is the word blob, a space, the content's length in bytes (6 for hello plus a newline), and a null byte. Git uses SHA-1 here by default, though the format can be switched to SHA-256. Two consequences follow from this design:

  • Identical content gets the same hash, so it is stored once. We later created copy.txt with the same text as an existing file, and git ls-files -s showed the same blob id for both (ce0136...). Ten copies of a file cost the space of one.
  • Any change to the content changes the hash. That is how Git notices corruption and tampering.

Trees: directories#

A tree stores one directory: a list of names, each pointing to a blob (a file) or another tree (a subfolder), together with a file mode. After we committed a file config.txt and src/a.txt, the root tree looked like this:

Shell
git cat-file -p HEAD^{tree}
Text
100644 blob f50d34fad4e67001a493928e2c35da0a17fd7151	config.txt
040000 tree e95bc8444bcd06692c882451e807e45dfe27b5ba	src

HEAD^{tree} means "the tree of the commit HEAD points to". Mode 100644 is a normal file and 040000 is a directory. (A file marked executable would be 100755.) The name lives in the tree, not in the blob, which is why renaming a file does not create a new blob. We can look into the subfolder, and also at the whole tree at once:

Shell
git ls-tree -r HEAD
Text
100644 blob f50d34fad4e67001a493928e2c35da0a17fd7151	config.txt
100644 blob 2227cddb7f6318ea735a1c4adb52f5cd36c5783c	src/a.txt

Commits: snapshots with history#

A commit points to one root tree (the snapshot) and to its parent commit or commits, and stores the author, the committer and the message.

Shell
git cat-file -p HEAD
Text
tree 6f81f3fdc3db4842a26489bc3e18784f2e33e4f1
parent de19f5fd25d90314dd2f04ad6b35decab788f886

Second commit

(We left out the author and committer lines.) This confirms lesson 13: a commit is a snapshot, as it names a complete tree. The hash of a commit covers its tree, its parent, the author and the message. Change any of those and you get a different commit id, and since each commit includes its parent's id, changing an old commit changes every commit after it. That is why rewriting history, as in lesson 18, always produces new commits.

Now see why Git is efficient. The first commit's tree and the second commit's tree both contain config.txt, and both point to the same blob f50d34f.... Only the file we changed, src/a.txt, has a new blob. A snapshot is cheap, because unchanged files are shared.

Annotated tags#

The fourth object type is the annotated tag from lesson 20. It holds a pointer to another object, a name and a message:

Shell
git tag -a v2 -m x
git cat-file -p v2
Text
object ad326734adbaa11f311aed0287bb020c60ae5d37
type commit
tag v2

(The tagger and message lines are cut.) A lightweight tag has no object of its own, as we will see next.

Refs: names for commits#

Objects are found by hash, which nobody wants to remember. Refs are human-friendly names stored as tiny files that contain a hash. A branch is just such a file:

Shell
cat .git/refs/heads/main
git rev-parse HEAD
ls .git/refs/heads .git/refs/tags
Text
ad326734adbaa11f311aed0287bb020c60ae5d37
ad326734adbaa11f311aed0287bb020c60ae5d37
.git/refs/heads:
main

.git/refs/tags:
v1

That is the whole truth about a branch: a file with one 40-character hash in it. Committing writes a new hash into that file. Creating a branch, with git branch topic, creates another file with the same hash, which is why it is instant and free. A lightweight tag such as v1 is also a file in refs/tags/. An annotated tag's file holds the hash of the tag object instead of a commit.

HEAD is a file too. Normally it does not hold a hash; it names the current branch:

Shell
cat .git/HEAD
git switch topic
cat .git/HEAD
Text
ref: refs/heads/main
ref: refs/heads/topic

When you commit, Git creates the commit, then updates the branch file that HEAD points to. This is how "HEAD is the branch you are on" works. In a detached HEAD state, the file contains a commit hash directly:

Shell
git switch --detach HEAD~1
cat .git/HEAD
Text
de19f5fd25d9...

(The hash is shortened.) Now HEAD points at a commit with no branch name in between, so a new commit moves nothing but HEAD itself. That is why commits made there are easy to lose, as lesson 15 warned: no branch file records them, and only the reflog remembers.

The index and packing#

The staging area is a binary file, .git/index. It lists each tracked path with the blob hash and mode that will go into the next commit:

Shell
git ls-files --stage
Text
100644 f50d34fad4e67001a493928e2c35da0a17fd7151 0	config.txt
100644 ce013625030ba8dba906f756967f9e9ca394464a 0	copy.txt
100644 2227cddb7f6318ea735a1c4adb52f5cd36c5783c 0	src/a.txt

git add stores the file as a blob and records it here. git commit then builds trees from the index, creates the commit object and moves the branch file.

One loose file per object would get slow for big projects. Git periodically packs objects into compressed pack files, and git gc does that on demand. Before and after:

Shell
git count-objects -v | head -4
git gc
git count-objects -v | head -4
Text
count: 10
size: 40
in-pack: 0
packs: 0
count: 0
size: 0
in-pack: 10
packs: 1

All ten loose objects moved into one pack. The branch files can be packed too: after gc, .git/refs/heads/main no longer existed, and .git/packed-refs held the same names and hashes. Commands such as git cat-file -p HEAD work exactly as before, because Git hides this detail. You can run git fsck to check that every object is intact; it printed nothing for our repository, which means no problems.

What this explains#

  • Why commits and branches are cheap: a commit adds a few small objects, and a branch is a 41-byte file.
  • Why a clone is a complete backup: the full object database is copied, so every clone has all the history.
  • Why you can recover "lost" commits: deleting a branch removes a file, but the objects stay until garbage collection removes unreferenced ones, which takes weeks by default.
  • Why you should never commit secrets: a blob exists as long as any commit points to it, even if the file is deleted in a later commit.
  • Why deploying "the repo" to a server works with just git clone or git pull: the server gets objects and updates refs.

Do not edit files under .git by hand unless you know what you are doing, and avoid copying a .git folder around as a way to share a project. Commands like git cat-file, git ls-tree, git rev-parse and git reflog are the supported way to look.

Try it#

  1. Create a repository with two commits. Run find .git/objects -type f after each commit and count how many new objects each one created.
  2. For your latest commit run git cat-file -t HEAD, git cat-file -p HEAD, then git cat-file -p on the tree hash it shows. Follow a blob and print its content.
  3. Run cat .git/HEAD and cat .git/refs/heads/<your-branch>, and compare the second to git rev-parse HEAD.
  4. Create a new branch and check that a new file appears under .git/refs/heads/. Switch to it and see what changes in .git/HEAD.
  5. Create two files with identical content, add both, and run git ls-files -s. What do you notice about the blob ids? Then run git gc and git count-objects -v.