Lesson Completion
Back to course

git add

Beginner
10 minutes4.8GitPlay to LearnLearn by Story

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 add copies 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 must git add again
  • You can stage individual files, directories, patterns, or even individual lines within a file
  • git add also marks untracked files as tracked for the first time
  • Staging is reversible — use git restore --staged to 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 plate
  • git 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

flowchart LR subgraph WT["📂 Working Tree"] F1["app.js ✏️"] F2["style.css ✏️"] F3["test.js ✏️"] end F1 -->|"git add app.js"| SA["📋 Staging Area"] F2 -->|"git add style.css"| SA SA -->|"git commit"| REPO["💾 Repository"] style WT fill:#1a1a2e,stroke:#e94560,color:#e94560 style SA fill:#0f3460,stroke:#ffd700,color:#ffd700 style REPO fill:#0f3460,stroke:#53d8fb,color:#53d8fb

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

bash
# ─── 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 staged

git add Cheat Sheet

CommandWhat It Does
git add file.txtStage one specific file
git add dir/Stage all changes in a directory
git add .Stage everything in current dir and below
git add -AStage ALL changes in entire repo (adds + deletes)
git add -uStage only tracked modified/deleted files (no new files)
git add -pInteractive patch — stage hunks selectively
git add "*.js"Stage all files matching a pattern
git add --dry-run .Preview what would be staged

Common Pitfall

bash
# 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 add moves changes to the staging area — it does not create a commit.
  • Use git add -p for surgical, line-level staging.
  • git add . stages everything locally; git add -A stages everything repo-wide.
  • If you modify a file after staging, you must git add again to update the staging area.

Interview Prep

  • Q: What is the difference between git add . and git 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 when git add was run. The newer changes are only in the working tree. You must run git add again 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.

Topics Covered

Git BasicsGit Fundamentals

Tags

#git#add#staging#beginner-friendly

Last Updated

2026-02-12