Q6 of 40 · Git

Explain the difference between the working directory, staging area, and commit.

GitJuniorgitstaging-areaworking-directorycommitsfundamentals

Short answer

Short answer: The working directory contains your actual files as you edit them. The staging area (index) holds the snapshot of changes selected for the next commit. A commit is a permanent, immutable snapshot stored in the repository. Changes flow: working directory → git add → staging area → git commit → repository.

Detail

Understanding Git's three areas explains why commands behave the way they do:

Working directory: the actual files on disk that you edit in your IDE. Git is aware of changes here (via git status) but doesn't manage them until you explicitly stage them.

Staging area (index): a prepared snapshot of what the next commit will contain. git add puts changes here; git restore --staged removes them. You can have a mix of staged and unstaged changes in the same file at the same time — the staging area holds a specific version, not just "is staged/not staged".

Repository (commit history): once you git commit, Git writes an immutable object (a commit) into the object store at .git/objects/. Commits point to parent commits, forming the history graph.

The practical flow:

Edit file → [working directory]
git add   → [staging area]
git commit → [repository / commit history]

And undoing:

git restore <file>          → discard working-directory change
git restore --staged <file> → unstage (move back to working dir)
git reset HEAD~1 --soft     → uncommit (move back to staging area)

// EXAMPLE

# See what's where at any point
git status
# On branch main
# Changes to be committed:         ← staging area
#   modified:   tests/LoginTest.java
# Changes not staged for commit:   ← working directory
#   modified:   tests/UserTest.java
# Untracked files:                 ← working directory (new files)
#   tests/NewFeatureTest.java

# Move changes through the pipeline
git add tests/LoginTest.java     # working dir → staging
git commit -m "fix: login test"  # staging → repository

# Move backwards
git restore --staged LoginTest.java  # staging → working dir (unstage)
git restore LoginTest.java           # working dir → discard changes

// WHAT INTERVIEWERS LOOK FOR

Ability to explain all three areas clearly and map git add, git commit, and the restore commands to the transitions between them. This mental model is the foundation for understanding git reset, git stash, and conflict resolution.

// COMMON PITFALL

Conflating the staging area with the working directory. Candidates who don't understand the staging area get confused by git diff vs git diff --staged and can't explain why git add followed by further file editing doesn't automatically update the staged version.