Browse cheat sheets · Git
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
- Problem
- Quick answer
- Code / commands
- Inspect before undoing
- Undo unstaged changes in one tracked file
- Undo all unstaged tracked changes
- Restore a file from HEAD
- Restore a file from an older commit
- Restore a file from another branch
- Unstage one file but keep its edits
- Unstage everything but keep all edits
- Unstage selected hunks
- Fix the latest local commit message
- Add a forgotten file to the latest local commit
- Remove the latest local commit but keep changes staged
- Remove the latest local commit but keep changes unstaged
- Understand reset modes
- Avoid git reset --hard for routine cleanup
- Undo a shared commit with git revert
- Revert the latest shared commit
- Revert a contiguous range into one new commit
- Abort a conflicted revert
- Continue a conflicted revert
- Revert a merge commit carefully
- Discard staged and unstaged changes for one tracked file
- Recover an accidentally reset commit
- Recover a deleted local branch
- Recover after an accidental rebase or amend
- Recover one file without moving the branch
- Undo only part of an unstaged file
- Inspect a commit before undoing it
- Compare what would disappear before moving a branch backward
- Reset to a specific local commit while preserving edits
- Hard reset to a remote branch only when local work is disposable
- Investigate a rejected push instead of deleting local work
- Undo a tracked file deletion before commit
- Preview untracked-file cleanup
- Delete untracked files only when intentional
- Be especially careful with ignored files
- Use stash when you are unsure whether work should be discarded
- Remove one commit from private history
- Undo an accidental merge before it is committed
- Undo a completed local merge
- Undo a cherry-pick in progress
- Undo a completed shared cherry-pick
- Prefer revert for production rollback commits
- Verify after undoing
- What the result means
- What to look for
- You are unsure whether a change is staged
- Reset removed a commit you still need
- Revert produced conflicts
- git restore did not remove an untracked file
- Push is rejected after a local reset
- Undoing one commit breaks later code
- Production notes
- Shared history should usually be undone with revert
- Reset is primarily a local-history tool
- Reflog is local
- Untracked files may have no Git recovery path
- Backup branches are cheap
- A successful revert can still break the application
- Common mistakes
- 1. Starting with git reset --hard
- 2. Using reset on shared history
- 3. Using revert when you wanted to rewrite private local history
- 4. Assuming git restore can recover untracked files
- 5. Running git clean -fd without previewing
- 6. Amending a shared commit casually
- 7. Reverting a merge without understanding -m
- 8. Force-pushing after reset without checking remote changes
- 9. Deleting a branch because the commit is “probably elsewhere”
- 10. Skipping verification after undo
- Next step
- 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.
Was this helpful?
What could be better?
Thanks — that helps me improve Javi Builds.
Stay in the loop
Get the Javi Builds updates you actually care about.
Check your inbox
I sent you a confirmation link. Your subscription will be active after you confirm it.