The Three Areas
Git files move through the working directory, the staging area, and the repository before reaching a remote.
Understanding Git's three local areas is the key to everything else.
- Working directory — the actual files you edit.
- Staging area (the index) — a snapshot of changes you have marked for the next commit.
- Repository — the committed history stored in
.git.
You move changes forward with git add (working → staging) and git commit (staging → repository). Later, git push sends commits to a remote such as GitHub.
Example
# See which area each file is in
git status
# working directory -> staging
git add index.html
# staging -> repository
git commit -m "Add home page"When to use it
- A developer stages only the bug-fix lines from a file, leaving unfinished feature code unstaged so it stays out of the commit.
- A QA engineer inspects the staging area before a release commit to confirm exactly which changes will be recorded.
- A student accidentally edits the wrong file, discards it from the working directory, and only commits the intentional staged change.
More examples
Inspect each area separately
Three commands reveal what lives in each of Git's three areas at any moment.
# Working directory vs staging area
git diff
# Staging area vs last commit
git diff --staged
# Summary of all three areas
git statusMove a change through all areas
Walks a single file edit through working directory to staging area to permanent repository snapshot.
# 1. Edit in working directory
echo 'print("hello")' >> app.py
# 2. Move to staging area
git add app.py
# 3. Commit to repository
git commit -m "Print hello on start"Unstage a file before committing
Shows how to move a file backwards from the staging area to the working directory without losing any edits.
git add config.py app.py
# config.py should not be in this commit
git restore --staged config.py
git status
# config.py back in working directory, only app.py staged
Discussion