Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 78 ยท Getting Started

How Git Works: Snapshots and the Three Areas

Understand Git's mental model - the working tree, the staging area and the repository, commits as snapshots, branches as pointers - with real commands you can run.

Most Git confusion disappears once you hold the right picture in your head. Git is not a mysterious tool with hundreds of unrelated commands. It is a small number of ideas: three places where your files can live, commits that are snapshots, and branches that are labels pointing at commits. Every command you will ever learn moves data between those places or moves a label.

This lesson builds that picture step by step and proves each claim with a real command. You already ran your first commit in Getting Started with Git. Here we look at what happens underneath, without yet diving into the low-level "plumbing" commands (those come in Git Objects).

The three areas

Git tracks your project in three places. Learn their names, because every Git message uses them.

TEXT
  WORKING TREE            STAGING AREA (index)          REPOSITORY (.git)
  the files you           the draft of your             the saved history
  see and edit            next commit                   of commits

  +-------------+  git add   +----------------+  git commit  +----------------+
  | todo.txt    | ---------> | todo.txt       | -----------> | commit 9b3c... |
  | notes.md    |            | (as of add)    |              | commit c331... |
  +-------------+            +----------------+              | commit 40d5... |
        ^                                                    +----------------+
        |                       git restore --staged                 |
        |  <---------------------------------------------------------+
        +------------------------ git restore / git switch ----------+
  • The working tree (also called the working directory) is the normal folder on disk. You open and edit those files with any program.
  • The staging area, also called the index, is a list of what will go into the next commit. It is Git's drafting table: you place changes there deliberately.
  • The repository is the permanent database inside the .git folder. Commits live there, and once recorded they are very hard to lose.

Why have a staging area at all? Because a good commit contains one logical change. You might have edited five files while fixing two unrelated things. Staging lets you commit the fix for the first problem now and the second one separately, even when the edits are mixed together in the working tree. Lesson Staging Changes goes further.

Watching the three areas move

We can see each area with a different command. git status compares the areas for us:

  • Changes to be committed = staging area differs from the last commit.
  • Changes not staged for commit = working tree differs from the staging area.
  • Untracked files = files in the working tree that Git has never been told about.

The example below walks one file through all three areas and prints the status after every move.

Bash One file travelling through the three areas

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The short status uses two columns. The left column compares the staging area with the last commit; the right column compares the working tree with the staging area. So A means "added to the index", M means "modified, not staged", and MM (step 5) means both: one version of the change is staged and a newer one is only in the working tree. If you committed now, you would record version two, not version three, because the commit uses whatever is in the index. This surprises beginners and is the reason to run git status before you commit.

You can also ask Git to show the content difference between neighbouring areas. git diff compares the working tree with the index, and git diff --staged compares the index with the last commit. Here a file has one staged edit and one newer unstaged edit:

Bash Comparing the areas with git diff

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first diff says the working tree has version three where the index has version two; the second says the index has version two where the last commit has version one. (We cut the first four header lines with tail to keep the output short; Status, Diff and Seeing Changes explains them.)

Commits are snapshots

A commit does not store "what changed". It stores a complete snapshot of every tracked file at that moment, plus metadata. Three facts define a commit:

  1. A pointer to the snapshot (a tree object that lists every file and its content).
  2. The parent commit(s) it came after (none for the very first commit, two for a merge).
  3. Author, committer, timestamps and the message.
TEXT
  commit 40d50df          commit c3315ac          commit 9b3c72c
  +-------------+         +-------------+         +-------------+
  | snapshot A  |  <------| snapshot B  |  <------| snapshot C  |
  | parent: -   |         | parent: 40d |         | parent: c33 |
  | "first"     |         | "second"    |         | "third"     |
  +-------------+         +-------------+         +-------------+

Each arrow points from a commit to its parent, so history is a chain you can walk backwards. Because the parent's hash is part of the child's data, you cannot change an old commit without changing every commit after it. That makes history tamper-evident and is the foundation of rewriting history later.

"Snapshot" sounds expensive, but Git is clever: if a file is identical to the previous commit, the new snapshot just points to the same stored content. A project with 10,000 files where one file changed adds one new file's worth of data, plus a few small bookkeeping objects.

We can check that claim. The example below makes three commits, changing only one of two files each time, and then asks how many distinct file contents Git had to store.

Bash Unchanged files are shared between snapshots

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The stable.txt line shows the same ID three times: all three snapshots share one stored copy. changing.txt has three different IDs because it has three different contents. (The syntax HEAD~2:stable.txt means "the file stable.txt as it was two commits ago"; you will master that in Revision Syntax.)

Git is still able to store history compactly on disk. It later packs similar objects together and stores differences between them (see The Index, Packfiles and Garbage Collection). That is a storage optimization; the model is always full snapshots.

Names are hashes

Every commit (and every file version and folder listing) is identified by a hash, a 40-character hexadecimal fingerprint of its content. You almost never type all 40 characters. Git accepts any unique prefix, usually 7 characters, which is what --oneline and --short show.

Two consequences:

  • Identical content gets the same ID. That is how snapshots share unchanged files.
  • The ID proves integrity. If one bit on disk changes, the hash no longer matches and Git can tell. git fsck checks this.

Hashes are not friendly for humans, which leads to the next idea.

Branches are movable labels

A branch is nothing more than a name that points to one commit. That is all. It is a tiny file containing a 40-character ID. When you make a new commit on a branch, Git creates the commit and moves the label forward to it.

A special pointer called HEAD says where you are right now. Usually HEAD points to a branch name, which points to a commit.

TEXT
   40d50df <---- c3315ac <---- 9b3c72c
                                  ^
                                  |
                                main  <---- HEAD

Creating a branch costs almost nothing, because it just writes a new label. That is why Git users create branches freely for every task. See it with your own eyes:

Bash Branches and HEAD are just pointers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Creating feature did not copy anything: both labels pointed at the same commit. Only when we committed on feature did that label move ahead while main stayed behind. The graph at the end shows both labels and where HEAD is. Branching gets a full lesson in Branches.

Everything is local first

The complete repository, including all history and all branches, is on your disk. Think of the commands in two groups:

Group Examples Network
Local add, commit, log, diff, branch, switch, merge, rebase, stash, reset Never
Sharing clone, fetch, pull, push Yes (to a remote)

A remote is just another repository that yours knows about by name. When you push, Git copies the commits the other side does not have and updates a label there. When you fetch, it does the reverse. Nothing in the local group needs a server, which is why Git feels instant. Remotes are the subject of Remotes: clone, remote, fetch.

Why Git is fast

Several design choices add up:

  • Local history. No round trip to a server for log, diff or blame.
  • Content addressing. Comparing two snapshots can skip whole folders whose hash is equal.
  • Cheap branches. A branch is a 41-byte file, not a copy of your project.
  • Compression and delta packing keep repositories small and transfers short.
  • The index caches file information, so git status usually checks timestamps and sizes rather than rereading every file.

A mental checklist

When something puzzles you, ask these four questions:

  1. Which area is the change in: working tree, index, or repository?
  2. Which commit does HEAD point to, and which branch (if any) does it follow?
  3. Which labels (branches, tags) point at the commits I care about?
  4. Is the data local or on a remote?

Almost every beginner question ("where did my change go?", "why is my branch behind?") is answered by those four.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the staging area (index)?

    The index holds the content that the next git commit will record. You edit in the working tree, stage with git add, and the commit takes whatever is staged.
  2. 2. A file shows MM in git status --short. What does it mean?

    The left M compares index with the last commit, the right M compares the working tree with the index. Committing now would record only the staged version.
  3. 3. How does Git store a commit?

    Each commit points to a full snapshot. Files that did not change point to the same stored content, so snapshots are cheap.
  4. 4. What is a branch in Git?

    A branch is a small file holding a commit ID. Committing on the branch moves the label forward, which is why creating branches is nearly free.
  5. 5. Which of these commands needs a remote repository to do its job?

    push, pull, fetch and clone talk to another repository. Logging, branching and committing all work on your local copy.

Summary

  • Files live in three areas: the working tree, the staging area (index) and the repository.
  • git add copies changes from the working tree into the index; git commit records the index as a new commit.
  • A commit is a snapshot with a parent pointer, an author and a message; unchanged files are shared between snapshots.
  • Commits are named by content hashes; a short prefix (7 characters) is usually enough.
  • A branch is a movable label on a commit, and HEAD marks where you currently are.
  • Local commands never touch the network; clone, fetch, pull and push exchange commits with a remote.

Next: Installing and Configuring Git