git add — Staging Working Tree Changes
How to stage changes in Git: git add ., staging specific files, interactive patch staging (git add -p), and unstaging files.
git add — Staging Working Tree Changes
The git add command adds changes in your working directory to the staging area (the index). It tells Git that you want to include updates to a particular file in the next commit snapshot.
1. Common Staging Commands
# 1. Stage a specific individual file:
git add src/components/Header.tsx
# 2. Stage multiple files:
git add package.json pnpm-lock.yaml
# 3. Stage all modified, deleted, and new files in the entire project:
git add .
# or:
git add -A2. Interactive Patch Staging (git add -p)
When you have modified multiple parts of a single file but only want to commit some of the changes, use interactive patch staging:
git add -p src/utils/format.tsGit splits the file into visual diff chunks ("hunks") and prompts you for each hunk:
y: Stage this hunk.n: Do not stage this hunk.s: Split the hunk into smaller hunks.q: Quit interactive staging.
3. Unstaging Files (git restore --staged)
If you accidentally staged a file that you do not want in the upcoming commit:
# Unstage a specific file (preserves disk changes):
git restore --staged sensitive-config.json
# Unstage all currently staged files:
git restore --staged .Related Topics
git status — Checking Working Tree State
Mastering git status: tracked vs untracked files, staged vs unstaged modifications, short format (-s), and branch sync status.
git commit — Creating Permanent Snapshots
Creating commits in Git: writing semantic commit messages, amending the previous commit (--amend), atomic commits, and commit hashes (SHA-1).