Lesson 18 — Git Internals: Objects, Trees, Blobs, Commits and Packfiles¶
1. Learning objectives¶
By the end of this lesson, you should be able to:
- Explain Git's content-addressable object storage.
- Identify the four main Git object types.
- Inspect blobs, trees and commits.
- Understand how a commit represents a repository snapshot.
- Explain how branches and tags reference commits.
- Understand Git's object database and
.gitdirectory. - Inspect loose objects and packed objects.
- Use Git plumbing commands for low-level inspection.
- Explain what Git does when you commit, branch, merge and clone.
Part 1 — Understand Git's internal architecture¶
2. The Git object model¶
Git stores repository data as objects.
There are four primary object types:
| Object | Purpose |
|---|---|
| Blob | Stores file content |
| Tree | Stores directory structure and references to files or subdirectories |
| Commit | Stores a snapshot reference, parent commit(s), author and commit metadata |
| Tag | Stores annotated tag metadata and references another object |
A simplified representation:
A commit points to a tree representing the root directory of that snapshot.
A tree can reference:
- Blobs representing files.
- Other trees representing directories.
A commit can also reference one or more parent commits.
2.1 Blob objects¶
A blob stores file content.
For example, imagine this file:
README.md
Git stores the content as a blob.
Important:
A blob does not store the filename or directory path. It stores the file's content.
The filename and its relationship to directories are stored in tree objects.
This means two files with identical content can reference the same blob object.
2.2 Tree objects¶
A tree represents a directory.
For example:
The root tree contains references to:
README.mdblob.LICENSEblob.docs/tree.
The docs/ tree contains references to its own file blobs.
Trees preserve filenames, file modes, and references to the corresponding objects.
2.3 Commit objects¶
A commit records a snapshot and its history metadata.
A simplified commit object looks like this:
tree 8a4f...
parent 7b2c...
author Developer <developer@example.com> 1790000000 +0000
committer Developer <developer@example.com> 1790000000 +0000
docs: add git internals lesson
The fields mean:
| Field | Meaning |
|---|---|
| tree | Root tree of this snapshot |
| parent | Previous commit |
| author | Person who originally created the change |
| committer | Person who created this commit object |
| Message | Commit message |
A root commit has no parent.
A normal commit usually has one parent.
A merge commit normally has two or more parents.
2.4 Tag objects¶
Git supports two important types of tags:
Lightweight tag¶
A lightweight tag is a reference directly pointing to an object, commonly a commit.
Annotated tag¶
An annotated tag is a separate Git object containing:
- Tag name.
- Tagger.
- Date.
- Message.
- Target object reference.
For releases, annotated tags are generally preferable because they contain metadata.
Example:
Part 2 — Understand the .git directory¶
3. Inspect the repository structure¶
From your cloned repository, execute:
Inspect the Git directory:
On Windows PowerShell:
You should see directories and files similar to:
.git/
├── HEAD
├── config
├── description
├── hooks/
├── index
├── info/
├── objects/
├── refs/
└── logs/
The exact structure can vary depending on the repository and Git version.
Important locations¶
| Path | Purpose |
|---|---|
.git/HEAD |
Identifies the current branch or detached commit |
.git/config |
Repository-specific configuration |
.git/index |
Staging area information |
.git/objects/ |
Git object database |
.git/refs/ |
References such as branches and tags |
.git/logs/ |
Reference update history, including reflogs |
.git/hooks/ |
Local Git hook scripts |
Do not manually modify internal files during this lesson. Use Git commands to inspect and manipulate repository data.
3.1 Inspect HEAD¶
Run:
On Windows PowerShell:
A typical result:
This means HEAD points to the main branch reference.
Inspect the current commit:
Inspect the current branch:
Inspect all references:
You will see references to local branches and tags.
Part 3 — Understand content-addressable storage¶
4. How Git identifies objects¶
Git identifies objects using a cryptographic hash of their type, size and content.
Conceptually, Git hashes a structure similar to:
The hash identifies the resulting object.
Git repositories commonly use SHA-1 object IDs, although SHA-256 repositories are also supported.
Do not assume every repository uses a 40-character SHA-1 object ID. Check the repository's object format:
Typical results:
or:
Why content-addressable storage matters¶
Git can:
- Identify objects by their content.
- Reuse identical content.
- Detect unexpected object corruption.
- Reference objects consistently throughout repository history.
If file content changes, its blob object ID will normally change.
A filename change without a content change does not require a new blob. The tree structure changes to record the new filename.
Part 4 — Create a Git internals laboratory¶
5. Create a separate laboratory repository¶
Do not perform destructive experiments in your main training repository.
Create a disposable repository:
Configure a local identity for this laboratory:
Create a directory:
Create files:
README.md
docs/first-file.md
Stage and commit:
Inspect the result:
You now have a repository with at least one commit, a root tree and multiple blobs.
Part 5 — Inspect Git objects¶
6. Discover object IDs¶
Run:
You should see information similar to:
tree <tree-object-id>
author Git Training Developer <git-training@example.com> ...
committer Git Training Developer <git-training@example.com> ...
chore: initialize git internals lab
The tree ID identifies the root tree of the commit.
Inspect the object type:
Expected:
Inspect its size:
This reports the uncompressed object content size.
6.1 Inspect the root tree¶
Run:
You should see entries similar to:
The exact IDs will differ.
Inspect the nested directory:
This displays the entries inside docs.
Understand file modes¶
Common Git tree modes include:
| Mode | Meaning |
|---|---|
100644 |
Regular non-executable file |
100755 |
Executable file |
120000 |
Symbolic link |
040000 |
Directory tree |
160000 |
Git submodule reference |
6.2 Inspect a blob¶
Copy the blob ID for README.md from the previous command.
Then run:
Expected:
Read the blob:
Inspect its size:
Notice that the blob contains the file content, but not its filename.
6.3 Inspect a commit's full contents¶
Run:
You can inspect a previous commit with:
For a commit with multiple parents, inspect its parent IDs with:
Part 6 — Create and inspect blobs manually¶
7. Use git hash-object¶
Create a file:
manual-blob.txt
Calculate its Git object ID without storing it:
This calculates the blob hash but does not write the object into the object database.
Now store it:
The -w option writes the object into Git's object database.
Inspect the object:
Experiment: identical file content¶
Create another file:
manual-blob-copy.txt
Run:
Compare the result with the first blob ID.
The IDs should match because the file contents are identical.
This demonstrates Git's content-addressable storage.
Experiment: change the content¶
Modify the second file:
Run:
The object ID should now be different.
Part 7 — Understand the index and staging area¶
8. The Git index¶
The index is Git's staging area.
When you execute:
Git prepares the file content as an object and updates the index to record what should enter the next commit.
Inspect the index:
Example:
The index contains information such as:
- File path.
- File mode.
- Blob object ID.
- Stage number.
For ordinary staged files, the stage number is usually 0.
During unresolved merge conflicts, Git can use stages 1, 2 and 3 to represent the common ancestor, current side and incoming side.
Inspect staged changes:
Inspect unstaged changes:
Inspect both working tree and index status:
Exercise¶
- Modify
README.md. - Run
git diff. - Stage the file.
- Run
git diff --cached. - Inspect
git ls-files --stage. - Commit the change.
- Inspect the new commit using
git cat-file -p HEAD.
Record what changed in the index and what changed in the commit.
Part 8 — Understand commits and snapshots¶
9. A commit is a snapshot, not a patch¶
A common misunderstanding is that Git commits simply store differences between files.
Git's commit model is based on snapshots.
A commit points to a tree representing the complete tracked repository state at that point in history.
Git uses object reuse and storage optimizations to avoid storing every file's full content repeatedly.
For example:
Commit A
|
v
Tree A
|
+---- README blob A
|
+---- config blob A
Commit B
|
v
Tree B
|
+---- README blob A (reused)
|
+---- config blob B (changed)
The unchanged README can reuse the same blob object.
The changed configuration file receives a different blob object.
9.1 Inspect commit history¶
Run:
Inspect commit metadata:
Inspect the tree referenced by a commit:
Inspect the tree referenced by the previous commit:
Inspect the difference between two snapshots:
The diff is calculated from the snapshots. It is not the primary representation of the commit object.
Part 9 — Understand branches and references¶
10. Branches are references¶
A branch is not a complete copy of the repository.
A branch is a movable reference pointing to a commit.
Example:
Both branch names point to commits.
Inspect branches:
Inspect references:
Inspect the current branch's commit:
Inspect the current HEAD:
Create a branch:
Inspect its reference:
Create a commit on that branch:
echo "Branch experiment" > branch-experiment.txt
git add branch-experiment.txt
git commit -m "test: add branch experiment"
Inspect the branch reference again:
Notice that the feature branch now points to the new commit.
Switch back:
The branch reference remains available.
10.1 Inspect a reference without changing it¶
Inspect the current symbolic HEAD:
These commands help you distinguish between:
- A branch reference.
- A commit object.
- The symbolic HEAD reference.
Part 10 — Understand merge commits¶
11. Inspect a merge commit¶
Create two branches with independent commits.
First, create a base commit on main:
git switch main
echo "Base content" > merge-base.txt
git add merge-base.txt
git commit -m "test: add merge base"
Create a feature branch:
Add a feature file:
echo "Feature content" > feature-file.txt
git add feature-file.txt
git commit -m "feat: add feature file"
Switch back to main:
Add another file:
Merge the feature branch:
Inspect the latest commit:
You should see two parent lines.
Inspect its parent IDs:
Inspect the graph:
What did you learn?¶
A merge commit records multiple parent references.
The merge commit still points to a tree representing the resulting repository snapshot.
Part 11 — Loose objects and packfiles¶
12. What are loose objects?¶
When Git creates objects, they may initially be stored as individual compressed files inside:
These are called loose objects.
Their storage path is based on their object ID.
For example, an object ID:
may be stored at:
The first two characters form the directory name.
The remaining characters form the filename.
Do not manually edit these files.
12.1 Inspect object storage¶
Run:
Example output:
The values depend on your repository.
Important fields:
| Field | Meaning |
|---|---|
| count | Number of loose objects |
| size | Approximate loose-object storage in KiB |
| in-pack | Number of packed objects |
| packs | Number of packfiles |
| size-pack | Approximate packed storage in KiB |
12.2 What are packfiles?¶
As repositories grow, storing every object separately becomes inefficient.
Git can combine objects into packfiles.
A packfile can contain:
- Blobs.
- Trees.
- Commits.
- Tags.
Git can also use delta compression to represent related object contents efficiently.
A packfile is commonly accompanied by an index file.
Typical paths:
The .pack file stores packed objects.
The .idx file provides an index to locate objects inside the packfile.
12.3 Inspect packfiles¶
First, inspect current object statistics:
Run Git garbage collection in the disposable laboratory:
Inspect statistics again:
Inspect the pack directory:
On PowerShell:
Depending on the repository's size and configuration, Git may not create a packfile during this experiment. A small repository may not have enough objects to demonstrate meaningful packing.
You can also use:
If no .idx file exists, this command has nothing to inspect. Do not treat that as a repository error.
The output can include object IDs, types, sizes and delta information.
12.4 Why packfiles matter¶
Packfiles help Git:
- Reduce repository storage.
- Transfer repository data efficiently.
- Optimize object storage.
- Avoid storing repeated content unnecessarily.
Git commonly uses packed objects when communicating with remote repositories.
Part 12 — Git plumbing versus porcelain¶
13. Understand the distinction¶
Git commands are often grouped into two categories.
Porcelain commands¶
These are user-facing commands designed for everyday workflows.
Examples:
Plumbing commands¶
These expose lower-level Git functionality.
Examples:
git cat-file
git hash-object
git ls-tree
git ls-files
git update-index
git update-ref
git rev-parse
git write-tree
git commit-tree
You do not need to use plumbing commands every day.
However, they are useful for:
- Debugging.
- Automation.
- Git tooling.
- Repository analysis.
- Understanding Git's internal behavior.
13.1 Important plumbing commands¶
| Command | Purpose |
|---|---|
git hash-object |
Calculate or write an object |
git cat-file |
Inspect object type, size and content |
git ls-tree |
Inspect tree entries |
git ls-files |
Inspect index entries |
git rev-parse |
Resolve Git references and object IDs |
git show-ref |
List references |
git write-tree |
Write the current index as a tree object |
git commit-tree |
Create a commit object from a tree |
git update-ref |
Update a reference |
git verify-pack |
Inspect and verify packfile indexes |
Use these commands carefully. Some plumbing commands can directly modify Git's internal references or object database.
Part 13 — Advanced laboratory: create a tree and commit¶
14. Create a tree from the index¶
This experiment uses Git plumbing to create a tree object.
First, ensure the working tree is clean:
Create a new file:
plumbing-example.txt
Stage it:
Write the index as a tree:
Git prints a tree object ID.
Inspect it:
You should see:
The tree contains the entries represented by the current index.
14.1 Create a commit object using commit-tree¶
This is an advanced experiment. It creates a commit object without using the normal git commit workflow.
Capture the current tree ID:
Create a commit:
Git prints a new commit object ID.
Inspect it:
Notice:
- The commit references the supplied tree.
- The
-p HEADargument specifies the parent commit. - The message is stored inside the commit object.
Important: git commit-tree creates the commit object but does not automatically move your current branch to it.
For this exercise, do not update main or another important reference manually. Inspect the object and leave it unreachable after the experiment.
An object that is not reachable from a reference may eventually be eligible for garbage collection.
Part 14 — Understand object reachability¶
15. What does reachable mean?¶
An object is reachable when Git can find it by following references and object relationships.
For example:
The branch points to a commit.
The commit points to a tree.
The tree points to blobs and other trees.
All these objects are reachable through the branch reference.
Other references include:
- Other branches.
- Tags.
- Remote-tracking branches.
- References maintained by Git operations.
Reflogs can also temporarily keep otherwise-unreferenced commits available.
15.1 Why reachability matters¶
Consider this situation:
- You create a commit.
- You reset a branch to an older commit.
- The new commit is no longer referenced by that branch.
- You may still recover it through the reflog.
- Eventually, Git may remove unreachable objects during garbage collection.
This connects directly to Lesson 17 — Git Reflog and Repository Recovery.
15.2 Inspect unreachable objects¶
In your disposable laboratory, you can inspect objects that are not reachable from the normal references:
For a broader object-database check:
git fsck can report unreachable or dangling objects. Such reports are not automatically evidence of corruption.
Do not attempt to manually delete objects to clean up the repository.
Part 15 — How Git uses these internals in everyday operations¶
16. What happens when you run git add?¶
A simplified process:
- Git reads the working-tree file.
- Git calculates the blob object ID.
- Git stores the blob object if necessary.
- Git updates the index with the file path and blob reference.
16.1 What happens when you run git commit?¶
A simplified process:
- Git reads the index.
- Git writes a tree representing the staged snapshot.
- Git creates a commit object.
- The commit references the tree and its parent commit.
- Git updates the current branch reference to the new commit.
16.2 What happens when you create a branch?¶
Git creates a reference pointing to an existing commit.
It does not duplicate the entire repository.
16.3 What happens when you merge?¶
Git determines the relevant history and computes the resulting repository state.
Depending on the situation, it may:
- Fast-forward a branch reference.
- Create a merge commit.
- Stop for conflict resolution.
A merge commit references the resulting tree and its parent commits.
16.4 What happens when you clone?¶
At a high level, Git:
- Establishes the remote repository connection.
- Negotiates which objects are needed.
- Receives repository objects, commonly through packfile transfer.
- Stores the objects locally.
- Sets up remote-tracking references and local repository configuration.
- Checks out the selected branch when applicable.
The exact transfer process depends on the protocol, server and clone options.
Part 16 — Practical exercises¶
17. Exercise 1: Identify object types¶
Use your laboratory repository.
Run:
Then inspect a blob ID from:
Run:
Expected object types:
| Object | Expected type |
|---|---|
| HEAD | commit |
| HEAD tree | tree |
| File object | blob |
Deliverable: Create a Markdown file explaining the three object types and include the command outputs.
17.1 Exercise 2: Compare identical content¶
- Create two files with identical content.
- Calculate both object IDs using
git hash-object. - Compare the IDs.
- Change one file.
- Calculate its object ID again.
Deliverable: Explain why the IDs matched initially and changed after editing.
17.2 Exercise 3: Inspect a commit¶
Run:
Deliverable: Explain:
- Which tree the commit references.
- Whether it has a parent.
- How the parent relationship represents history.
17.3 Exercise 4: Inspect the index¶
- Modify
README.md. - Run
git diff. - Stage the file.
- Run
git diff --cached. - Run
git ls-files --stage. - Commit the modification.
Deliverable: Explain the difference between the working tree, index and committed snapshot.
17.4 Exercise 5: Inspect merge internals¶
- Create a feature branch.
- Create a commit on the feature branch.
- Create another commit on
main. - Merge the feature branch using
--no-ff. - Inspect the merge commit.
Use:
Deliverable: Explain why the merge commit has multiple parents.
17.5 Exercise 6: Inspect packfiles¶
Run:
Inspect:
Deliverable: Explain:
- What loose objects are.
- What packfiles are.
- Why Git uses packfiles.
- What delta compression means.
If the repository is too small to generate a packfile, document that result rather than forcing a cleanup or manipulating internal files.
Part 17 — Repository training deliverables¶
18. Add the lesson to your main training repository¶
Return to your main repository:
Create the following file:
Copy this lesson into that file.
Create an additional exercise report:
Suggested report structure:
## Lesson 18 — Git Internals Lab Report
## 1. Environment
- Operating system:
- Git version:
- Object format:
- Repository:
## 2. Object inspection
### Commit object
Command:
```bash
git cat-file -p HEAD
Findings:
Tree object¶
Command:
Findings:
Blob object¶
Command:
Findings:
3. Content-addressable storage¶
- First file object ID:
- Identical file object ID:
- Modified file object ID:
Explanation:
4. Index inspection¶
Commands used:
Findings:
5. Merge commit inspection¶
- Merge commit ID:
- First parent:
- Second parent:
- Tree ID:
Explanation:
6. Packfile inspection¶
Commands used:
Findings:
7. Lessons learned¶
1. 2. 3.
8. Questions¶
- What is the difference between a blob and a tree?
- Why does a commit reference a tree?
- Why can two files share the same blob?
- Why are branches references rather than copies?
- What is the purpose of a packfile?
Part 18 — Git workflow for the lesson¶
19. Create your branch¶
In your main training repository:
git switch main
git pull origin main
git switch -c docs/add-git-internals-lesson
````
Create:
```text
docs/github/module-01-code/lesson-18-git-internals.md
docs/github/module-01-code/exercises/lesson-18-git-internals-lab.md
Complete the lab report with your own command outputs and explanations.
19.1 Commit your changes¶
Check your files:
Stage:
git add docs/github/module-01-code/lesson-18-git-internals.md
git add docs/github/module-01-code/exercises/lesson-18-git-internals-lab.md
Review staged changes:
Commit:
Push:
19.2 Create a pull request¶
Suggested PR title:
Suggested PR description:
## Summary
Adds Lesson 18 to the GitHub Deep Dive training.
## Contents
- Git object model
- Blobs, trees, commits and tags
- Content-addressable storage
- Git index and staging area
- Git plumbing commands
- Loose objects and packfiles
- Object reachability
- Practical Git internals laboratory
## Exercises
- [ ] Identify Git object types
- [ ] Compare identical blob content
- [ ] Inspect commit and tree objects
- [ ] Inspect the Git index
- [ ] Inspect a merge commit
- [ ] Inspect object storage and packfiles
## Verification
- [ ] Markdown reviewed
- [ ] Commands tested in disposable repository
- [ ] No destructive experiments performed in the training repository
Merge the PR after reviewing it.
Part 19 — Create an annotated tag and GitHub Release¶
20. Tag the lesson¶
After the PR is merged, update your local main:
Create an annotated tag:
Push the tag:
Verify:
20.1 Create a GitHub Release¶
Open your repository on GitHub.
- Navigate to Releases.
- Select Draft a new release.
- Choose the tag
module-01-lesson-18. - Set the release title:
- Add these release notes:
## Lesson 18 — Git Internals
## Topics covered
- Git's four primary object types
- Blob, tree, commit and tag objects
- Content-addressable storage
- The Git index
- Commit snapshots and parent relationships
- Branch references
- Merge commits
- Loose objects and packfiles
- Git plumbing and porcelain commands
- Object reachability and garbage collection
## Practical work
- Created a separate Git internals laboratory
- Inspected object types and object contents
- Compared identical blob contents
- Inspected the index and commit snapshots
- Inspected a merge commit
- Examined Git object storage
## Completion criteria
- [ ] All six practical exercises completed
- [ ] Lab report committed
- [ ] Pull request reviewed and merged
- [ ] Annotated tag created
- Publish the release.
Part 20 — Final knowledge check¶
21. Questions¶
Answer these without looking at the lesson.
- What are the four primary Git object types?
- Does a blob contain the filename?
- What does a tree object represent?
- What does a commit object reference?
- What is the difference between a commit and a tree?
- Why can two files with identical content share one blob?
- What is a branch internally?
- What is the difference between a lightweight and annotated tag?
- What is the Git index?
- What is a loose object?
- What is a packfile?
- Why does Git use delta compression?
- What is the difference between Git plumbing and porcelain?
- What does
git cat-file -pdo? - What does
git hash-object -wdo? - What does
git write-treedo? - What is the difference between a merge commit and a normal commit?
- What does object reachability mean?
- Why can an unreachable commit sometimes still be recovered?
- What happens at a high level when you execute
git commit?
21.1 Completion checklist¶
- [ ] I understand the Git object model.
- [ ] I can distinguish blobs, trees, commits and tags.
- [ ] I can inspect objects with
git cat-file. - [ ] I can inspect trees with
git ls-tree. - [ ] I understand how the index works.
- [ ] I understand why Git uses snapshots.
- [ ] I understand how branches reference commits.
- [ ] I can inspect merge commit parent relationships.
- [ ] I understand loose objects and packfiles.
- [ ] I understand the difference between plumbing and porcelain.
- [ ] I completed the six practical exercises.
- [ ] I created the lesson and lab report in the training repository.
- [ ] I opened and merged the PR.
- [ ] I created the annotated tag.
- [ ] I published the GitHub Release.
Lesson 18 complete¶
You now have a foundation for understanding Git beyond its everyday command-line interface.
Next lesson — Lesson 19: Advanced Git Configuration, Aliases, Attributes and Ignore Rules¶
The next lesson will cover repository-level and global Git configuration, configuration precedence, aliases, .gitattributes, line-ending normalization, binary files, diff and merge drivers, .gitignore, and practical configuration standards for professional development teams.