The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Before undoing a Git commit, ask whether it has been shared and what you want to keep. For a commit others may already have fetched, use git revert to add a new commit that reverses it. For a local commit, choose a reset or amend command based on whether you want to keep, replace, or discard the work.
First: has the commit been shared?
If the commit is on a shared branch or someone else may have based work on it, preserve the existing history. Use git revert, which records a new commit that reverses the earlier change. Rewriting shared history can disrupt other people’s work.
If the commit exists only in your local history, you can consider moving the branch tip with git reset or replacing the latest commit with git commit --amend. Those operations change the local branch history; do not use them to rewrite a commit already made public.
Second: what should happen to the work?
| Situation | Command | What it does |
|---|---|---|
| The commit is shared, and you want to reverse its effect | git revert <commit> |
Adds a new commit that reverses the earlier patch while preserving history. |
| The latest commit is local; keep its work for editing and recommitting | git reset --soft HEAD^ |
Moves the branch tip back while leaving the work available in the index and working tree. |
| The latest local commit should be replaced with a corrected commit or message | git commit --amend |
Replaces the latest commit. Limit this to unpublished history. |
| Remove the latest local commit and discard its tracked changes | git reset --hard HEAD^ |
Moves the branch tip and resets the index and working tree to match it. Tracked changes can be lost. |
| Restore only selected file contents from the previous commit | git restore --source=HEAD^ path/to/file |
Restores the named path without moving the branch tip. Review the resulting diff before committing. |
The Git Project’s Git User Manual, in “Fixing mistakes,” puts the choice this way: “If you make a commit that you later wish you hadn’t, there are two fundamentally different ways to fix the problem:” The practical distinction is whether to add a reversing commit or change the local branch history.
#1 Best Overall
Undo a commit that was already pushed
Use git revert <commit> for a pushed commit when you need to undo its change without removing it from history. Replace <commit> with the commit identifier. Revert requires a clean working tree; commit or otherwise preserve outstanding work before starting. If later changes overlap with the patch being reversed, Git may stop for conflict resolution.
If the commit is a merge, the command needs an intentional mainline choice: git revert -m <parent-number> <merge-commit>. The parent number determines which parent Git treats as the mainline, and therefore affects which changes are reversed. Do not choose it as a routine extra flag; establish which side of the merge should remain.
Rank #2
Keep the changes but undo the latest local commit
Run git reset --soft HEAD^ when the latest commit is local and you want to edit or recommit its work. The branch tip moves back one commit, while the index and working tree retain the changes. The Git reset manual uses this approach for redoing an incomplete commit or fixing a misspelled commit message.
If you only want to change the message or replace the latest commit with a corrected version, git commit --amend creates a replacement for the latest commit. Because it changes history, amend only a commit that has not been shared.
Discard the latest local commit and its tracked changes
git reset --hard HEAD^ moves the branch tip back and makes the index and working tree match that earlier commit. Any tracked changes removed by this operation may be difficult to recover. Before running it, make sure you do not need those changes and have backed up anything important. Do not use it to remove a commit from history that others may already have fetched.
Restore one file without undoing the whole commit
If only a particular path needs to return to its previous contents, use git restore --source=HEAD^ path/to/file, replacing the path with the file you want to restore. This changes the file contents without moving the branch tip; inspect the diff to confirm the result before making a new commit.
To inspect the earlier version without changing the file, use git show HEAD^:path/to/file. This displays that path’s contents from the previous commit.
If a revert stops for conflicts
Resolve the conflicts in the affected files, stage the resolutions, then run git revert --continue to finish the in-progress revert. If you decide not to apply it, use git revert --abort to stop the revert and return to the state before it began. These sequencer commands are documented in the Git revert manual.
Quick Recap
Best Value
Quick decision path
- Shared or public commit: use
git revert <commit>to add an inverse commit. - Local latest commit, work should stay available: use
git reset --soft HEAD^. - Local latest commit, corrected replacement wanted: use
git commit --amend. - Local latest commit, tracked changes should be discarded: use
git reset --hard HEAD^only if that loss is deliberate. - Only certain files need restoration: use
git restore --source=HEAD^ path/to/file.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




