Git Internals Overview

Updated · 9 min read
hardGit 2.53gitinternalsplumbingporcelainobjectsinterview

Git internals are the data structures and low-level commands that everything else in Git is built on. Underneath the friendly commands, a Git repository is a small database of objects, each one stored under a name calculated from its content, plus a set of references (branches, tags, HEAD) that point into that database. Every command you have used so far, from git add to git rebase, only creates objects and moves references. Understanding this layer explains why Git behaves the way it does, and makes unusual situations much easier to reason about.

A library is a good picture. The books on the shelves are the objects: once printed, a book never changes, and its catalog number identifies it. The catalog cards are the references: they can be rewritten to point to another book at any time. Borrowing, returning and reorganizing never change a book. They only change cards.

Porcelain and Plumbing

Git's commands come in two layers, named after a bathroom: the porcelain you see and use, and the plumbing behind the wall.

Porcelain commandsPlumbing commands
ForPeopleScripts and other commands
Examplesadd, commit, log, merge, switchhash-object, cat-file, ls-tree, update-index, write-tree, commit-tree, update-ref, rev-parse
OutputFriendly, may change between versionsStable, meant to be parsed
DoesA whole taskOne small step

Porcelain commands are made of plumbing steps. In this module, you will use the plumbing directly, and at the end, build a commit with plumbing alone.

Porcelain commands are built on plumbing

The .git Folder

"Inside the .git Folder" introduced it. Here is what each part is for, after one commit:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
ls -F .git
Output
COMMIT_EDITMSG
HEAD
config
description
hooks/
index
info/
logs/
objects/
refs/
EntryHolds
HEADWhich branch you are on (or a commit, when detached)
configThis repository's settings, such as remotes and branches
descriptionA name for the repository, used only by the old gitweb tool
hooks/Scripts Git can run at certain moments (see the lessons on hooks)
indexThe staging area, one binary file
info/exclude, the private ignore list
logs/The reflogs
objects/The object database: every file version, folder, commit and annotated tag
refs/Branches (refs/heads/), tags (refs/tags/) and remote-tracking branches (refs/remotes/)
COMMIT_EDITMSGThe message of the last commit, kept in case you need to reuse it

Other files appear while something is in progress, such as MERGE_HEAD, ORIG_HEAD or rebase-merge/, as earlier lessons showed.

The Four Kinds of Objects

Everything Git stores about your project's history is one of four object types:

ObjectStoresPoints to
blobThe content of one file version (no name, no permissions)Nothing
treeOne folder: names, permissions and the objects of its files and subfoldersBlobs and trees
commitA snapshot (one tree), parents, author, committer, messageOne tree, and parent commits
tagAn annotated tag: name, tagger, messageUsually a commit

Each object is named by a hash of its content: 40 hexadecimal characters with SHA-1, the default (Git also supports SHA-256, see "Git 3.0: What Is Planned"). The same content always gets the same name, and any change gives a different one.

What git add and git commit Really Write

The one commit above created three objects:

Terminal
find .git/objects -type f | sort
Output
.git/objects/2d/e12a7bc775374fc0d44b3171f096fd4a1fc80a
.git/objects/6b/651a00635bf65b657f4411ae05477b3d3af84e
.git/objects/c2/d44c59023fef48017eb9c7ffa24fed5b3e13a6

Each object is stored as a compressed file: the first two characters of its ID are the folder name, and the other 38 the file name. git cat-file -t tells you the type of each:

Terminal
for object in $(find .git/objects -type f | sed "s#.git/objects/##; s#/##"); do
  echo "$object $(git cat-file -t $object)"
done
Output
6b651a00635bf65b657f4411ae05477b3d3af84e commit
c2d44c59023fef48017eb9c7ffa24fed5b3e13a6 tree
2de12a7bc775374fc0d44b3171f096fd4a1fc80a blob

Explanation:

  1. git add fees.txt compressed the file's content into a blob and recorded it in the index, under the name fees.txt.
  2. git commit turned the index into a tree object, the folder listing, and created a commit object pointing to that tree, with the author, date and message.
  3. Then it wrote the commit's ID into .git/refs/heads/main, the reference for the branch.
Terminal
cat .git/HEAD
cat .git/refs/heads/main
git cat-file -p HEAD
Output
ref: refs/heads/main
6b651a00635bf65b657f4411ae05477b3d3af84e
tree c2d44c59023fef48017eb9c7ffa24fed5b3e13a6
author Navika Anamaneni <navika@example.com> 1767629160 -0600
committer Navika Anamaneni <navika@example.com> 1767629160 -0600
 
Add the term fee

HEAD names the branch, the branch names the commit, the commit names the tree. From any reference, Git can find everything by following these names.

Why This Design Matters

PropertyThanks to
IntegrityAn object's ID is the hash of its content, so corrupted or tampered data no longer matches its name. git fsck checks this.
No duplicatesIdentical files in different folders or commits share one blob.
Cheap branchesA branch is one small file naming a commit.
Fast comparisonsTwo trees with the same ID are identical, so Git skips whole folders when comparing snapshots.
Safe historyA commit's ID covers its tree and parents, so changing anything in the past changes every later ID.

The next lessons look at each part in detail: "Git Objects" covers blobs, trees, commits and tags, "The Index and Refs" covers the staging area and references, "Packfiles and Garbage Collection" covers how objects are stored compactly, and "Build a Commit by Hand" puts it all together.

Common Mistakes

Editing files in .git by hand. Plumbing commands keep everything consistent. Editing objects or the index directly can corrupt the repository.

Thinking a commit stores changes. It stores a complete snapshot (a tree). Diffs are calculated when you ask for them.

Expecting file names in blobs. Names and permissions live in trees. A blob is only content.

Thinking deleting a branch deletes commits. It deletes a reference. The objects stay until they are unreachable and old enough for garbage collection.

Parsing porcelain output in scripts. Its format can change. Use plumbing commands or --porcelain options.

Under the Hood

A loose object file is compressed with zlib. Inside, it starts with a short header, the type, a space, the size in bytes and a zero byte, followed by the content. The object's ID is the SHA-1 hash of that header and content together. Git can compute it for any content without storing anything:

Terminal
git hash-object fees.txt
git rev-parse HEAD:fees.txt
Output
2de12a7bc775374fc0d44b3171f096fd4a1fc80a
2de12a7bc775374fc0d44b3171f096fd4a1fc80a

git hash-object calculated the ID from the file's content, and git rev-parse HEAD:fees.txt looked up the blob stored in the commit: the same ID. Content alone decides the name.

Interview Corner

What are porcelain and plumbing commands?

Porcelain commands (add, commit, log) are the user-facing commands. Plumbing commands (hash-object, cat-file, write-tree, commit-tree, update-ref) are low-level building blocks with stable output, used by scripts and by the porcelain itself.

What types of objects does Git store?

Blobs (file contents), trees (folders: names, modes and object IDs), commits (a tree, parents, author, committer, message) and annotated tags (an object, tag name, tagger, message).

What is a Git object ID?

A hash (SHA-1 by default, SHA-256 optionally) of the object's type, size and content. Identical content always gets the same ID, which gives integrity checking and deduplication for free.

What happens internally when you run git commit?

Git writes a tree object from the index (plus subtrees), writes a commit object pointing to that tree and to the current commit as parent, and updates the current branch's reference to the new commit.

Practice

Challenge: the same content twice. Create a folder docs with a file copy.txt containing exactly Term 1 fee: $12,000, commit it, and count the objects again. How many new objects does the commit create?

What does Git print? Before you open the solution, think about which objects already exist: the new file has the same content as fees.txt.

Show solution
Terminal
mkdir docs
echo 'Term 1 fee: $12,000' > docs/copy.txt
git add docs
git commit --quiet -m "Add a copy in docs"
find .git/objects -type f | wc -l | tr -d " "
git ls-tree -r HEAD
Output
6
100644 blob 2de12a7bc775374fc0d44b3171f096fd4a1fc80a	docs/copy.txt
100644 blob 2de12a7bc775374fc0d44b3171f096fd4a1fc80a	fees.txt

Three new objects, not four: a tree for docs, a new top-level tree and a new commit. The file's content already existed as a blob, so both fees.txt and docs/copy.txt point to the same blob, as git ls-tree -r shows.

Checkpoint. In a practice repository, make one commit with two files, then list .git/objects and identify every object with git cat-file -t and -p. Draw the arrows: commit to tree, tree to blobs.

Quick check: Git internals

4 questions
1Which of these is a plumbing command?
2What does a blob store?
3What does git commit write to .git/refs/heads/main?
4Two files in different folders have identical content. How many blobs does Git store?

Key Takeaways

Quick reference

CommandIt is used to
git cat-file -t <id>Show an object's type
git cat-file -p <id>Show an object's content
git hash-object <file>Compute a file's blob ID
git rev-parse <name>Turn any name into an object ID
git ls-tree -r HEADList every file in a commit with its blob
git help gitrepository-layoutRead the reference for .git

Was this tutorial helpful?

Lesson 72 of 106