Lesson 7 +10 XP

The Staging Environment

The Staging Environment

The staging area (also called the index) is a middle step between editing your files and saving them.

Why stage first?

Staging lets you decide exactly which changes go into your next commit. You can:

  • Stage only some files, and leave others for later.
  • Check everything looks right before committing.
  • Group small related changes into clean commits.

Stage files

git add file1.js file2.js

Stage all changes:

git add -A

Or stage only modified and new files (not deletions):

git add -u

Unstage a file

If you staged the wrong file, take it back out of the staging area:

git restore --staged file1.js

The file stays changed in your working directory, it is just no longer staged.

See the difference

  • git status shows what is staged and what is not.
  • git diff shows unstaged changes.
  • git diff --staged shows exactly what is ready to commit.

TL;DR

  • The staging area is the "prepare for commit" step.
  • git add stages files.
  • git restore --staged unstages them.
  • git diff --staged previews your next commit.