Sub topic 4
How Git thinks: snapshots and the three areas
This chapter explains the model behind Git's everyday commands. It assumes you can use a terminal, know what files and folders are, and have read the earlier chapters on why version control exists and how local, centralized, and distributed systems differ. You do not need Git installed yet to follow the ideas. The next chapters cover installation, and the commands shown here will make full sense once Git is on your machine.
Most confusion in a first month with Git comes from a wrong mental model, not from the commands themselves. If you picture Git as a list of file changes, commands like git add and git reset seem arbitrary. If you picture it as a store of snapshots with three places where your files can sit, they become predictable.
Snapshots, not differences
Many older version control systems store each version of a project as a list of changes. The first version is a full copy of the files. Each later version is recorded as a set of edits to apply on top of the previous one. This is called delta-based storage. To rebuild version 5, the system starts from version 1 and replays four sets of edits.
Git thinks about history differently. Each time you commit, Git records what the whole project looks like at that moment. A commit is a snapshot of every tracked file as it was when you made the commit.
Two details keep this from being wasteful:
- If a file has not changed since the last commit, Git does not store a second copy. The new snapshot points to the identical content already stored.
- Git compresses what it stores. Periodically it also packs objects together and may use deltas internally to save disk space. This is a storage optimization. It does not change the model you work with, where every commit is a complete picture of the project.
The result is that looking at any commit is cheap. Git does not rebuild a version from a chain of edits. It reads the snapshot directly. Comparing two commits, switching branches, and browsing old versions all work on complete snapshots, and most of these operations happen locally with no network access.
In the diagram, file A and file C never change, so all three snapshots point to the same stored content. Only file B gets new stored content when it changes.
What a commit contains
A commit is more than the files. Each commit records:
- A pointer to the snapshot of the project.
- The author's name and email, and the time of the commit.
- A commit message that describes the change.
- A pointer to the parent commit, or several parents for a merge commit. The first commit in a repository has no parent.
Git identifies each commit by a checksum, which is a 40-character hexadecimal string computed from the commit's contents. It is usually shown shortened, such as a1b2c3d. Because the checksum depends on the content and on the parent's checksum, you cannot change an old commit without changing its identifier and the identifiers of every commit after it. This is how Git detects corruption and why history is tamper-evident.
Newer Git versions can use SHA-256 for these checksums in place of SHA-1, but SHA-1 remains the default in most installations. For learning purposes the difference does not matter.
The three areas
Your files in a Git project can be in three main places. Knowing which place a change is in tells you what each command will do.
- Working tree. This is the set of files and folders you see and edit. Git fills it by checking out one snapshot from the repository. It is an ordinary directory, and you can use any editor on it.
- Staging area. This is a place where you assemble the next commit. It is also called the index. It lists exactly which version of each file will go into the next snapshot.
- Repository. This is the database of commits that Git keeps in a hidden
.gitdirectory at the top of your project. Committing writes a new snapshot there.
The workflow moves changes from left to right.
Why a staging area exists
Beginners often ask why Git needs a middle step. The answer is control over what goes into each commit.
Suppose you spend an hour fixing a bug and also tidying the formatting of an unrelated file. Without a staging area, one commit would capture both. With it, you stage the bug fix, commit it with a message about the bug, and then stage the formatting changes for a second commit. Each commit then describes one idea, which makes history easier to read, review, and undo.
Staging also lets you stage only part of a file. The command git add -p walks through a file's changes one chunk at a time and asks whether to include each chunk.
File states
Git classifies each file in your project into a state. These states map to the areas.
| State | Meaning | Where the current version lives |
|---|---|---|
| Untracked | Git has never been told about this file. | Working tree only |
| Modified | A tracked file has changed since the last commit, and the change is not staged. | Working tree differs from the repository |
| Staged | The current version of the file is marked for the next commit. | Working tree and staging area |
| Committed | The version is safely stored in the repository. | Repository, matching the working tree |
A tracked file is one that was in the last snapshot or that you have staged. Everything else is untracked. Files that you list in a .gitignore file are untracked and also hidden from Git's status output, which is useful for build output and secrets.
A single file can be in two states at once. If you stage a file and then edit it again, the staged version is the earlier edit and the working tree holds a newer one. Git shows the file twice in git status: once under staged changes and once under changes not staged for commit. This surprises many learners. It happens because git add stages the content as it was at that moment, not the file name.
Following a change through the areas
The commands below create a repository, add a file, and show where the file sits at each step. You will run similar commands in the first-commit chapter, so for now focus on what git status reports.
mkdir demo && cd demo
git init
echo "Hello, Git" > hello.txt
git status
git add hello.txt
git status
git commit -m "Add hello.txt"
git status
git log --onelineAfter git init, the .git directory exists, and the repository is empty. After you create hello.txt, the first git status lists it under untracked files. It exists only in the working tree.
After git add hello.txt, the second git status lists the file under changes to be committed. Its content is now in the staging area. Git has also already stored the file's content in its object database at this point, though no commit refers to it yet.
After git commit, the repository holds a snapshot, and the third git status reports a clean working tree. All three areas now agree.
Now edit the file again:
echo "A second line" >> hello.txt
git status
git diff
git add hello.txt
git diff --stagedThe first git status shows hello.txt as modified but not staged. git diff with no options compares the working tree to the staging area, so it shows the new line. After git add, git diff would print nothing, because the working tree and staging area match. git diff --staged compares the staging area to the last commit, so it now shows the new line.
The pair of diff commands is a good way to check your understanding:
| Command | Compares |
|---|---|
| `git diff` | Working tree against staging area |
| `git diff --staged` | Staging area against the last commit |
| `git diff HEAD` | Working tree against the last commit |
What lives in the .git directory
The .git directory is the repository. If you copy it, you copy the full history. If you delete it, the project folder becomes an ordinary directory again, and all history is gone. Do not delete it unless you mean to.
Some of its contents are worth recognizing:
objects/holds the stored content: file contents, directory listings, and commits, all addressed by checksum.refs/holds named pointers such as branches and tags. A branch is just a small file containing the checksum of one commit.HEADis a file that says which branch, or which commit, you currently have checked out.indexis the staging area, stored as a binary file.configholds settings for this repository only. The configuration chapter covers this.
You rarely edit these files by hand. Seeing them helps explain why Git is fast and why most operations are local: it is reading and writing files on your own disk.
Common errors and misconceptions
"Git saves my files when I save them in my editor." It does not. Saving writes to the working tree only. Nothing enters history until you stage and commit. Work that was never committed has no history to recover from, so commit often enough that you never rely on the working tree alone.
"I committed, so my change is in the commit." A commit contains the staged content, not whatever is in the working tree. If you edit a file after git add and then commit, the commit holds the earlier staged version. Run git status before committing to confirm nothing is left unstaged by accident.
"A commit is a diff." The git show command displays a commit as a diff against its parent because that is a readable way to present it. Underneath, the commit refers to a full snapshot. The distinction matters when you reason about operations such as cherry-picking or reverting, where Git computes differences on demand.
"Empty folders are tracked." Git tracks file contents, not directories as such. A directory with no files in it is not recorded. The common workaround is to place an empty placeholder file, often named .gitkeep, inside it.
"git add only adds new files." It stages the current content of whatever paths you give it, whether the file is new, modified, or deleted. The name refers to adding content to the next commit.
"Staged work is safe." It is safer than unstaged work, because the content is stored in Git's object database. But the staging area is not history. Until you commit, the snapshot is not recorded, and there is no commit to return to.
Putting the model to use
With the model in place, each of the common commands has one clear job:
| Command | Effect on the areas |
|---|---|
| `git add <path>` | Copies the current content from the working tree into the staging area. |
| `git commit` | Turns the staging area into a new snapshot in the repository. |
| `git restore --staged <path>` | Resets the staging area entry for that path to the last commit. The working tree is untouched. |
| `git restore <path>` | Overwrites the working tree file with the staged version. |
| `git switch <branch>` | Moves to another branch and updates the staging area and working tree to match its snapshot. |
| `git status` | Reports differences between the three areas. |
Because each command touches specific areas, you can predict its effect before running it. This is also why Git feels safe once you know it. Anything that has been committed is very hard to lose, while anything that exists only in the working tree is the part at risk.
Summary of the model
- Git stores history as a series of snapshots, one per commit, and shares unchanged content between them.
- Each commit has a checksum, a parent pointer, an author, a time, and a message.
- Changes move from the working tree to the staging area with
git add, and from the staging area to the repository withgit commit. - The staging area lets you build commits that each express one idea.
git statusandgit diffshow the differences between the areas, and a file can appear in more than one state at once.
Exercise
You run echo "v1" > notes.txt, then git add notes.txt, then echo "v2" > notes.txt, then git commit -m "Add notes". Which content does the commit hold, and what does git status show for notes.txt afterward?
Show answer
The commit holds "v1", because the commit uses the content that was in the staging area when git add ran. Afterward git status shows notes.txt as modified and not staged, since the working tree now holds "v2", which differs from the committed version. To record "v2" you would run git add notes.txt and commit again.
Exercise
Which of the three areas does each of these commands compare: git diff, and git diff --staged? Which area holds the data that the next git commit will record?
Show answer
git diff compares the working tree against the staging area. git diff --staged compares the staging area against the last commit in the repository. The staging area holds the data that the next git commit records.
Exercise
Explain why storing every commit as a snapshot does not use as much disk space as copying the whole project each time.
Show answer
Git shares content between snapshots. A file that has not changed is stored once, and each snapshot that includes it points to the same stored content. Git also compresses objects and packs them together. Only new or changed content adds to the repository's size.