Reset & Revert

You might say

A shared commit added a debug entry. How can I undo that and keep later work?

Reset moves the current branch and handles changes by mode; revert adds a commit that offsets an earlier changeSuppose B adjusts navigation and C accidentally adds a debug entry. If C is only local, reset can move the branch pointer; --soft keeps changes staged, the default --mixed resets the index but leaves working-tree files, and --hard also replaces tracked working-tree files. If C is on a shared branch and teammates may have based work on it, revert usually appends a commit that offsets C while preserving later history. If later edits overlap with C, revert may also require conflict resolution.
Know first
ResetRevert
git revert a1b2c3d

Local B adjusts navigation, and C accidentally adds a debug entry; shared history later includes teammate D. Reset moves the branch pointer and updates the index or working tree by mode. Revert appends E after D, offsets C, and keeps history. Inspect the commit diffs and working-tree status before choosing.

When to use it

  • For an unshared commit, consider reset only after checking staged and working-tree changes
    $ git reset --hard HEAD~1HEAD is now at 3f9c2ab Adjust navigation spacing
  • Keep changes but redo the commit with --soft
    $ git reset --soft HEAD~1Changes stay staged; restage and commit after tidying up
  • For a shared commit, consider revert and check for conflicts
    $ git revert a1b2c3d[main 9e8f7a6] Revert "Add debug code by mistake"
  • Before --hard, inspect tracked-file changes it will replace in the index and working tree
    git status shows uncommitted changesreset --hard discards these changes too

When NOT to use it

  • Reset a shared branch and force push without coordinating with teammates
    Force pushing after resetting a pushed commit splits teammates' copiesDo not rewrite shared history; use revert
  • Run reset --hard without checking which tracked-file changes it will replace
    Two hours of uncommitted work disappear with reset --hardRun git status first to confirm
  • Revert without reviewing later edits that may overlap and need conflict resolution
    Reverted a teammate's commit without telling themThey keep building on reverted work, creating needless conflicts
  • Skip verification after undoing
    reset ✓→The page errors
Anatomy
HEAD
Back to here
Reset moves the branch pointer so the commit is no longer in that branch's later history; whether its changes remain depends on the mode. Revert keeps the original commit and adds an offsetting commit.
Variants
--soft
git reset --soft HEAD~1
Undo the commit but keep the changes to restage them
--hard
git reset --hard HEAD~1
Move the branch to the previous commit and replace later tracked-file index and working-tree state; check first
revert
git revert a1b2c3d
Undo a pushed commit without rewriting shared history
Typical use cases
Local commits B and C, including a debug entry
Local main · C has not been shared
a1b2c3d (HEAD → main) Add debug entry by mistake · C 3f9c2ab Adjust navigation spacing · B 81a0b11 Create page
UndoC · debug entry
KeepB · navigation update

Use git log / git show to confirm the target commit and its changes first.

Reset --soft moves the branch but keeps staged changes
git reset --soft HEAD~1
HEAD -> 3f9c2ab Adjust navigation spacing
Branch pointerMoves to B
IndexKeeps C's changes staged
Working filesKeep C's changes

Reset updates the branch, index, and working tree according to its mode. --hard replaces tracked working-tree changes, so check status first.

Revert appends an offsetting commit to shared history
Shared main · teammate D was committed after C
3f9c2ab B · Adjust navigation spacing a1b2c3d C · Add debug entry by mistake 9b8c7d6 D · Teammate updates help copy (HEAD, origin/main)
Offset Cgit revert a1b2c3d
New EAppend Revert C after D

This does not move shared C or the teammate's D. If later edits overlap with C, revert may also require manual conflict resolution.

Recheck navigation and teammate changes remain
Page recheck · keep B and D, remove C's effect
History after the merge
BNavigation update remains
COriginal commit stays in history
DTeammate's help copy remains
EOffsets C's debug entry
my-first-page

My portfolio

Navigation and help copy remain; the debug entry is gone.

Run the page and inspect affected paths. If revert overlaps later work, resolve the conflict too.

Further reading