Git Staging Area: status, add -p, commit --amend, and Partial Commits
The index is what a commit actually contains, which makes partial commits possible and commit -a dangerous. What amend rewrites, and when that is safe.

Quick Summary
Git has three places a change can live: the working tree, the index (staging area), and the commit history. git status compares all three; git add copies content from the working tree into the index; git commit turns the index into a commit. Nothing about a commit depends on the working tree — what you staged is exactly what gets committed, even if the file changed again afterwards.
That is the mechanism behind partial commits: stage one hunk, commit it, and the rest of the file stays uncommitted. git add -p is the tool for it, and git restore --staged is the undo.
git commit --amend replaces the previous commit rather than editing it — a new commit with a new hash, the old one left in the reflog. That is harmless before a push and a rewrite after one.
Basic Syntax & Core Signatures
# Inspect
git status
git status -sb # short format with branch and ahead/behind
git status --porcelain=v2 # stable machine-readable output for scripts
git diff # working tree vs index
git diff --staged # index vs HEAD (alias: --cached)
git diff HEAD # working tree vs HEAD
# Stage
git add file.txt
git add -A # everything, including deletions, from repo root
git add -u # tracked files only: modifications and deletions
git add . # current directory and below (respects .gitignore)
git add -p # hunk by hunk
git add -N newfile.txt # intent-to-add: diff shows it, content not staged
git add -f build/output.bin # override .gitignore
# Commit
git commit -m "feat: add retry budget"
git commit -m "fix: handle empty payload" -m "Refs: APP-142"
git commit -am "chore: bump deps" # add -u then commit in one step (tracked only)
git commit --amend --no-edit # add staged changes to the last commit, keep message
git commit --amend -m "new message"
git commit --fixup=HEAD # commit that autosquashes later
git commit --allow-empty -m "chore: trigger ci"
git commit --no-verify # skip hooks, for emergencies only
# Unstage and restore
git restore --staged file.txt # unstage, keep working tree changes
git restore file.txt # discard working tree changes
git restore --source=HEAD~2 file.txtgit commit -a is not git add . && git commit: it only sees tracked files, so a new file is silently left out. That is why the pattern "commit -am" misses the file you just created.
Key Comparison Table
| Command | New files | Modifications | Deletions | Scope |
|---|---|---|---|---|
git add -A | Yes | Yes | Yes | Whole repository |
git add -u | No | Yes | Yes | Whole repository |
git add . | Yes | Yes | Yes | Current directory and below |
git add -p | No, only tracked hunks | Yes, selectable | Via prompt | Interactive |
git commit -am | No | Yes | Yes | Whole repository |
| Status code | Meaning | Typical next step |
|---|---|---|
M | Modified and staged | git commit |
M | Modified, not staged | git add -u |
MM | Staged, then modified again | git add to include the newer change |
A | Added to the index | git commit |
D / D | Deleted and staged / deleted, not staged | git add -u to stage the deletion |
?? | Untracked | git add if it belongs, else .gitignore |
UU | Unmerged, conflict present | Resolve, then git add |
Practical Edge Cases & Deep Dives
1. Splitting one file into two commits
Editing a file for two unrelated reasons is normal; committing them together is what makes review and revert painful. git add -p walks hunks and offers y/n/s/e; s splits a hunk, e opens the hunk in an editor for line-level selection.
git add -p src/handler.ts # stage only the bug fix hunks
git commit -m "fix: guard against missing id"
git add -p src/handler.ts # stage the refactor hunks
git commit -m "refactor: extract parse step"If the two changes are on adjacent lines Git cannot split them, and e is the only way through. Editing the hunk means deleting the + lines you do not want; the index keeps the rest while the working tree keeps everything.
2. Amending safely, and what it does to history
--amend builds a new commit from the current index plus the old commit's parent. The old commit stays reachable through HEAD@{1} until garbage collection. Before a push, that is a clean way to fix a message or fold in a forgotten file. After a push to a shared branch, it means every colleague's main diverges.
git add forgotten.ts
git commit --amend --no-edit
git push --force-with-lease origin feature/x # only if the branch is yours--amend does not change the author date; the committer date becomes now. That is why a rebased or amended history shows old author dates with new committer dates.
3. --fixup and --autosquash as the review-friendly path
Instead of amending, commit a fix tagged to the commit it belongs to. The history stays linear and the squash happens at rebase time:
git commit --fixup=3f2a1b9 # message becomes "fixup! <original subject>"
git commit --squash=3f2a1b9 # keeps the message for editing during rebase
GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash origin/mainGIT_SEQUENCE_EDITOR=: accepts the generated todo list without opening an editor, which makes the whole operation scriptable in CI or a pre-push hook.
4. Staging a deletion while keeping the file
git rm --cached removes a path from the index but leaves the file on disk, which is the correct way to start ignoring a file that was committed by mistake. Combined with .gitignore, it stops tracking without deleting:
git rm --cached .env.local
printf '\n.env.local\n' >> .gitignore
git commit -m "chore: stop tracking local env file"The file stays in history. If it contained a secret, rotate the secret; rewriting history does not un-leak it.
5. Pathspec commits that bypass the index
git commit path/to/file commits the working-tree content of those paths even if the index holds something else for them, and it leaves the index untouched. Useful for a one-line hotfix while a large refactor sits staged; confusing when you expected the staged version to be committed:
git commit -m "hotfix: null check" src/parse.ts
git status # the staged refactor is still staged6. status in scripts and submodules
Parsing git status output is fragile; --porcelain=v2 -z is the stable interface. In a repository with submodules, git status reports a modified submodule as a single line regardless of how many files changed inside it, and git add submodule stages the recorded commit, not the contents. Use git submodule foreach when the change must be committed inside each submodule first.
A second scripting trap is the exit code. git status returns 0 whether or not the tree is clean, so a script that checks if git status; then always takes the clean path. Use git diff --quiet and git diff --cached --quiet to test for unstaged and staged changes, and git status --porcelain | grep -q . when untracked files matter too:
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "dirty"
fiCommon Git Operation Mistakes
| Symptom | Cause | Fix |
|---|---|---|
| New file missing from the commit | git commit -am used | git add the file, then commit |
| Commit contains more than intended | git add . from the repo root | git add -p, or git restore --staged the extras |
| Deleted file still tracked | git add . run from a subdirectory, or -u not used | git add -A from the root |
| Amended commit duplicated in the remote | --amend after a push | --force-with-lease, and tell colleagues |
.env committed and pushed | .gitignore added after the file was tracked | git rm --cached, rotate the secret |
| Staged change did not make it into the commit | Pathspec commit used | Check git status for what is still indexed |
| Hook blocks every commit | Pre-commit hook failing on unrelated files | Fix the hook, do not make --no-verify the habit |
| Index looks empty after a merge | Merge state present, index holds the merge | Finish with git commit or git merge --abort |
| Commit went to the wrong branch | switch failed silently in a script | Use git switch -c name and check git branch --show-current |
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit config: Aliases, rerere, and Credential Helpers
Five config scopes decide one value at a time. Aliases are user-level shorthand, rerere records conflict resolutions, and credential helpers choose where your secret lives.
Git Hooks: pre-commit, commit-msg, and Husky Automation
Hooks live in .git/hooks, which a clone never receives, so core.hooksPath and Husky are how they travel. Exit codes abort the operation, and every hook is bypassable.
Git bisect: Binary Search, Automated Scripts, and Bug Hunting
Mark one bad and one good commit and Git halves the range each step. bisect run automates it through exit codes, and monotonicity is the assumption that must hold.
Git ignore: Patterns, Negation, and Untracking Cached Files
Patterns are relative and last match wins, but a negation cannot revive a file inside an excluded directory. Ignore rules never touch tracked files: rm --cached first.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.