The staging area (also called the index) is a preparation zone between your working directory and the repository. It lets you craft precise commits by selecting exactly which changes to include. git add -p (patch mode) lets you stage individual hunks within a file.
Git
Beginner
9 min read
The Staging Area and git add -p
Example
# Suppose you edited auth.js with two unrelated changes.
# You want to commit them separately for a clean history.
# Interactive patch mode — stage only some hunks:
git add -p auth.js
# Git shows each changed hunk and asks:
# Stage this hunk? [y/n/q/a/d/s/?]
# y — yes, stage this hunk
# n — no, skip this hunk
# s — split into smaller hunks
# q — quit (don't stage remaining hunks)
# ? — show help
# Commit only the staged hunk:
git commit -m "fix: handle expired JWT tokens"
# Then stage and commit the second change:
git add -p auth.js
git commit -m "feat: add refresh token support"
# View what is staged vs unstaged:
git diff # unstaged changes
git diff --staged # staged changes (ready to commit)
# See staged files only (a quick summary):
git status -s
# M auth.js (M in green = staged, M in red = unstaged)
# ?? new.js (?? = untracked)