What Is Git - How Git Stores a Commit as Commit, Tree and Blob Objects, with Real git cat-file Output

TL;DR

  • Git is a distributed version control system. Every clone holds the full history, so you can commit, branch and read the log with no server. The Git book describes it as storing snapshots of your files, not a list of file changes.
  • Underneath, Git is a content-addressable key-value store. You give it content, it returns a 40-character hash, and that hash is the key. The Git Objects chapter says the first two characters become a directory name under .git/objects and the other 38 become the file name.
  • A commit is three kinds of object: a blob (file contents, no file name), a tree (a directory listing of names pointing to blobs and sub-trees) and a commit (one tree, zero or more parents, author, committer, message).
  • Same content means same object. In my run, copy.txt and hello.txt held identical bytes and shared one 10-byte blob, 8d0e412. The blob ID is the SHA-1 of the text blob 10, a NUL byte and the content; sha1sum gave me the same ID as git hash-object.
  • A branch is a tiny text file holding one 40-character commit ID in .git/refs/heads/. Moving a branch means rewriting that one file.
  • Not your exact case? If you already know the model and want to undo things, read git reset soft, mixed and hard; if you want to start working with branches, read Git branching explained.

Table of Content

  1. What Git is and what problem it solves
  2. The lab repository
  3. The .git directory
  4. Blob, tree and commit in the object database
  5. Why the object ID is the hash of the content
  6. Identical content is stored once
  7. A branch is a pointer, and a second commit adds a parent
  8. git gc and packfiles
  9. What broke when I ran this
  10. Conclusion

1. What Git is and what problem it solves

The problem is old and boring. You have a folder of files, you change them, and a week later you need yesterday's version, or you need to know who changed line 40 of the deployment script, or two people edited the same file and one of them lost the work. Version control records every change with an author and a message so that you can go back, compare and merge.

Git does that in a distributed way. The older systems (CVS, Subversion) kept the history on one central server and your laptop held only the latest files. With Git, git clone copies the whole history to your machine. That has three consequences I care about every day: the log, diff and blame commands run offline and instantly, a commit never needs a network, and any clone is a full backup of the project. GitHub, GitLab and Bitbucket are just convenient places to put one more clone that the whole team agrees to treat as the shared one. They are not Git itself, and I covered that difference in the Git and GitHub blueprint course.

The part most tutorials skip is how Git keeps the history. Once you see that, commands that look like magic (reset, rebase, cherry-pick, reflog) become simple pointer moves. So in this post I do not explain git add and git commit as buttons. I run them in an empty folder and open the box.

Git object model: a branch points to a commit, the commit points to a tree and a parent, the tree points to blobs
The four things in the database: ref, commit, tree and blob. Dashed boxes belong to the first commit.

ThingLives inWhat it holdsNamed by
Blob.git/objectsThe bytes of one file, no name, no modeSHA-1 of blob <size>, NUL, content
Tree.git/objectsNames, modes and IDs of blobs and sub-treesSHA-1 of its own contents
Commit.git/objectsOne tree ID, parent IDs, author, committer, messageSHA-1 of its own contents
Ref (branch).git/refs/heads/<name>A commit ID, as 40 hex charactersThe file name is the branch name
HEAD.git/HEADUsually ref: refs/heads/<branch>Fixed file name

2. The lab repository

I ran everything below with git 2.43.0 on 2026-12-02, in an empty folder, with the default branch name set by -b main. To make the object IDs reproducible I set the author and committer to Rahul <r@example.com> and pinned both dates to 2026-12-02T10:00:00Z with GIT_AUTHOR_DATE and GIT_COMMITTER_DATE. A commit ID includes the author, the committer and the timestamps, so if you run this without those variables your commit and tree IDs for the commit objects will differ from mine, but the blob ID will be exactly the same, because a blob depends only on its content.

1$ git init -b main
2Initialized empty Git repository in /tmp/lab/demo/.git/

3. The .git directory

Everything Git knows about this project lives in one folder, .git, at the top of the working tree. Delete it and you have a plain folder again; copy it and you have copied the entire history. Here is what git init created, with ls -F:

1$ ls -F .git
2HEAD
3branches/
4config
5description
6hooks/
7info/
8objects/
9refs/

The layout is documented in gitrepository-layout. The three entries that matter for this post are HEAD (which branch you are on), refs/ (the branch and tag pointers) and objects/ (the database). At this point objects/ holds no object files yet.

4. Blob, tree and commit in the object database

I created one file and staged it. I did not commit yet, and I list every file under .git/objects:

1$ echo "hello git" > hello.txt
2$ git add hello.txt
3$ find .git/objects -type f
4.git/objects/8d/0e41234f24b6da002d962a26c2495ea16a425f

This is the first surprise for most people: git add already wrote an object. The file hello.txt was compressed with zlib and stored as a blob whose ID is 8d0e41234f24b6da002d962a26c2495ea16a425f. The directory is 8d and the file is the remaining 38 characters, exactly as the Git Objects chapter describes. git add is the step that puts content into the database; the staging area (the index) is only a list that maps file names to those blob IDs.

Now the commit:

1$ git commit -qm "first commit"
2$ find .git/objects -type f | sort
3.git/objects/07/ed5a7aebb914e3a02edf6d622b82d364037e3c
4.git/objects/8d/0e41234f24b6da002d962a26c2495ea16a425f
5.git/objects/b2/d644a81819d43f2f0fe51cbcb00425665b8947
6$ git log --oneline
7b2d644a first commit

Two new objects appeared: 07ed5a7 (the tree) and b2d644a (the commit). Three objects for one commit of one file. I opened each one with git cat-file, the plumbing command that prints an object. The -p flag pretty-prints it, -t prints the type and -s prints the size in bytes:

 1$ git cat-file -p HEAD
 2tree 07ed5a7aebb914e3a02edf6d622b82d364037e3c
 3author Rahul <r@example.com> 1796205600 +0000
 4committer Rahul <r@example.com> 1796205600 +0000
 5
 6first commit
 7$ git cat-file -p 07ed5a7
 8100644 blob 8d0e41234f24b6da002d962a26c2495ea16a425f	hello.txt
 9$ git cat-file -p 8d0e412
10hello git
11$ git cat-file -t HEAD
12commit
13$ git cat-file -t 07ed5a7
14tree
15$ git cat-file -t 8d0e412
16blob
17$ git cat-file -s 8d0e412
1810

Read that top to bottom and you have the whole model. The commit has no files in it. It names a tree, an author, a committer and a message. The tree is a directory listing: mode 100644 (a regular, non-executable file), type blob, the blob ID, and the file name hello.txt. The blob holds the text and nothing else, not even its own name. The file name lives in the tree, not in the blob, which is why renaming a file does not create a new blob.

The commit above has no parent line because it is the first one, the root commit. The author timestamp 1796205600 is seconds since the Unix epoch, which is 2026-12-02 10:00:00 UTC, the date I pinned.

5. Why the object ID is the hash of the content

The ID of a blob is not random and not a counter. It is computed from the content. The git hash-object command computes an ID for any input, and the format is a header, blob, a space, the size as a decimal number and a NUL byte, followed by the content. I checked it two ways on the same text:

1$ echo "hello git" | git hash-object --stdin
28d0e41234f24b6da002d962a26c2495ea16a425f
3$ printf 'blob 10\0hello git\n' | sha1sum
48d0e41234f24b6da002d962a26c2495ea16a425f  -

Both give 8d0e412..., the same ID git add created. So the ID is a fingerprint of the content. Two consequences follow, and they are the reason Git is trustworthy as a history:

  1. Anyone, anywhere, who stores the same bytes gets the same ID. Two clones can compare a single 40-character ID instead of comparing files.
  2. You cannot change content without changing its ID, and a commit includes its tree's ID and its parent's ID. Change one old file and the blob changes, then the tree, then the commit, then every later commit. That chain is what makes history tamper-evident, and it is also why "rewriting history" with rebase creates new commits rather than editing old ones, as I saw in git squash commits.

One caution about the hash itself. Git uses SHA-1 by default today. The project has documented a plan to move to SHA-256 in the hash function transition design document, which says that in late 2018 the project picked SHA-256 as the successor. For learning, and for any repository you create today with default settings, the 40-character IDs in this post are what you will see.

A few numbers worth keeping in your head, all from the runs above:

MeasurementValueWhere I saw it
SHA-1 object ID length40 hex characters (160 bits)git cat-file -p HEAD
SHA-256 object ID length64 hex characters (256 bits)hash function transition
Blob for hello git10 bytesgit cat-file -s
Second-version blob15 bytesgit verify-pack -v
Whole repository after git gc6 objects, 1.66 KiB (size-pack with -H), 457 bytes in the .pack filegit count-objects -vH, wc -c
.git folder on disk after git gc, with sample hooks176 KBdu -sk .git
.git/HEAD21 byteswc -c .git/HEAD
git cat-file -p HEAD wall time3 msshell time on this tiny repository
git log --oneline wall time4 msshell time on this tiny repository
Share of the 176 KB .git folder that is historyunder 1% (the pack is 457 bytes)du -sk and wc -c
Commit with one parent197 bytesgit verify-pack -v

6. Identical content is stored once

Now the part that makes Git's storage small. I copied the file under a new name and staged it, then listed the index with git ls-files -s (the -s flag shows mode, object ID and stage number):

1$ echo "hello git" > copy.txt
2$ git add copy.txt
3$ git ls-files -s
4100644 8d0e41234f24b6da002d962a26c2495ea16a425f 0	copy.txt
5100644 8d0e41234f24b6da002d962a26c2495ea16a425f 0	hello.txt

Two names, one blob ID. Adding copy.txt did not write a new content object, because an object with that ID already existed. This is the practical meaning of "content-addressable": Git looks up the ID, finds the object, and skips the write. A repository with a hundred identical licence files stores the licence text once, and a file you rename or move between folders costs nothing in content storage, because only the tree changes.

7. A branch is a pointer, and a second commit adds a parent

For the second commit I appended a line to hello.txt and committed with git commit -qam (-a stages changes to tracked files, so copy.txt stayed as it was in the index; I had already added it):

 1$ echo more >> hello.txt
 2$ git commit -qam "second"
 3$ git cat-file -p HEAD
 4tree aaecccfa99ee95acfd0a1f9cfd2a6963d43f2403
 5parent b2d644a81819d43f2f0fe51cbcb00425665b8947
 6author Rahul <r@example.com> 1796205600 +0000
 7committer Rahul <r@example.com> 1796205600 +0000
 8
 9second
10$ cat .git/HEAD
11ref: refs/heads/main
12$ cat .git/refs/heads/main
13dac8acb44a0b1c63c5fd416244d29a9a4eb8c680

The new commit has a parent line holding the ID of the first commit. That one line is the entire "history": a commit points back to its parent, and git log just follows those arrows. The second commit's tree, aaecccf, lists two entries. copy.txt still points at blob 8d0e412 (it did not change), and hello.txt points at a new blob, b46f845, which is the 15-byte text hello git plus the line more.

Look at the last two commands. HEAD is a text file that says ref: refs/heads/main, and refs/heads/main is a text file that contains the ID of the second commit. That is all a branch is. Creating a branch writes one new small file; switching branches rewrites HEAD; committing writes the new objects and then updates the branch file to the new commit ID. This is why branching in Git is instant regardless of project size, and it is the foundation for Git branching explained and for the three reset modes in git reset soft, mixed and hard, which are only different combinations of "move the branch pointer", "reset the index" and "reset the working tree".

8. git gc and packfiles

So far every object was a separate small file, called a loose object. That is fine for a few objects but wasteful for thousands, so Git packs them. The automatic trigger is git gc, which I ran by hand to see the effect, and the pack format is explained in the Packfiles chapter:

 1$ git gc -q
 2$ find .git/objects -type f | sort
 3.git/objects/info/commit-graph
 4.git/objects/info/packs
 5.git/objects/pack/pack-ebf9e1a98221922593f4b644ff56e7bc45b60ef1.idx
 6.git/objects/pack/pack-ebf9e1a98221922593f4b644ff56e7bc45b60ef1.pack
 7.git/objects/pack/pack-ebf9e1a98221922593f4b644ff56e7bc45b60ef1.rev
 8$ git count-objects -v | head -6
 9count: 0
10size: 0
11in-pack: 6
12packs: 1
13size-pack: 1
14prune-packable: 0
15$ git verify-pack -v .git/objects/pack/*.idx | head -6
16dac8acb44a0b1c63c5fd416244d29a9a4eb8c680 commit 197 142 12
17b2d644a81819d43f2f0fe51cbcb00425665b8947 commit 155 116 154
188d0e41234f24b6da002d962a26c2495ea16a425f blob   10 19 270
19b46f8455a3857c7fadd19a22ffcd3ec9ae08248b blob   15 24 289
20aaecccfa99ee95acfd0a1f9cfd2a6963d43f2403 tree   73 77 313
2107ed5a7aebb914e3a02edf6d622b82d364037e3c tree   37 47 390

Six objects in total for two commits: two commits, two trees, two blobs. After git gc, count (loose objects) is 0 and in-pack is 6. The loose files such as .git/objects/8d/0e41... are gone, folded into one .pack file with an .idx index. With -H the same command prints size-pack: 1.66 KiB, and the .pack file itself is 457 bytes; the rest is the index and reverse-index files. du -sk .git reported 176 KB for the whole folder, nearly all of it the sample hook scripts that git init copies in, so the history here is a tiny part of it. The numbers in the verify-pack rows are the object size, its size inside the pack, and its offset. Nothing about the model changed: git cat-file -p 8d0e412 still prints hello git. Packing is only storage.

9. What broke when I ran this

Nothing crashed, but five things surprised me and each one is a place where readers get lost.

1. Looking for the commit object and finding the blob. Before my first commit, find .git/objects -type f already listed one object. I had expected an empty folder until git commit. The object was the blob 8d0e412, written by git add. So the moment you git add a file, its content is in the database, which is worth remembering before you stage a file that holds a password.

2. Commit IDs depend on who and when. I pinned the author, committer and dates to get stable IDs, because a commit ID includes them. If you follow along and your git log --oneline shows different commit hashes but 8d0e412 for the blob, nothing is wrong: the blob depends only on its content.

3. The size was 10, not 9. hello git is 9 characters, but git cat-file -s reported 10 and my printf 'blob 10\0hello git\n' matched only with the trailing newline. echo adds a newline, and my first mental count of the header was blob 9. The hash only matches with blob 10 and the trailing newline, so one missing byte gives a completely different ID, which is exactly what content addressing means.

4. The branch file disappeared too. After git gc, .git/refs/heads/main no longer existed (wc -c said No such file or directory) because gc moves refs into one .git/packed-refs file. git log still worked. Read refs with git rev-parse main, not with cat.

5. The loose object path disappeared after git gc. After git gc, the find listing shows no .git/objects/8d/ file any more, but git cat-file -p 8d0e412 still prints the content. If you write a script that reads .git/objects/xx/yyyy directly, it will break after the first garbage collection. Use git cat-file instead of reading the folders.

10. Conclusion

Git is a database of immutable objects named by the hash of their content, with a handful of tiny text files (HEAD and the refs) saying which commit is current. A commit points to one tree and its parents, a tree points to blobs and other trees, and a blob is just bytes. Everything else you will learn in Git, from branches and merges to rebase and reset, is a way of creating those objects or moving those pointers.

Things to check when you want to see what Git is doing in your own repository:

  1. git cat-file -p HEAD prints the current commit, and git cat-file -p HEAD^{tree} prints its tree.
  2. git ls-files -s shows which blob each staged file will become.
  3. cat .git/HEAD and cat .git/refs/heads/<branch> show where the pointers are.
  4. git count-objects -v tells you how many loose objects and packs you have.

This is lesson Day 2 of the DevOps Master Course on YouTube, and the written foundation for the rest of the series is in the free Git and GitHub blueprint course.

Did this save you a debugging session? Stay in the loop -