git add β Staging Changes
What It Doesβ
git add moves changes from your working tree into the index (also called the staging area). Think of the index as a draft of your next commit β you curate exactly what goes in before committing.
The index allows you to be intentional. You can edit ten files but only commit three of them, or stage only specific lines within a single file.
Syntaxβ
git add [options] [pathspec...]
Common Usageβ
Stage a specific fileβ
git add src/main/java/com/example/TransactionService.java
Stage all changes in the current directory (and subdirectories)β
git add .
Stage all tracked and untracked changes across the entire repoβ
git add -A
# or equivalently:
git add --all
Stage only modifications and deletions (not new/untracked files)β
git add -u
# or:
git add --update
Stage a whole directoryβ
git add src/main/java/com/example/service/
Stage files matching a patternβ
git add "*.java"
git add src/**/*.yml
Interactive and Patch Stagingβ
Interactive staging (-i)β
Opens a menu to selectively stage, unstage, or inspect files:
git add -i
Patch mode (-p) β stage individual hunksβ
This is one of Git's most powerful features. Stage specific lines within a file, not the whole file:
git add -p
# or:
git add --patch
Git presents each changed "hunk" one at a time and asks what to do:
@@ -14,7 +14,9 @@ public class TransactionService {
public Page<TransactionDto> findTransactions(...) {
- validateDateRange(fromDate, toDate);
+ validateDateRange(fromDate, toDate);
+ log.debug("Fetching transactions for userId={}", userId);
return transactionRepository.findBy...
}
Stage this hunk [y,n,q,a,d,s,e,?]?
| Key | Action |
|---|---|
y | Stage this hunk |
n | Do not stage this hunk |
s | Split into smaller hunks |
e | Manually edit the hunk |
q | Quit β stop staging |
a | Stage all remaining hunks in this file |
d | Skip this file and all remaining hunks |
? | Show help |
This is useful when a file has two unrelated changes and you want to make two separate, focused commits.
Viewing What Is Stagedβ
After staging, check what is queued for the next commit:
# Show staged vs last commit (what will go into the commit)
git diff --staged
# Show working tree vs staged (what is NOT yet staged)
git diff
# High-level summary of staged, unstaged, and untracked files
git status
git status -s # short format
Unstaging Filesβ
If you staged something by mistake:
# Unstage a specific file (keeps your working tree changes)
git restore --staged src/main/java/com/example/TransactionService.java
# Unstage everything
git restore --staged .
# Legacy syntax (still works):
git reset HEAD <file>
Ignoring Files with .gitignoreβ
Files listed in .gitignore will never be staged or tracked. Common entries for Java/Spring projects:
# Build output
target/
*.class
*.jar
*.war
# IDE files
.idea/
*.iml
.vscode/
.eclipse/
# Spring Boot
*.log
application-local.yml
application-secret.yml
# OS
.DS_Store
Thumbs.db
# Environment variables
.env
.env.local
Force-add a normally ignored file (use sparingly):
git add -f src/main/resources/application-template.yml
Useful Flags Summaryβ
| Flag | Meaning |
|---|---|
. | All changes in current directory tree |
-A / --all | All changes in the whole repo |
-u / --update | Modifications + deletions only (no new files) |
-p / --patch | Interactive hunk-by-hunk staging |
-i / --interactive | Full interactive staging menu |
-n / --dry-run | Show what would be added without doing it |
-v / --verbose | Show files being added |
Use git add -p to keep commits small and focused. A commit titled "fix: resolve NPE in TransactionService" should only contain the fix β not an unrelated refactor you happened to do in the same session. Patch staging makes that easy.
Interview Questions (Senior Level)β
- How do you use patch staging to keep commits reviewable in high-change files?
- What policy prevents accidental staging of secrets or generated artifacts?
- When do you prefer
git add -uovergit add -Ain production workflows? - How do you coach teams to avoid βeverything in one commitβ behavior?
Short answer guide:
- Use
-pto separate intent and reduce review noise. - Combine
.gitignore, pre-commit checks, and secret scanning. - Prefer
-uwhen you only want tracked-file updates. - Tie commit quality to PR standards and release traceability.
Show how selective staging improves review quality and rollback confidence in real team workflows.
Staging everything by default without reviewing git diff --staged.