How Does Git Actually Work?

Commits are snapshots, not diffs β€” and why that changes everything about how you use Git.

How It Works

Git Internals: 6 Core Concepts

What actually happens when you run git add, git commit, and git branch.

πŸ“‹
1. Snapshot, Not Diff
Tap to flip
Most people think Git stores changes (diffs) between versions. It does not. Each commit stores the entire state of every file at that moment. Unchanged files are linked to their previous blob via a pointer. This is why Git can jump to any commit instantly. [1]
πŸ›₯️
2. Three Object Types
Tap to flip
Git stores everything in 3 object types: Blobs (file contents β€” one per unique version), Trees (directory snapshots β€” maps file names to blobs), and Commits (point to a tree + parent commit + metadata). All stored in .git/objects/.
πŸ”’
3. Content-Addressed by SHA-1
Tap to flip
Every object is identified by its SHA-1 hash β€” a 40-character hex string derived from the object’s content. Same content = same hash. This makes Git a content-addressable filesystem: you look up content by its hash, not its name. [2]
🏷️
4. Branches Are Just Pointers
Tap to flip
A branch is a movable pointer to a commit hash. When you commit on main, Git creates a new commit and moves the main pointer forward. Creating a branch = creating a new pointer (takes milliseconds). There is no "copying" of files.
πŸ—οΈ
5. The Staging Area (Index)
Tap to flip
When you git add, Git creates blobs for each file and stores them in the index β€” a temporary snapshot of what the next commit will contain. git commit then packages that index into a commit. Add + commit = two-step snapshot saving.
πŸ”„
6. Immutability & No Editing
Tap to flip
Git objects are immutable β€” they are never modified once created. A commit’s hash depends on its tree, parent hash, and message. Changing anything produces a completely new hash. There is no "edit" button in Git. git commit --amend creates a new commit.
Deep Dive

Git Concepts Explained

The mechanisms that make Git different from every other VCS.

πŸ”
HEAD: Where You Are Now
Tap to flip
HEAD is a special pointer that tells Git where you are. Normally it points to a branch name (HEAD β†’ main). In detached HEAD state, it points directly to a commit hash. Used for inspecting old commits without creating a branch.
πŸ”„
Rebase: Rewriting Pointers
Tap to flip
git rebase does not modify existing commits. It creates new commits with the same content but different parents, then moves the branch pointer. This is why rebasing is called "rewriting history" β€” the old commits still exist, but the branch no longer points to them.
πŸ“Š
Git Is Efficient (Packing)
Tap to flip
Storing every version of every file sounds wasteful. Git periodically packs objects into packfiles using delta compression β€” storing the differences between version internally. The snapshot model works at the logical level; storage is optimised under the hood.
⚠️
Merge Creates New History
Tap to flip
When you merge two branches, Git creates a merge commit with two parents. This preserves the full history tree. Fast-forward merges (no new commit) happen when one branch is directly ahead of the other β€” the pointer just moves forward.
πŸ›‘οΈ
The .git Folder
Tap to flip
Everything Git knows about your repository is in the .git/ directory. It contains objects/ (all blobs, trees, commits), refs/ (branches, tags), HEAD, and config. Deleting .git/ removes all version history but leaves working files intact.
🌐
Remote Tracking Branches
Tap to flip
Your Git also stores remote branch references (origin/main). These are local copies of what the remote had last time you fetched. git fetch updates them without changing your local branches. git pull = fetch + merge.
Quiz

Git Knowledge Check

6 statements. TRUE or FALSE?

About how Git stores data:

Git stores changes (diffs) between file versions.

About branch operations:

Creating a new branch in Git takes approximately the same amount of time as copying all project files.

About Git objects:

Three types of Git objects are blobs (file contents), trees (directories), and commits (snapshots).

About commit immutability:

When you run git commit --amend, Git edits the previous commit in place.

About HEAD:

HEAD is a pointer that tells Git which commit or branch you are currently on.

About storage efficiency:

Git would be extremely inefficient storing every version of every file as a full snapshot.

Mastery Level

Where Are You on the Git Learning Curve?

From basic commits to understanding internals. Slide to see each level.

30
050100
Slide to assess
Master Git

7 Tips to Level Up Your Git Skills

Practical commands and mental models that make Git click.

Think in snapshots, not diffs
Every commit is a complete picture of your project. Branches are just labels on snapshots. This mental model makes everything else make sense.
Use interactive rebase for clean history
Before merging a feature branch, run git rebase -i HEAD~N to squash, reorder, and clean up commits. Your teammates will thank you.
Master git log --graph
See the full commit tree: git log --oneline --graph --all --decorate. It visualises branches, merges, and where HEAD is.
Use reflog β€” Git’s safety net
Git reflog records every HEAD movement for 90 days. Accidentally reset or deleted something? git reflog shows you where everything went.
Stash before switching context
git stash saves your working directory without committing. git stash pop restores it. Perfect for switching branches mid-task.
git bisect finds the bug
When a bug appears, git bisect does binary search through your commit history to find exactly which commit introduced it.
Do not fear merge conflicts
Conflicts happen when two branches touch the same lines. Resolve them in a text editor, then git add + git commit. Practice makes them less scary.