Undoing and ignoring
Undoing changes safely
Goal: Pick the right undo command for each situation · 10 min
Which command you need depends on where the change is:
| situation | command |
|---|---|
| discard edits in a file (not staged) | git restore file |
| unstage a file, keep the edits | git restore --staged file |
| fix the message / add a forgotten file to the last commit (not yet pushed) | git commit --amend |
| undo a commit that is already shared | git revert <hash> |
| move the branch back, discarding commits (local only) | git reset --hard <hash> |
git revert creates a new commit that reverses an old one — history stays intact, so it is safe on shared branches. git reset --hard and git restore throw away uncommitted work permanently. Rule: never rewrite (amend/reset) commits that others already pulled.
Worked example: Forgot a file in the last commit
- The commit was made, but style.css was missing.
- Stage the forgotten file.
- --amend replaces the last commit; --no-edit keeps its message.
git commit -m "Add contact page"
git add style.css
git commit --amend --no-edit