Git Objects: Blob, Tree, Commit and Tag

Updated · 11 min read
hardGit 2.53gitobjectsblobtreecommitcat-fileinterview

A Git object is one entry in Git's object database: an immutable piece of data stored under an ID calculated from its content. There are four types: a blob holds a file's content, a tree holds a folder listing, a commit holds a snapshot with its history, and a tag holds an annotated tag. Together they form a chain of links from every commit down to every byte of every file. The plumbing commands git cat-file and git ls-tree let you read any of them.

A family photo album is a fair picture. Each photo is a blob: just the picture, no caption. Each page is a tree: it says which photos go where and labels them. The album's dated entries are commits: "this page, on this day, after that entry". And a gold sticker that says "Wedding, 2026" on one entry is a tag.

The Starting Point

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
mkdir src
echo "class FeeCalculator {}" > src/FeeCalculator.java
git add .
git commit --quiet -m "Start the project"
echo "Run the app to see the fees." >> README.md
git commit --quiet -am "Explain how to run the app"
git tag -a v1.0.0 -m "First release"
git log --oneline
Output
20c7d31 (HEAD -> main, tag: v1.0.0) Explain how to run the app
c53550f Start the project

Reading Objects: git cat-file

OptionDescription
git cat-file -t <object>It is used to show the object's type.
git cat-file -s <object>It is used to show the object's size in bytes.
git cat-file -p <object>It is used to print the object's content in a readable form.
git cat-file --batch-all-objects --batch-checkIt is used to list every object in the repository, with type and size.

<object> can be an ID or any name from "Naming Commits: HEAD, ~ and ^", such as HEAD, HEAD:README.md (a file in a commit) or HEAD^{tree} (a commit's tree).

Blob: File Content

A blob (binary large object) is the content of one version of one file, and nothing else: no name, no date, no permissions.

Terminal
git cat-file -t HEAD:README.md
git cat-file -s HEAD:README.md
git cat-file -p HEAD:README.md
Output
blob
41
Fee Tracker
Run the app to see the fees.

Explanation: HEAD:README.md names the blob that README.md points to in the newest commit. It is a blob of 41 bytes, and printing it gives the file's content. Because a blob has no name, two files with identical content, anywhere in the history, are one blob.

Tree: A Folder

A tree lists the entries of one folder. Each entry has a mode, a type, an ID and a name:

Terminal
git cat-file -p HEAD^{tree}
Output
100644 blob 9a059a2a8f40f0268c653f62c7e93ef7f01adeb5	README.md
040000 tree 13fd335f9d326bcf87515d98d736b2552046dcd7	src

Explanation: the top folder holds a blob called README.md, and a tree called src, which is itself a folder. Following the src entry leads to the next level:

Terminal
git ls-tree HEAD:src
git ls-tree -r --name-only HEAD
Output
100644 blob 2bec87b439cb8e1072c1ec91431b301e1c2e6d68	FeeCalculator.java
README.md
src/FeeCalculator.java

git ls-tree prints trees like cat-file -p, and -r walks into every subfolder, listing all files of the commit. The mode tells what each entry is:

ModeMeaning
100644A normal file
100755An executable file
120000A symbolic link (the blob holds the link target)
040000A subfolder (a tree)
160000A submodule (a commit in another repository)

Note that Git only stores "executable or not" for files, not full permissions, and it has no entry for an empty folder, which is why "Git .gitignore" needed .gitkeep.

Commit: A Snapshot with History

Terminal
git cat-file -p HEAD
Output
tree f86ec8ecf88923e4480a15bcfb137a3d7418dc33
parent c53550f3ac01145edb11424466171679a0fd540e
author Navika Anamaneni <navika@example.com> 1767629400 -0600
committer Navika Anamaneni <navika@example.com> 1767629400 -0600
 
Explain how to run the app
LineHolds
treeThe ID of the top-level tree: the whole snapshot
parentThe previous commit. The first commit has none, a merge commit has two or more
authorWho wrote the change, with a timestamp (seconds since 1970) and time zone
committerWho created this commit object, and when
(empty line)The rest is the message

A commit stores no list of changed files and no diff: it points to a complete tree, and Git compares trees when you ask for a diff. Because the parent's ID is part of the commit's content, a commit's ID depends on the whole history before it.

A merge commit simply has more than one parent line:

Terminal
git switch --quiet -c feature/fees
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the fees"
git switch --quiet main
echo "Built by Navika." >> README.md
git commit --quiet -am "Credit the author"
git merge --quiet --no-edit feature/fees
git cat-file -p HEAD | head -4
Output
tree 57828e1790a062272291c5789e9715b241ebf7af
parent cf038faf6de39d1e9f1b11fff369feea34b8e2be
parent 768669488a5a4673eb25a35ee6bda654f7b098bb
author Navika Anamaneni <navika@example.com> 1767630720 -0600

Tag: An Annotated Tag

A lightweight tag is only a reference. An annotated tag is an object of its own:

Terminal
git cat-file -t v1.0.0
git cat-file -p v1.0.0
Output
tag
object 20c7d3199eb2923567c02854a41b226ef3a5291f
type commit
tag v1.0.0
tagger Navika Anamaneni <navika@example.com> 1767629460 -0600
 
First release

It names the object it tags (object and type), the tag name, the tagger and the message. git rev-parse v1.0.0^{commit} follows it to the commit it points to.

Every name in Git leads down to blobs

Every Object in the Repository

--batch-all-objects --batch-check lists the whole database, one object per line, with its type and size in bytes:

Terminal
git cat-file --batch-all-objects --batch-check
Output
00ece55ae0e044205438ba8485fcc7f881a453c0 blob 58
0c8acc2511bfe4066d6a5c6f0f9ce50b78df6f3c tree 67
13fd335f9d326bcf87515d98d736b2552046dcd7 tree 46
20c7d3199eb2923567c02854a41b226ef3a5291f commit 249
2bec87b439cb8e1072c1ec91431b301e1c2e6d68 blob 23
2de12a7bc775374fc0d44b3171f096fd4a1fc80a blob 20
55c7bc431150e6acc8e7be8d37339c0df510cb79 tree 67
57828e1790a062272291c5789e9715b241ebf7af tree 103
768669488a5a4673eb25a35ee6bda654f7b098bb commit 235
998f42cb7d192d09ee44a058019a8bf3f2e94b92 tree 103
9a059a2a8f40f0268c653f62c7e93ef7f01adeb5 blob 41
c53550f3ac01145edb11424466171679a0fd540e commit 192
cf038faf6de39d1e9f1b11fff369feea34b8e2be commit 240
d5924b0d02d9ef4f29fef5369a7ddcaf82c830c7 blob 12
f86ec8ecf88923e4480a15bcfb137a3d7418dc33 tree 67
fa89cba4fbb08f89d7937351a9dfa37b53d74070 tag 148
fb74e12370dd0b444ebd55c37515b752aa26e8b1 commit 298

Count them by type: 5 blobs (three versions of README.md, plus FeeCalculator.java and fees.txt), 6 trees, 5 commits and 1 tag. There are fewer trees than you might expect, because unchanged folders keep the same tree ID: the src tree, 13fd335, is shared by every commit.

How an ID Is Calculated

An object's ID is the SHA-1 hash of a small header followed by the content. For a blob, the header is the word blob, a space, the size in bytes, and a zero byte. git hash-object performs that calculation for any content, with no file needed:

Terminal
echo "Fee Tracker" | git hash-object --stdin
git rev-parse HEAD~3:README.md
Output
d5924b0d02d9ef4f29fef5369a7ddcaf82c830c7
d5924b0d02d9ef4f29fef5369a7ddcaf82c830c7

Explanation: the first commit's README.md contained exactly Fee Tracker and a new line. Hashing the same text gives the same ID as the blob in that commit. --stdin reads the content from the pipe, and adding -w would also write it into the database. Trees and commits are hashed the same way, with tree or commit in the header and their own text as content.

Common Mistakes

Looking for file names in blobs. A blob holds content only. The tree that lists it gives the name.

Expecting a commit to contain a diff. It contains a snapshot (a tree ID). Diffs are computed.

Confusing HEAD:README.md with HEAD -- README.md. The colon form names the blob in that commit. The -- form is a path argument for commands like git log.

Treating a lightweight tag as an object. git cat-file -t on a lightweight tag shows commit, because the name points directly at a commit.

Expecting empty folders to be stored. Trees list files and subfolders that contain something. An empty folder has nothing to point to.

Under the Hood

Each loose object is a file under .git/objects/xx/yyyy..., compressed with zlib. Uncompressed, a tree is a binary list: for each entry, the mode in text, a space, the name, a zero byte and the 20-byte binary ID. That is why cat-file -p exists: it decodes trees into the readable form you saw. Commits and tags, by contrast, are plain text after decompression. Every link in the system is an ID, so the whole history forms a graph where each object's name proves its content: change one byte of one blob, and its ID changes, then the tree's, then the commit's, and every later commit's.

Interview Corner

What is the difference between a blob and a tree?

A blob stores the content of one file, with no name or metadata. A tree stores a folder: a list of entries, each with a mode, type, object ID and name, pointing to blobs (files) and other trees (subfolders).

What does a commit object contain?

The ID of its top-level tree, the IDs of its parent commits (none for the first commit, two or more for a merge), the author and committer with timestamps, and the message.

How is an object's ID calculated?

As the SHA-1 (or SHA-256) hash of a header (type, a space, the size in bytes and a zero byte) followed by the content. git hash-object computes it.

Why can't Git store an empty folder?

Trees only list entries that point to objects. An empty folder has no files to point to, so it doesn't appear in any tree. A placeholder file such as .gitkeep makes it non-empty.

How do you see every object in a repository?

git cat-file --batch-all-objects --batch-check lists each object's ID, type and size.

Practice

Challenge: follow the chain by hand. Starting from the v1.0.0 tag, reach the content of src/FeeCalculator.java using only git cat-file -p and the IDs it prints: tag, then commit, then tree, then the src tree, then the blob. The solution does each step with rev-parse, so you can compare.

What does Git print? Before you open the solution, decide how many steps there are, and what the last one prints.

Show solution
Terminal
commit=$(git rev-parse v1.0.0^{commit})
tree=$(git rev-parse "$commit^{tree}")
src=$(git cat-file -p "$tree" | awk '$4 == "src" {print $3}')
blob=$(git cat-file -p "$src" | awk '$4 == "FeeCalculator.java" {print $3}')
echo "tag    $(git rev-parse --short v1.0.0)"
echo "commit $(git rev-parse --short $commit)"
echo "tree   $(git rev-parse --short $tree)"
echo "src    $(git rev-parse --short $src)"
echo "blob   $(git rev-parse --short $blob)"
git cat-file -p "$blob"
Output
tag    fa89cba
commit 20c7d31
tree   f86ec8e
src    13fd335
blob   2bec87b
class FeeCalculator {}

Five objects from the tag to the content: tag, commit, top tree, src tree and blob. The last step prints the file. awk picks the ID (third field) of the entry whose name (fourth field) matches, exactly what you would do by reading the output with your eyes.

Checkpoint. In a project of your own, start from HEAD and follow cat-file -p down to one file three folders deep. Then run git cat-file --batch-all-objects --batch-check and count the objects by type.

Quick check: Git objects

4 questions
1Which object stores a file's name?
2How many parent lines does a merge commit have?
3What does mode 040000 in a tree mean?
4What does git cat-file -p HEAD^{tree} show?

Key Takeaways

Quick reference

CommandIt is used to
git cat-file -p <object>Print an object
git cat-file -t <object>Show its type
git ls-tree -r HEADList all files in a commit
git rev-parse HEAD:<path>Get the ID of a file or folder in a commit
git hash-object --stdinCompute the ID of some content
git cat-file --batch-all-objects --batch-checkList every object

Was this tutorial helpful?

Lesson 73 of 106