Git Internals Overview
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 commands | Plumbing commands | |
|---|---|---|
| For | People | Scripts and other commands |
| Examples | add, commit, log, merge, switch | hash-object, cat-file, ls-tree, update-index, write-tree, commit-tree, update-ref, rev-parse |
| Output | Friendly, may change between versions | Stable, meant to be parsed |
| Does | A whole task | One 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.
The .git Folder
"Inside the .git Folder" introduced it. Here is what each part is for, after one commit:
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 .gitCOMMIT_EDITMSG
HEAD
config
description
hooks/
index
info/
logs/
objects/
refs/| Entry | Holds |
|---|---|
HEAD | Which branch you are on (or a commit, when detached) |
config | This repository's settings, such as remotes and branches |
description | A name for the repository, used only by the old gitweb tool |
hooks/ | Scripts Git can run at certain moments (see the lessons on hooks) |
index | The 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_EDITMSG | The 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:
| Object | Stores | Points to |
|---|---|---|
| blob | The content of one file version (no name, no permissions) | Nothing |
| tree | One folder: names, permissions and the objects of its files and subfolders | Blobs and trees |
| commit | A snapshot (one tree), parents, author, committer, message | One tree, and parent commits |
| tag | An annotated tag: name, tagger, message | Usually 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:
find .git/objects -type f | sort.git/objects/2d/e12a7bc775374fc0d44b3171f096fd4a1fc80a
.git/objects/6b/651a00635bf65b657f4411ae05477b3d3af84e
.git/objects/c2/d44c59023fef48017eb9c7ffa24fed5b3e13a6Each 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:
for object in $(find .git/objects -type f | sed "s#.git/objects/##; s#/##"); do
echo "$object $(git cat-file -t $object)"
done6b651a00635bf65b657f4411ae05477b3d3af84e commit
c2d44c59023fef48017eb9c7ffa24fed5b3e13a6 tree
2de12a7bc775374fc0d44b3171f096fd4a1fc80a blobExplanation:
git add fees.txtcompressed the file's content into a blob and recorded it in the index, under the namefees.txt.git committurned the index into a tree object, the folder listing, and created a commit object pointing to that tree, with the author, date and message.- Then it wrote the commit's ID into
.git/refs/heads/main, the reference for the branch.
cat .git/HEAD
cat .git/refs/heads/main
git cat-file -p HEADref: 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 feeHEAD 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
| Property | Thanks to |
|---|---|
| Integrity | An object's ID is the hash of its content, so corrupted or tampered data no longer matches its name. git fsck checks this. |
| No duplicates | Identical files in different folders or commits share one blob. |
| Cheap branches | A branch is one small file naming a commit. |
| Fast comparisons | Two trees with the same ID are identical, so Git skips whole folders when comparing snapshots. |
| Safe history | A 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:
git hash-object fees.txt
git rev-parse HEAD:fees.txt2de12a7bc775374fc0d44b3171f096fd4a1fc80a
2de12a7bc775374fc0d44b3171f096fd4a1fc80agit 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
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 HEAD6
100644 blob 2de12a7bc775374fc0d44b3171f096fd4a1fc80a docs/copy.txt
100644 blob 2de12a7bc775374fc0d44b3171f096fd4a1fc80a fees.txtThree 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.
Key Takeaways
Quick reference
| Command | It 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 HEAD | List every file in a commit with its blob |
git help gitrepository-layout | Read the reference for .git |
Was this tutorial helpful?