The Hook (The "Byte-Sized" Intro)
git add is the most misunderstood command in Git. Beginners think it "saves" files — it doesn't. It curates them. It's the bouncer at the commit door, deciding what gets in and what stays out. Master git add and you master the art of clean commits.
📖 What is git add?
git add moves changes from the working tree into the staging area. It doesn't create a commit — it prepares one. Think of it as selecting items for your next snapshot.
Conceptual Clarity
git addcopies the current state of a file into the staging area- If you modify the file after
git add, the staging area still has the old version — you mustgit addagain - You can stage individual files, directories, patterns, or even individual lines within a file
git addalso marks untracked files as tracked for the first time- Staging is reversible — use
git restore --stagedto unstage
Real-Life Analogy
You're at an all-you-can-eat buffet:
- Working tree = The entire buffet spread
git add= Putting specific dishes on your plategit commit= Sitting down to eat (plate is sealed)
You can go back and swap items on your plate before sitting down. But once you sit, that's your meal.
Visual Architecture
Why It Matters
- Precision: You decide exactly what enters each commit — no more, no less.
- Atomic commits: Stage related changes together, keep unrelated changes separate.
- Partial staging: With
git add -p, you can stage specific lines within a file. - Safety: Nothing is committed until you say so. The staging area is your review checkpoint.
Code
# ─── Stage one file ───
git add app.js
# ─── Stage multiple files ───
git add app.js style.css README.md
# ─── Stage everything in the current directory ───
git add .
# ─── Stage everything in the entire repo ───
git add -A
# ─── Stage all tracked, modified files (skip untracked) ───
git add -u
# ─── Stage by pattern (all JavaScript files) ───
git add "*.js"
# ─── Interactive patch mode: stage specific hunks ───
git add -p app.js
# Git shows each change ("hunk") and asks:
# Stage this hunk [y,n,q,a,d,s,e,?]?
# y = stage this hunk
# n = skip this hunk
# s = split into smaller hunks
# q = quit (stage nothing more)
# ─── Dry run: see what WOULD be staged ───
git add --dry-run .
# Shows what files would be staged without actually staging them
# ─── Verify what's staged ───
git status
git diff --staged # See the exact changes that are stagedgit add Cheat Sheet
| Command | What It Does |
|---|---|
git add file.txt | Stage one specific file |
git add dir/ | Stage all changes in a directory |
git add . | Stage everything in current dir and below |
git add -A | Stage ALL changes in entire repo (adds + deletes) |
git add -u | Stage only tracked modified/deleted files (no new files) |
git add -p | Interactive patch — stage hunks selectively |
git add "*.js" | Stage all files matching a pattern |
git add --dry-run . | Preview what would be staged |
Common Pitfall
# You stage a file, then modify it again:
echo "v1" > app.js
git add app.js # Stages "v1"
echo "v2" > app.js # Working tree now has "v2"
git status
# Output:
# Changes to be committed: app.js ← "v1" (staged version)
# Changes not staged: app.js ← "v2" (working tree)
# If you commit now, only "v1" gets committed!
# Fix: run git add app.js again to stage "v2"Key Takeaways
git addmoves changes to the staging area — it does not create a commit.- Use
git add -pfor surgical, line-level staging. git add .stages everything locally;git add -Astages everything repo-wide.- If you modify a file after staging, you must
git addagain to update the staging area.
Interview Prep
-
Q: What is the difference between
git add .andgit add -A? A:git add .stages new and modified files in the current directory and its subdirectories.git add -A(or--all) stages all changes across the entire repository, including file deletions and changes in parent directories. -
Q: What happens if you modify a file after running
git add? A: The staging area retains the version from whengit addwas run. The newer changes are only in the working tree. You must rungit addagain to update the staged version. -
Q: How can you stage only specific lines from a file? A: Use
git add -p(patch mode). Git presents each changed "hunk" interactively and lets you accept (y), reject (n), or split (s) each one individually.