Skip to the workspace
Pebellslearn by building
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.

git github / The Working Directory and the Staging Area

Explore each scene at your own pace.

01 · The rule

The rule

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.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.