TutreNews Learn

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:

situationcommand
discard edits in a file (not staged)git restore file
unstage a file, keep the editsgit 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 sharedgit 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

  1. The commit was made, but style.css was missing.
  2. Stage the forgotten file.
  3. --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