Most Git confusion disappears once you understand three places: the working directory, the staging area and the repository. This lesson explains them with diagrams, git status output and the commands that move changes between them.
π What You’ll Learn: the three areas of Git, the four file states, reading git status and its short format, git diff vs git diff –staged, moving changes with add, restore, rm and mv, and why the staging area creates cleaner commits.
π§© The Three Areas of Git
Every Git project has three areas. Understanding them makes every other Git command easier.
| Area | Also called | What it holds |
|---|---|---|
| Working directory | Working tree | The files you see and edit in your folder |
| Staging area | Index | The changes you have selected for the next commit |
| Repository | Local repo, .git | Every commit, saved permanently |
βββββββββββββββββββββ git add ββββββββββββββββ git commit ββββββββββββββββ
β Working directory β ββββββββββΆ β Staging area β ββββββββββββΆ β Repository β
β (edit files) β ββββββββββ β (index) β β (.git) β
βββββββββββββββββββββ git restoreββββββββββββββββ ββββββββββββββββ
--staged
Analogy: packing for a trip. The working directory is your room full of clothes, the staging area is the open suitcase where you put only what you want to take, and a commit closes the suitcase and labels it with the date.
π¦ The Four States of a File
| State | Meaning | How it gets there |
|---|---|---|
| Untracked | New file Git has never seen | You create a file |
| Unmodified | Tracked and identical to the last commit | After a commit |
| Modified | Tracked, changed, not staged | You edit a tracked file |
| Staged | Change selected for the next commit | git add |
π Reading git status
git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
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: test-plan.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
login.spec.ts
Three sections, three areas: staged (green), modified but not staged (red), and untracked.
The short format
git status -s
M README.md
M test-plan.txt
?? login.spec.ts
| Code | Meaning |
|---|---|
M in the left column | Modified and staged |
M in the right column | Modified, not staged |
MM | Staged, then modified again |
A | New file, staged |
D | Deleted |
?? | Untracked |
π¬ See the Differences in Each Area
git diff # working directory vs staging area (unstaged changes)
git diff --staged # staging area vs last commit (what will be committed)
git diff HEAD # working directory vs last commit (everything)
π‘ Tip: Run git diff --staged right before every commit. It shows exactly what you are about to save and catches debug code, passwords and accidental changes.
π Moving Changes Between Areas
| Goal | Command |
|---|---|
| Stage a change | git add file |
| Unstage a change (keep it in the file) | git restore --staged file |
| Discard unstaged changes in a file | git restore file (cannot be undone) |
| Stop tracking a file but keep it on disk | git rm --cached file |
| Delete a tracked file and stage the deletion | git rm file |
| Rename or move a tracked file | git mv old.txt new.txt |
β οΈ Common mistake: git restore file throws away your uncommitted edits to that file permanently. Check with git diff file first.
More ways to undo things, including commits, are in Undo Changes.
π― Why the Staging Area Is Useful
The staging area lets you build clean, focused commits even when you changed many things at once:
# You fixed a bug AND reformatted some files. Commit them separately:
git add src/checkout.ts
git commit -m "Fix rounding of tax on checkout"
git add .
git commit -m "Format test files with Prettier"
With git add -p you can even stage parts of a file: Git shows each changed block (hunk) and asks y (stage), n (skip), s (split) or q (quit).
Small, focused commits make history readable, code review easier and bugs easier to find with tools like git bisect.
π Assignment
In your git-practice repository:
- Create a new file, modify a tracked file, and stage only the modified one. Run
git statusandgit status -sand identify each state. - Run
git diff,git diff --stagedandgit diff HEADand describe what each shows. - Stage a file, then unstage it with
git restore --staged. - Make a change you do not want and discard it with
git restore. - Rename a file with
git mvand commit the rename. - Bonus: change two unrelated lines in one file and commit them separately using
git add -p.
β FAQ
π€ What is the staging area in Git?
A middle step between your files and the repository where you collect the exact changes for the next commit. It is also called the index.
π€ What is the difference between git diff and git diff –staged?
git diff shows changes not yet staged. git diff --staged shows staged changes that will go into the next commit.
π€ How do I unstage a file in Git?
git restore --staged file moves the change out of the staging area but keeps your edits in the file.
π€ What does ?? mean in git status -s?
The file is untracked: Git has never been told to track it.
π€ How do I stop tracking a file without deleting it?
git rm --cached file, then add it to .gitignore so it is not added again.
π― Next Steps
- π View History and Changes: git log, show and diff
- π .gitignore: Ignore Files the Right Way
- π Git and GitHub Tutorial: full course
π¬ Learn Git and GitHub step by step: watch the series on YouTube!
Subscribe to SqaToolsByDY • Practice on sqatools.in
1 thought on “Git Working Directory, Staging Area and Repository Explained”