Browse cheat sheets · Git

Technology: GitCategory: Version ControlDifficulty: IntermediateUpdated

Undo Changes Safely

Undo Git changes safely by choosing the right command for unstaged edits, staged changes, local commits, or shared history without destroying work accidentally.

On this page
  1. Problem
  2. Quick answer
  3. Code / commands
  4. Inspect before undoing
  5. Undo unstaged changes in one tracked file
  6. Undo all unstaged tracked changes
  7. Restore a file from HEAD
  8. Restore a file from an older commit
  9. Restore a file from another branch
  10. Unstage one file but keep its edits
  11. Unstage everything but keep all edits
  12. Unstage selected hunks
  13. Fix the latest local commit message
  14. Add a forgotten file to the latest local commit
  15. Remove the latest local commit but keep changes staged
  16. Remove the latest local commit but keep changes unstaged
  17. Understand reset modes
  18. Avoid git reset --hard for routine cleanup
  19. Undo a shared commit with git revert
  20. Revert the latest shared commit
  21. Revert a contiguous range into one new commit
  22. Abort a conflicted revert
  23. Continue a conflicted revert
  24. Revert a merge commit carefully
  25. Discard staged and unstaged changes for one tracked file
  26. Recover an accidentally reset commit
  27. Recover a deleted local branch
  28. Recover after an accidental rebase or amend
  29. Recover one file without moving the branch
  30. Undo only part of an unstaged file
  31. Inspect a commit before undoing it
  32. Compare what would disappear before moving a branch backward
  33. Reset to a specific local commit while preserving edits
  34. Hard reset to a remote branch only when local work is disposable
  35. Investigate a rejected push instead of deleting local work
  36. Undo a tracked file deletion before commit
  37. Preview untracked-file cleanup
  38. Delete untracked files only when intentional
  39. Be especially careful with ignored files
  40. Use stash when you are unsure whether work should be discarded
  41. Remove one commit from private history
  42. Undo an accidental merge before it is committed
  43. Undo a completed local merge
  44. Undo a cherry-pick in progress
  45. Undo a completed shared cherry-pick
  46. Prefer revert for production rollback commits
  47. Verify after undoing
  48. What the result means
  49. What to look for
  50. You are unsure whether a change is staged
  51. Reset removed a commit you still need
  52. Revert produced conflicts
  53. git restore did not remove an untracked file
  54. Push is rejected after a local reset
  55. Undoing one commit breaks later code
  56. Production notes
  57. Shared history should usually be undone with revert
  58. Reset is primarily a local-history tool
  59. Reflog is local
  60. Untracked files may have no Git recovery path
  61. Backup branches are cheap
  62. A successful revert can still break the application
  63. Common mistakes
  64. 1. Starting with git reset --hard
  65. 2. Using reset on shared history
  66. 3. Using revert when you wanted to rewrite private local history
  67. 4. Assuming git restore can recover untracked files
  68. 5. Running git clean -fd without previewing
  69. 6. Amending a shared commit casually
  70. 7. Reverting a merge without understanding -m
  71. 8. Force-pushing after reset without checking remote changes
  72. 9. Deleting a branch because the commit is “probably elsewhere”
  73. 10. Skipping verification after undo
  74. Next step
  75. Related deep dives

Problem

You changed the wrong file, staged too much, committed something by mistake, or need to undo a change that is already shared.

Git has several commands that sound similar:

restore
reset
revert
checkout
reflog

but they affect different parts of Git state.

The risk is using a destructive command before identifying where the unwanted change exists:

  • only in the working tree
  • in the staging area
  • in a local commit
  • in published/shared history

The goal is to undo the smallest possible scope while preserving recoverability.

Quick answer

Use this decision flow:

Unstaged file edit
→ git restore <file>

Staged by mistake
→ git restore --staged <file>

Latest local commit is wrong, keep changes staged
→ git reset --soft HEAD~1

Latest local commit is wrong, keep changes unstaged
→ git reset HEAD~1

Latest local commit needs a small fix
→ git commit --amend

Shared/pushed commit must be undone
→ git revert <commit>

Think a local commit was lost
→ git reflog

Before undoing anything:

git status
git diff
git diff --cached
git log --oneline --decorate -10

Do not start with:

git reset --hard

unless you intentionally want to discard local tracked changes.

Code / commands

Inspect before undoing

git status
git diff
git diff --cached
git log --oneline --decorate -10

These commands are low-risk and tell you whether the unwanted change is in the working tree, index, or commit history.

Undo unstaged changes in one tracked file

git restore src/app.py

This discards unstaged edits in that file.

Check first:

git diff src/app.py

Undo all unstaged tracked changes

git restore .

This can discard many local edits. Review git status and git diff first.

Restore a file from HEAD

git restore \
  --source=HEAD \
  -- src/app.py

Restore a file from an older commit

git restore \
  --source=HEAD~1 \
  -- src/app.py

This changes the working-tree file only. Review and commit separately if the older version is intentionally being restored.

Restore a file from another branch

git restore \
  --source=origin/main \
  -- src/app.py

Use this when one file should match another branch without moving your current branch.

Unstage one file but keep its edits

git restore --staged src/app.py

The file remains modified in the working tree.

Unstage everything but keep all edits

git restore --staged .

Useful before reorganizing one large staged change into smaller commits.

Unstage selected hunks

git reset -p

Then stage only the correct pieces again:

git add -p
git diff --cached

Fix the latest local commit message

git commit --amend

This creates a new commit ID. Use it freely before the commit is shared.

Add a forgotten file to the latest local commit

git add src/config.py
git commit --amend --no-edit

Do not casually amend commits other people already depend on.

Remove the latest local commit but keep changes staged

git reset --soft HEAD~1

Result:

commit removed from local branch history
changes remain staged

Remove the latest local commit but keep changes unstaged

git reset HEAD~1

Default mode is --mixed.

Result:

commit removed
changes remain in working tree
staging area reset

Understand reset modes

--soft
→ move HEAD/branch
→ keep index
→ keep working tree

--mixed
→ move HEAD/branch
→ reset index
→ keep working tree

--hard
→ move HEAD/branch
→ reset index
→ reset working tree

--hard is the destructive option.

Avoid git reset --hard for routine cleanup

git reset --hard HEAD

This discards tracked staged and unstaged changes.

Before using it:

git status
git diff
git diff --cached

If the current commit may matter, create a safety branch first:

git branch backup-before-reset

Undo a shared commit with git revert

git revert <commit>

Example:

git revert a1b2c3d

This creates a new commit that reverses the selected commit.

Use this for shared history because existing commit IDs remain intact.

Revert the latest shared commit

git revert HEAD

Revert a contiguous range into one new commit

git revert --no-commit <oldest>^..<newest>

Review:

git status
git diff --cached

Then commit:

git commit -m "Revert problematic release changes"

Abort a conflicted revert

git revert --abort

Continue a conflicted revert

Resolve files, then:

git add path/to/file
git revert --continue

Revert a merge commit carefully

git revert -m 1 <merge-commit>

-m selects the mainline parent. Do not guess.

Inspect the merge first:

git show --no-patch --pretty=%P <merge-commit>

A wrong parent can revert the wrong side of the merge.

Discard staged and unstaged changes for one tracked file

git restore \
  --source=HEAD \
  --staged \
  --worktree \
  -- src/app.py

This is destructive for that file.

Recover an accidentally reset commit

git reflog

Example:

e4f5a6b HEAD@{0}: reset: moving to HEAD~1
a1b2c3d HEAD@{1}: commit: Add report engine

Inspect:

git show a1b2c3d

Recover safely:

git switch -c recovery a1b2c3d

Recover a deleted local branch

git reflog
git switch -c recovered-feature <commit>

Deleting a branch usually removes the branch name, not the commit object immediately.

Recover after an accidental rebase or amend

git reflog

Find the previous branch tip, inspect it, and create a temporary recovery branch:

git switch -c before-rewrite <old-commit>

Recover one file without moving the branch

git restore \
  --source=<commit> \
  -- path/to/file

Prefer this over resetting an entire branch just to recover one file.

Undo only part of an unstaged file

git restore -p src/app.py

Git presents hunks and lets you choose what to discard.

Inspect a commit before undoing it

git show <commit>

Summary only:

git show --stat <commit>

Compare what would disappear before moving a branch backward

git diff <commit>..HEAD

Reset to a specific local commit while preserving edits

Keep staged:

git reset --soft <commit>

Keep unstaged:

git reset <commit>

Use only where history rewriting is acceptable.

Hard reset to a remote branch only when local work is disposable

git fetch origin
git reset --hard origin/main

Before doing this:

git status
git log --oneline origin/main..HEAD
git branch backup-before-hard-reset

Do not use this merely because a push was rejected.

Investigate a rejected push instead of deleting local work

git fetch origin
git log \
  --oneline \
  --graph \
  --decorate \
  HEAD \
  origin/<branch>

Understand the divergence first.

Undo a tracked file deletion before commit

If unstaged:

git restore path/to/file

If the deletion is staged:

git restore --staged path/to/file
git restore path/to/file

Preview untracked-file cleanup

git restore cannot recover or remove files Git never tracked.

Preview:

git clean -n

Include directories:

git clean -nd

Delete untracked files only when intentional

git clean -f

Include untracked directories:

git clean -fd

These commands are destructive.

Be especially careful with ignored files

Preview:

git clean -nx

git clean -x includes ignored files and can remove environment files, generated artifacts, caches, and local configuration.

Use stash when you are unsure whether work should be discarded

git stash push -u -m "Safety copy before cleanup"

Then inspect later:

git stash list
git stash show -p stash@{0}

Remove one commit from private history

For local/unshared history:

git rebase -i HEAD~5

Change:

pick a1b2c3d Bad commit

to:

drop a1b2c3d Bad commit

This rewrites affected commit IDs.

Undo an accidental merge before it is committed

git merge --abort

Undo a completed local merge

If unpublished, preserve a recovery pointer first:

git branch backup-before-reset
git reset --hard HEAD~1

If the merge is already shared, revert the merge instead.

Undo a cherry-pick in progress

git cherry-pick --abort

Undo a completed shared cherry-pick

git revert HEAD

Prefer revert for production rollback commits

For a deployed shared change:

git revert <commit>
git push

This preserves an auditable rollback.

Verify after undoing

Run the relevant checks again:

git status
git diff
git diff --cached
git log --oneline --decorate -10

A successful command does not prove you undid the intended thing.

What the result means

Situation Safer command Preserves edits? Rewrites history?
Unstaged file edit git restore <file> No for selected edits No
Staged by mistake git restore --staged <file> Yes No
Latest local commit, keep staged git reset --soft HEAD~1 Yes Yes
Latest local commit, keep unstaged git reset HEAD~1 Yes Yes
Latest local commit needs small fix git commit --amend Yes Yes
Shared commit must be undone git revert <commit> Yes No
Lost local commit git reflog Recovery aid No
Remove untracked files git clean No No

The important distinction is:

restore
→ working tree / index

reset
→ branch + index + possibly working tree

revert
→ new commit that reverses existing shared history

What to look for

You are unsure whether a change is staged

Run:

git status
git diff
git diff --cached

Do not guess.

Reset removed a commit you still need

Run:

git reflog

The old commit may still be recoverable.

Revert produced conflicts

The repository changed since the original commit.

Resolve the intended inverse change carefully, then continue the revert.

git restore did not remove an untracked file

Expected.

Use:

git clean -n

to preview untracked cleanup.

Push is rejected after a local reset

You rewrote branch history.

Do not force push until you confirm whether the remote branch is shared.

Undoing one commit breaks later code

Later commits may depend on it.

Inspect the dependency chain before reverting additional history.

Production notes

Shared history should usually be undone with revert

This preserves:

  • audit trail
  • commit IDs
  • CI/CD references
  • collaborator history

Reset is primarily a local-history tool

Use it for commits that are not broadly shared.

Reflog is local

git reflog records local reference movement.

It is not a remote/global audit log, and recovery depends on local reflog/object retention.

Untracked files may have no Git recovery path

If an untracked file was never committed or stashed and is deleted with git clean, Git cannot normally restore it.

Backup branches are cheap

Before risky operations:

git branch backup-before-change

This can make recovery trivial.

A successful revert can still break the application

Rollback code can conflict with:

  • database migrations
  • API contracts
  • deployment state
  • feature flags
  • later dependent commits

Run tests and validate the release/rollback plan.

Common mistakes

1. Starting with git reset --hard

It can destroy local tracked work unnecessarily.

2. Using reset on shared history

This rewrites branch history and can disrupt collaborators.

3. Using revert when you wanted to rewrite private local history

Revert preserves the original commit and adds an inverse commit.

For unpublished cleanup, reset/rebase may be cleaner.

4. Assuming git restore can recover untracked files

It cannot restore content Git never tracked.

5. Running git clean -fd without previewing

Untracked files can be permanently deleted.

6. Amending a shared commit casually

The commit ID changes.

7. Reverting a merge without understanding -m

You can reverse the wrong side of the merge.

8. Force-pushing after reset without checking remote changes

You can overwrite teammates’ commits.

9. Deleting a branch because the commit is “probably elsewhere”

Verify first.

10. Skipping verification after undo

Git may have done exactly what you asked but not what you intended.

Next step

Use the current state to choose the smallest safe undo.

  • Unstaged edit should disappear → git restore <file>.
  • File was staged by mistake → git restore --staged <file>.
  • Need to split staged work → use patch staging/unstaging.
  • Latest local commit should be rebuilt → use soft or mixed reset.
  • Latest local commit needs a small fix → amend before sharing.
  • Published commit must be reversed → git revert.
  • Shared merge must be undone → inspect merge parents, then revert the merge.
  • Think work disappeared → inspect git reflog.
  • Untracked files need cleanup → preview with git clean -n.
  • Unsure whether work matters → create a backup branch or stash before destructive commands.

← All cheat sheets