Undoing and recovery
Git, what if you forgot to add a file to a commit?
Stage the missing file and amend the last commit when the work is still local and nobody else has pulled the branch yet.
This happens to me often in notes-project. You commit a README change, then notice ideas.txt still listed under Changes not staged for commit.
Check exactly what the last commit contains and what stayed outside it:
git status
git show --stat --oneline HEAD
git diff -- ideas.txt
git status might show:
Changes not staged for commit:
modified: ideas.txt
git show --stat HEAD lists README.md but not ideas.txt. That confirms the file missed the snapshot.
If the missing file belongs to that same change, stage it:
git add ideas.txt
git diff --staged
Read the staged diff before amending. You want one coherent snapshot, not an unrelated file swept in by accident.
Then replace the last commit while keeping its message:
git commit --amend --no-edit
Verify the result:
git show --stat --oneline HEAD
git status
Now git show --stat HEAD should list both README.md and ideas.txt. git status should report a clean working tree.
Amending does not open and modify the old commit. Git creates a new commit from the current index and gives it a new object ID. Compare the IDs:
git log --oneline -1
The hash differs from the one you had before the amend.
Only amend work that is still private to your local branch. If you already ran git push and a teammate pulled, amending rewrites history. Their next pull breaks until everyone coordinates. Make a normal follow-up commit instead.
Compare IDs before and after:
git rev-parse HEAD
The hash changes every time you amend, even with --no-edit.
If you amend twice in a row, you still have one commit at the tip. You do not accumulate fix commits. That is why amend is for polishing the latest snapshot, not for stacking corrections.
Try this: commit a README change without staging ideas.txt, note the commit ID, amend, and confirm the new ID and file list.
For other Git mistakes, I built a free Git recovery tool that gives you step-by-step commands to get out of trouble.
Lesson completed