Git Working Directory, Staging Area and Repository Explained

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.

Advertisement

πŸ“– 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.

AreaAlso calledWhat it holds
Working directoryWorking treeThe files you see and edit in your folder
Staging areaIndexThe changes you have selected for the next commit
RepositoryLocal repo, .gitEvery 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

StateMeaningHow it gets there
UntrackedNew file Git has never seenYou create a file
UnmodifiedTracked and identical to the last commitAfter a commit
ModifiedTracked, changed, not stagedYou edit a tracked file
StagedChange selected for the next commitgit 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
CodeMeaning
M in the left columnModified and staged
M in the right columnModified, not staged
MMStaged, then modified again
ANew file, staged
DDeleted
??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

GoalCommand
Stage a changegit add file
Unstage a change (keep it in the file)git restore --staged file
Discard unstaged changes in a filegit restore file (cannot be undone)
Stop tracking a file but keep it on diskgit rm --cached file
Delete a tracked file and stage the deletiongit rm file
Rename or move a tracked filegit 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:

  1. Create a new file, modify a tracked file, and stage only the modified one. Run git status and git status -s and identify each state.
  2. Run git diff, git diff --staged and git diff HEAD and describe what each shows.
  3. Stage a file, then unstage it with git restore --staged.
  4. Make a change you do not want and discard it with git restore.
  5. Rename a file with git mv and commit the rename.
  6. 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

🎬 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”

Leave a Comment