The Three Areas of Git: Working Tree, Staging Area and Repository
The model behind every Git command: the working tree you edit, the staging area that prepares the next commit, and the repository of committed snapshots. How a file moves between them, how to see each version, what commit -a skips, and why the staging area exists.
kvi5hnuCode Sprout team member10 min readOn this page
Most confusion with Git comes from not knowing where a change is. Is it only in the file you edited? Is it prepared for the next commit? Is it committed? Git keeps up to three versions of every file at once, in three places:
- The working tree: the ordinary files in your project folder, the ones you open and edit.
- The staging area, also called the index: the next commit, being prepared.
git addcopies changes here. - The repository: the committed snapshots, stored in
.git.HEADrefers to the latest commit on the branch you're on.

A commit doesn't take your files as they are on disk. It records the staging area. That single fact explains a lot of Git behaviour that otherwise seems odd.
Three versions of one file
The example makes the three areas visible. It commits a file, stages a change to it, then makes another change without staging it, so the file has a different version in each area:
git init -q trail-guide
cd trail-guide
echo "Ridge loop: 8 km" > trails.txt
git add trails.txt
git commit -q -m "Add trails list"
echo "Lake path: 5 km" >> trails.txt
git add trails.txt # stage the second line
echo "Summit climb: 12 km" >> trails.txt # a third line, not staged
echo "--- in the last commit (HEAD)"
git show HEAD:trails.txt
echo "--- in the staging area"
git show :trails.txt
echo "--- in the working tree"
cat trails.txt
echo "--- git status"
git status
--- in the last commit (HEAD)
Ridge loop: 8 km
--- in the staging area
Ridge loop: 8 km
Lake path: 5 km
--- in the working tree
Ridge loop: 8 km
Lake path: 5 km
Summit climb: 12 km
--- git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: trails.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: trails.txt
HEAD:trails.txt names the file as committed, and :trails.txt, with nothing before the colon, names the staged version. The working tree has all three lines.
git status lists trails.txt twice: under "Changes to be committed", the difference between HEAD and the staging area, and under "Changes not staged for commit", the difference between the staging area and the working tree. If you committed now, the commit would contain two lines, not three. The third line would stay in the working tree as an uncommitted change.
Comparing the areas with git diff
Each pair of areas has its own git diff:
| Command | Compares | Answers |
|---|---|---|
git diff | staging area → working tree | "What have I changed but not added yet?" |
git diff --staged (or --cached) | HEAD → staging area | "What will the next commit contain?" |
git diff HEAD | HEAD → working tree | "What have I changed since the last commit, staged or not?" |
git init -q trail-guide
cd trail-guide
echo "Ridge loop: 8 km" > trails.txt
git add trails.txt
git commit -q -m "Add trails list"
echo "Lake path: 5 km" >> trails.txt
git add trails.txt
echo "Summit climb: 12 km" >> trails.txt
echo "=== git diff (not yet staged)"
git diff
echo "=== git diff --staged (what the next commit contains)"
git diff --staged
echo "=== git diff HEAD (everything since the last commit)"
git diff HEAD
=== git diff (not yet staged)
diff --git a/trails.txt b/trails.txt
index 8a303ba..2dc574b 100644
--- a/trails.txt
+++ b/trails.txt
@@ -1,2 +1,3 @@
Ridge loop: 8 km
Lake path: 5 km
+Summit climb: 12 km
=== git diff --staged (what the next commit contains)
diff --git a/trails.txt b/trails.txt
index e4e52a0..8a303ba 100644
--- a/trails.txt
+++ b/trails.txt
@@ -1 +1,2 @@
Ridge loop: 8 km
+Lake path: 5 km
=== git diff HEAD (everything since the last commit)
diff --git a/trails.txt b/trails.txt
index e4e52a0..2dc574b 100644
--- a/trails.txt
+++ b/trails.txt
@@ -1 +1,3 @@
Ridge loop: 8 km
+Lake path: 5 km
+Summit climb: 12 km
In a diff, lines starting with + were added and lines starting with - were removed. The @@ -1 +1,2 @@ header says which lines are shown: line 1 of the old version, and lines 1 to 2 of the new one. Article 7 explains the format and git diff's many options. The most common surprise is that plain git diff shows nothing after git add: the changes are now staged, so you need --staged to see them.
How a file moves between the areas
Every file in the working tree is either tracked, meaning it's in the staging area (and usually in HEAD), or untracked, meaning Git has never been told about it. A tracked file is unmodified if all three versions match, modified if the working tree differs, and staged if the staging area differs from HEAD.
The commands that move content between areas:
| Command | Copies | Effect |
|---|---|---|
git add file | working tree → staging area | stage the file as it is now |
git commit | staging area → repository | record the staging area as a new commit |
git restore --staged file | HEAD → staging area | unstage: the edit stays in the working tree |
git restore file | staging area → working tree | discard unstaged edits to the file |
git restore --source=HEAD --staged --worktree file | HEAD → both | throw away all changes to the file since the last commit |
git rm --cached file | removes from the staging area | stop tracking the file, but keep it on disk |
git switch branch | a commit → both | change branches, filling both areas from that branch's commit |
git init -q trail-guide
cd trail-guide
echo "Ridge loop: 8 km" > trails.txt
git add trails.txt
git commit -q -m "Add trails list"
echo "Lake path: 5 km" >> trails.txt
git add trails.txt
echo "--- staged"
git status -s
git restore --staged trails.txt
echo "--- unstaged again: the edit is still in the file"
git status -s
cat trails.txt
git restore trails.txt
echo "--- discarded: the file matches the last commit"
git status -s
cat trails.txt
--- staged
M trails.txt
--- unstaged again: the edit is still in the file
M trails.txt
Ridge loop: 8 km
Lake path: 5 km
--- discarded: the file matches the last commit
Ridge loop: 8 km
git restore without --staged overwrites the file in the working tree. Changes that were never staged or committed can't be recovered afterwards, because Git never had a copy. Use it only when you really want to throw the edit away.
git restore arrived in Git 2.23 (2019), with git switch, to split the overloaded git checkout into clearer commands. Older guides use git reset HEAD file to unstage and git checkout -- file to discard edits. Both still work. Article 24 covers undoing in full.
git commit -a: skipping the staging step
git commit -a (or --all) stages every change to tracked files, then commits. It's a convenient shortcut when you want to commit everything you've changed. It has one important gap: it doesn't add untracked files.
git init -q trail-guide
cd trail-guide
echo "Ridge loop: 8 km" > trails.txt
git add trails.txt
git commit -q -m "Add trails list"
echo "Lake path: 5 km" >> trails.txt # a change to a tracked file
echo "Pack water and a map." > tips.txt # a new, untracked file
git commit -a -m "Add lake path"
echo "--- after git commit -a"
git status -s
git show --stat --format="%s" HEAD
[main 61c2bbf] Add lake path
1 file changed, 1 insertion(+)
--- after git commit -a
?? tips.txt
Add lake path
trails.txt | 1 +
1 file changed, 1 insertion(+)
tips.txt is still untracked after the commit. New files always need an explicit git add the first time.
Why have a staging area at all?
Some version control systems commit every changed file at once. Git's extra step exists so you can choose what each commit contains, and good commits pay off later, when you read history, review changes, or undo one change without undoing another:
- Split unrelated changes. You fixed a typo in one file and started a new feature in another. Stage and commit the typo fix on its own, then commit the feature separately.
- Commit part of a file.
git add -pstages selected parts of a file's changes (article 5), so leftover debugging lines or an unfinished second change stay out of the commit. - Review before recording.
git diff --stagedshows exactly what you're about to commit. - Resolve merges step by step. During a merge conflict, the staging area tracks which files you've resolved (article 13).
git init -q trail-guide
cd trail-guide
printf "# Trial guide\n" > README.md
echo "Ridge loop: 8 km" > trails.txt
git add . && git commit -q -m "Start project"
printf "# Trail guide\n" > README.md # fix a typo
echo "Lake path: 5 km" >> trails.txt # an unrelated new trail
git add README.md
git commit -q -m "Fix typo in README title"
git add trails.txt
git commit -q -m "Add lake path"
git log --stat --format="%h %s"
f449756 Add lake path
trails.txt | 1 +
1 file changed, 1 insertion(+)
0124736 Fix typo in README title
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
6ad4fd3 Start project
README.md | 1 +
trails.txt | 1 +
2 files changed, 2 insertions(+)
Each commit now contains one logical change, so either can be reviewed, reverted or cherry-picked on its own.
Looking at the staging area directly
The staging area isn't just a list of changes: it holds a full copy, a reference to the stored contents, of every tracked file. Right after a commit, it matches HEAD exactly. git ls-files lists the files in it, and --stage shows the details:
git init -q trail-guide
cd trail-guide
mkdir trails
echo "# Trail guide" > README.md
echo "Ridge loop: 8 km" > trails/ridge.txt
echo "draft" > notes.txt
git add README.md trails
git commit -q -m "Start project"
echo "--- tracked files (notes.txt is untracked, so it's not listed)"
git ls-files
echo "--- mode, object ID, stage number and path"
git ls-files --stage
--- tracked files (notes.txt is untracked, so it's not listed)
README.md
trails/ridge.txt
--- mode, object ID, stage number and path
100644 fc9f508347d28b40eb3d7526f37ec1a43f02f8bf 0 README.md
100644 e4e52a05617d5ccc302695b22864aea0c62cb606 0 trails/ridge.txt
Each line of --stage shows the file mode (100644, an ordinary file), the ID of the stored contents (a blob, article 48), a stage number (0 normally, 1 to 3 during a merge conflict) and the path. The file itself is stored once in .git/objects, and the staging area, the commit and other versions all refer to it by that ID.
What's current
git restoreandgit switch(Git 2.23) are the clearest commands for moving content between the areas. They're stable, and Git 3.0 keepsgit checkoutas well: none of the three is going away.git statushints always name the command to undo each step, such as "usegit restore --staged <file>...to unstage". If you forget a command, rungit statusand read the hints.- Editors and IDEs show the staging area as "Staged Changes" or a checkbox per file: the same idea, with a different interface.
Common questions
Is "index" the same as "staging area"?
Yes. "Index" is the original name, and you'll see it in commands and options such as --cached (an older spelling of --staged). Physically, it's the file .git/index.
If I edit a file after git add, which version gets committed?
The version from the time you ran git add. Later edits are in the working tree only. Run git add again to stage them, or git commit -a to stage all tracked changes as you commit.
Does git add save my work permanently?
Partly. The staged contents are stored in .git/objects, so they survive even if you overwrite the file. But staged content isn't part of the history until it's committed, and it's easy to lose by staging something else over it. Commit early and often for real safety.
Why does git diff show nothing when I know I changed the file?
The change is already staged. git diff compares the working tree with the staging area. Use git diff --staged to see staged changes, or git diff HEAD to see everything since the last commit.
What happens to the working tree and staging area when I switch branches? Git fills both with the other branch's commit. Uncommitted changes that don't conflict are carried along. If a change would be overwritten, Git refuses to switch until you commit or stash it (articles 11 and 27).
Summary
- Git keeps up to three versions of each file: in the working tree, in the staging area (index), and in the last commit,
HEAD. - A commit records the staging area, not the working tree. Changes you didn't
git addaren't included. git diffshows unstaged changes,git diff --stagedshows what the next commit will contain, andgit diff HEADshows both together.git addstages,git restore --stagedunstages, andgit restorediscards unstaged edits, permanently.git commit -astages and commits changes to tracked files, but never adds new files.- The staging area lets you split work into focused commits.
Practice questions
What does git status -s print after these commands, and what would git commit (without -a) record?
git init -q && echo a > f.txt && git add f.txt && git commit -q -m one
echo b >> f.txt
git add f.txt
echo c >> f.txt
Answer
git status -s prints MM f.txt: modified and staged in the first column, modified again in the working tree in the second. A plain git commit would record f.txt with the lines a and b, the staged version. The line c would remain as an unstaged change.
You want to see exactly what your next commit will contain. Which command do you run, and why isn't plain git diff enough?
Answer
git diff --staged (or --cached), which compares HEAD with the staging area: exactly the changes a commit would record. Plain git diff compares the staging area with the working tree, so it shows what you haven't staged. That's the opposite.
A teammate runs git commit -a -m "Add config loader", but the new file loader.py is missing from the commit. Why, and how do they fix it?
Answer
-a stages changes to tracked files only, and loader.py was new and untracked. Fix it with git add loader.py followed by git commit --amend --no-edit, which adds the file to the commit just made, if it hasn't been pushed yet.
Your friend ran git restore notes.txt and lost an afternoon of edits that were never staged. Can Git get them back?
Answer
No. git restore overwrote the working-tree file with the staged version, and the lost edits were never staged or committed, so Git never stored them. Recovery would depend on the editor's undo history or local history, or on a backup. Edits that were staged at some point can sometimes be recovered as dangling blobs (article 26), which is one more reason to stage and commit often.
Review quiz
Test yourself on Part 1 with the Git Part 1 review quiz.
Previous: Your First Git Repository: init, add, commit, status and log
Next: Staging Changes: add, Patch Mode, Interactive Staging, rm, mv and Unstaging

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.