How Git works: snapshots, commits, branches and the three areas
The mental model that makes every Git command make sense: snapshots instead of differences, the working folder, staging area and repository, what a commit really contains, and why a branch is only a pointer.
Key ideas
How it works
1. Snapshots, not differences
Most people imagine Git stores a list of changes. It actually stores snapshots: each commit points to a full picture of the project. Files that did not change are not copied again; Git simply points to the same stored content. That is why switching branches is fast and why history is reliable.
Every piece of stored content gets an ID (a hash) calculated from the content itself, like 5e9b349. If even one character changes, the ID changes, so history cannot be quietly edited.
2. The three areas
| Area | What it is | Commands that move things |
|---|---|---|
| Working tree | Your files on disk | edit files; git restore to undo edits |
| Staging area | Changes chosen for the next commit | git add to stage; git restore --staged to unstage |
| Repository | Saved commits in .git | git commit to save; git log to view |
Staging lets you commit only part of your work: fix two bugs, then save them as two clean commits.
3. Look inside a real commit
These commands show what Git actually stored for the first commit of a small notes project. A commit points to a tree (the folder), the tree points to blobs (file contents), and the branch file simply holds the latest commit's ID.
$ git cat-file -t HEAD
commit
$ git cat-file -p HEAD
tree ab39980f441eabb3f33aa3ba8c6846a18da78113
author Asha Learner <[email protected]> 1793594280 +0530
committer Asha Learner <[email protected]> 1793594280 +0530
Add README with the course title
$ git cat-file -p HEAD^{tree}
100644 blob c4302146ff40c2cbd6dabd9dec8498f32cdb086a README.md
$ git cat-file -p HEAD:README.md
# My study notes
$ cat .git/HEAD
ref: refs/heads/main
$ cat .git/refs/heads/main
5e9b349eb22b7a59f77c79d033ec8e6a0a593b654. Branches are just pointers
mainis a file in .git that contains one commit ID. A new commit on main moves that pointer forward.- Creating a branch creates a new pointer to the same commit. It costs almost nothing, so make branches freely.
- HEAD is Git's "you are here". Normally it points to a branch (
ref: refs/heads/main). - Merging joins two lines of history; when one branch is simply ahead, Git just moves the pointer (a fast-forward).
5. Where GitHub fits
Your computer has the complete repository. GitHub keeps another copy (a remote, usually called origin) so you can back up, share and collaborate. You push commits up, and fetch or pull other people's commits down. Pull requests, reviews, issues and Actions are GitHub features built around that shared copy.
Common mistakes and fixes
| Mistake | Fix |
|---|---|
| Thinking GitHub and Git are the same | Git runs on your computer and works offline; GitHub hosts copies and adds collaboration tools. |
| Being afraid of branches | They are just pointers; create one for every change. |
| Committing everything at the end of the week | Commit small, logical steps with clear messages. |
Practice
- Draw the three areas on paper and write which command moves a change between each.
- In a test repository run git cat-file -p HEAD and find the tree, author and message.
- Explain to a friend why a branch costs almost nothing to create.
Quick check
Does Git store differences or snapshots?
Snapshots: each commit points to a full picture of the project; unchanged files are reused, not copied.
What does the staging area hold?
The changes you have chosen to include in the next commit.
What is inside .git/refs/heads/main?
The ID (hash) of the latest commit on main.
What is HEAD?
Git's pointer to where you are, normally the current branch.