Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
What is a Repository  ›  Lesson

The Working Directory and the Staging Area

Staging Area 12 minChanges start in the working directory, are chosen for the next snapshot in the staging area with git add, and become history with git commit.

The Working Directory and the Staging Area

Asha has edited two recipes: she fixed a typo in chai.md and started a half-finished rewrite of pancakes.md. She wants to save only the typo fix. Git makes that possible because a change passes through three places before it becomes history, and you choose what moves between them.

The rule

Edit in the working directory, choose in the staging area, keep in history.

  1. Working directory: the real files in your folder. This is where you edit. Git notices changes here but has not saved them.
  2. Staging area (also called the index): a waiting room for the next snapshot. git add copies a file's current content here.
  3. Repository history: the commits stored in .git. git commit turns whatever is staged into a permanent snapshot.
git add chai.md      # working directory -> staging area
git commit -m "Fix typo in chai recipe"   # staging area -> history

git status tells you where everything stands:

git status
# Changes to be committed:
#   modified:   chai.md
# Changes not staged for commit:
#   modified:   pancakes.md

"To be committed" means staged. "Not staged" means changed in the working directory only. Untracked files are ones Git has never recorded.

To see the actual lines, use two forms of git diff:

  • git diff compares the working directory with the staging area: what you changed but have not staged.
  • git diff --staged compares the staging area with the last commit: what the next commit will contain.

And to move things back:

  • git restore --staged chai.md unstages the file, keeping your edits.
  • git restore pancakes.md throws away unstaged edits and returns the file to its staged or committed state. This cannot be undone, so use it with care.

The shortcut

Picture three boxes in a row: Working → Staged → Committed. For any command, ask "which box does this move content between?"

Saving only the typo fixStep 1 of 4
  1. Check the boxes

    git status shows both chai.md and pancakes.md as modified and not staged.

The trap is editing a file after git add. The staging area holds the content from the moment you added it, so the newer edits show as "not staged" too. Run git add again to include them.

Drills

Answer fast — trust the rule.

quick drill

Which command moves pancakes.md from the working directory into the staging area?

quick drill

What does git diff --staged show?

quick drill

Asha runs git add chai.md, then edits chai.md again. What does git status show for it?

quick drill

Asha staged pancakes.md by mistake. Which command unstages it but keeps her edits?

Remember

  • Edit in the working directory, choose in the staging area, keep in history.
  • Working → Staged → Committed: ask which boxes a command moves between.
  • git diff shows unstaged changes; git diff --staged shows what the next commit holds.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainCommit only part of your work

Asha has edited both chai.md (finished) and pancakes.md (half done). Explain, step by step, how she commits only the chai change, and describe where each file's changes sit after the commit.