Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Use git revert when a commit may already be on a shared remote or other people may have built on it. Revert adds a new commit that reverses the earlier change, so the original commit stays in the branch history. Use git reset when you are cleaning up local commits that nobody else has seen. Reset moves the current branch pointer backward, which rewrites the branch’s recent history. The reset mode then decides what happens to your staged changes and working files, and --hard is the mode that can destroy work.
What each command does to history
git revert: adds a corrective commit
git revert <commit> applies the inverse of a commit’s patch and, by default, records the result as a new commit. The bad commit is still in the log, and the branch moves forward with the correction. Because no existing commit is altered, collaborators who already fetched the original commit can pull the revert without any special handling. This is the reason revert is the safe choice for shared branches.
As an Amazon Associate I earn from qualifying purchases.
git reset: moves the branch pointer
git reset [mode] <commit> sets the current branch’s HEAD to the target commit. It does not create a reversing commit. Depending on the mode, it also changes the index (the staging area) and the files in your working tree. The commits that were removed from the branch are no longer reachable from it, which is exactly what makes reset useful for tidying up local work and exactly what makes it risky once those commits have been pushed.
Reset modes: what each one changes
Think of Git as three layers: the branch pointer (HEAD), the index, and the working tree. The mode controls how far a reset reaches into the lower two layers.
#1 Best Overall
| Command | Branch tip (HEAD) | Index (staging area) | Working tree files | Typical use |
|---|---|---|---|---|
git reset --soft <target> |
Moves to target | Unchanged | Unchanged | Undo commits but keep every change staged, ready to recommit |
git reset --mixed <target> (default when no mode is given) |
Moves to target | Set to match target | Unchanged | Undo commits and leave the changes in the working tree, unstaged |
git reset --hard <target> |
Moves to target | Set to match target | Overwritten to match target | Discard tracked changes and commits; can destroy uncommitted work |
The official git-reset documentation says that --hard overwrites files and directories with their versions in the target and may also overwrite untracked files. Check git status and save anything you want before running it.
Which command to use in common situations
| Situation | Command | What to watch for |
|---|---|---|
| Undo a commit that is already pushed or shared | git revert <commit> |
Adds a new commit; no history is replaced for collaborators |
| Undo your last local commit and keep its changes staged | git reset --soft HEAD~1 |
Changes stay in the index, so the next commit contains them |
| Undo your last local commit and keep its changes unstaged | git reset --mixed HEAD~1 |
Files keep your edits, but you must stage them again |
| Undo your last local commit and throw away its tracked changes | git reset --hard HEAD~1 |
Removes the commit and overwrites matching working files |
| Unstage one file but keep your edits | git restore --staged <path> |
Touches only the index; working-tree edits remain |
| Undo edits to a file that has not been committed | git restore <path> or git restore --source=<commit> <path> |
Not a commit undo; choose the source version deliberately |
| Revert a merge commit | git revert -m <parent-number> <merge> |
The parent number selects the mainline to keep; check the resulting patch |
Undoing your last local commit
This procedure applies only to commits that exist solely on your machine. If the commit has been pushed, go to the revert section below.
Rank #2
- Run
git log --oneline -5and confirm the commit at the top is the one you want to remove. - Run
git status. Note any uncommitted edits, because they will be affected by the reset mode you choose. - Choose the mode. For most people who want to rewrite the commit and keep the code, run
git reset --soft HEAD~1. Use--mixedif you want to restage selectively. - Run
git statusagain. With--soft, the former commit’s changes should appear under “Changes to be committed.” - Commit again with
git commit, or adjust the files first and then commit.
Pushing a branch whose history you have rewritten requires a force update, so use git push --force-with-lease rather than --force, and only after the people sharing the branch know about the change.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsUnstaging a file without losing edits
git restore --staged <path> copies the version of that file from HEAD into the index, so the file is no longer staged. Your working-tree copy is untouched. The older form git reset <path> does the same thing for this purpose, and the official reset documentation describes it as the equivalent of the modern command. Neither form moves the branch pointer.
Undoing edits to an uncommitted file is a separate task. git restore <path> takes the version from the index, which discards your unstaged edits in that file. git restore --source=<commit> <path> takes the file from a specific commit. Confirm the source before you run either one, because the change cannot be reversed from Git’s history once you overwrite the file.
Undoing a commit that has been pushed
Revert is the right tool here. The steps are:
- Run
git revert <commit>. Git opens your editor for the new commit message; the default message names the reverted commit. - Review the change with
git show HEADto confirm it removes what you intended. - Push normally with
git push. No force update is needed.
Git’s revert command requires a clean working tree by default. Commit or stash your uncommitted changes first. If the revert conflicts, Git stops and waits for you. Resolve the conflicts, stage the files, and run git revert --continue. To abandon the operation, run git revert --abort. git revert --skip drops the current commit from the sequence and is rarely needed for a single revert.
Reverting a merge commit
A merge commit has more than one parent, so Git cannot know which side should be treated as the baseline. The -m option names that baseline by parent number. Parent 1 is the branch you were on when you ran the merge, which is the usual choice, so git revert -m 1 <merge> removes the changes that came in from the merged branch. Inspect the new commit with git show before pushing.
A reverted merge has a side effect. Git now treats the merged branch’s changes as already applied and may skip them if you merge that branch again later. Reverting the revert is the usual recovery path, and it is worth discussing with your team before you do it.
Best Value
Before you run reset –hard
- Run
git statusand confirm which files would be overwritten. - Stash changes you want to keep, including untracked files, with
git stash push -u. - Create a branch as a bookmark with
git branch backup/before-reset, so the current commits stay reachable after the reset. - Run the reset only after the checks above pass.
Recovering after a bad reset
Git records where HEAD has been in the reflog. Run git reflog to find the commit hash from before the reset, then create a branch at that hash with git branch recovered-work <hash>. Starting with a branch is safer than resetting straight back, because it does not overwrite anything else. After a reset, Git also stores the previous tip in ORIG_HEAD, which is useful for a quick undo of the most recent reset.
The reflog and ORIG_HEAD cover committed states only. Uncommitted edits that a --hard reset or a restore overwrote are not stored in Git’s history and generally cannot be recovered through these tools. That is why the backup steps above come before the destructive command, not after it.
Quick Recap
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.
Free tools Windows power users keep installed
One-click scans. No signup required.




