- Developers who already know basic commit and branch actions
- Readers who want to understand command boundaries and risk
Command Reference
git add Tutorial
Explains how git add stages changes for the next commit, how patch staging works, and how to avoid mixing unrelated edits into one commit.
- A basic mental model of worktree, index, and commits
- Comfort reading `git status` and a small commit graph
- Using local cleanup commands on already shared history
- Continuing to rewrite before confirming a recovery path
The short version
git add copies selected content from the working tree into the index so the next commit can use it.
git add does not “save your work” by itself. It updates the staging snapshot. That is why the same file can still have unstaged edits even after you run git add.
What problem this command actually solves
New Git users often assume a commit is built directly from current files.
Git is stricter than that: it asks you to decide which content belongs in the next commit first.
That extra step is what makes these workflows possible:
- split one messy working session into several logical commits
- stage only the lines that are ready for review
- leave half-finished experiments out of the next commit
- resolve conflicts and stage only the resolved files
The forms you will actually use
git add README.md
git add src/
git add .
git add --patch
git add README.md: stage one filegit add src/: stage a directorygit add .: stage all detected changes under the current directorygit add --patch: stage selected hunks interactively
A realistic daily workflow
The safest everyday loop is usually:
- inspect the current state with
git status - inspect the actual changes with
git diff - stage intentionally with
git add <path>orgit add --patch - confirm the staged result with
git diff --staged - commit only after the index looks right
For example:
git status
git diff
git add --patch
git diff --staged
git commit -m "fix: keep search fallback stable"
If a commit should tell one story, the index is the place where you enforce that story before it becomes history.
Where people get into trouble
The most common mistake is not syntax.
It is staging more than intended because git add . felt convenient.
Typical failure patterns:
- staging debug output, generated files, or unrelated refactors together
- assuming a file is fully staged when only part of it should be
- forgetting that new edits made after
git addare still unstaged - resolving a conflict but forgetting to stage the resolved file
Patch mode is the skill that changes everything
git add --patch
Patch mode matters because it lets you separate:
- one bug fix from a nearby refactor
- production code from temporary logging
- finished changes from still-uncertain edits
That is often the difference between:
- a commit that is easy to review
- and a commit that future-you cannot safely revert or cherry-pick
How to reason about staged versus unstaged edits
Imagine you modify the same file twice:
- first you change the real logic
- then you add temporary console output
- you run
git add --patchand stage only the logic change
Now the file exists in two states at once:
- the index contains the logic fix
- the working tree still contains the temporary logging
That is not weird Git behavior.
That is exactly what the index is for.
Diagram view
Boundaries worth remembering
git addusually does not move branch pointers, but it absolutely changes what the next commit will mean.git add .is fast, not careful. Prefer narrower staging when the change set is mixed.- If you are about to switch branches or recover from mistakes, a clean understanding of what is staged versus unstaged matters a lot.
Staging content does not publish it anywhere. It only changes the local index. If you need a recovery point, create a commit or a stash rather than assuming staged changes are “safe enough”.
Try it yourself
This exercise helps you feel the difference between the working tree and the index.
git switch -c lab/add-demo # edit one file with both a real fix and a temporary debug lineTry it
- Run `git diff` to see all edits.
- Run `git add --patch` and stage only the real fix.
- Run `git diff --staged` and confirm the staged change is clean.
- Leave the debug line unstaged and create the commit.
- The commit contains only the staged change.
- The working tree can still keep extra unstaged edits.
- You end up with cleaner history without losing the unfinished work.
- If `git diff` looks empty, check `git diff --staged` because the change may already be in the index.
- If your commit still contains mixed work, you probably staged too broadly.
- If you resolved a conflict, remember that the resolution is not complete until the file is staged.