Skip to main content

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,?]?
KeyAction
yStage this hunk
nDo not stage this hunk
sSplit into smaller hunks
eManually edit the hunk
qQuit β€” stop staging
aStage all remaining hunks in this file
dSkip 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​

FlagMeaning
.All changes in current directory tree
-A / --allAll changes in the whole repo
-u / --updateModifications + deletions only (no new files)
-p / --patchInteractive hunk-by-hunk staging
-i / --interactiveFull interactive staging menu
-n / --dry-runShow what would be added without doing it
-v / --verboseShow files being added

Commit Hygiene Starts Here

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)​

  1. How do you use patch staging to keep commits reviewable in high-change files?
  2. What policy prevents accidental staging of secrets or generated artifacts?
  3. When do you prefer git add -u over git add -A in production workflows?
  4. How do you coach teams to avoid β€œeverything in one commit” behavior?

Short answer guide:

  • Use -p to separate intent and reduce review noise.
  • Combine .gitignore, pre-commit checks, and secret scanning.
  • Prefer -u when you only want tracked-file updates.
  • Tie commit quality to PR standards and release traceability.
Interview Focus

Show how selective staging improves review quality and rollback confidence in real team workflows.

Interview Trap

Staging everything by default without reviewing git diff --staged.