Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

Undo a Git Commit: Two Questions Before You Type Anything

Before undoing a Git commit, check whether it has been shared and decide whether to keep, replace, or discard its work. Then choose revert, reset, amend, or restore.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick decision path

  1. Shared or public commit: use git revert <commit> to add an inverse commit.
  2. Local latest commit, work should stay available: use git reset --soft HEAD^.
  3. Local latest commit, corrected replacement wanted: use git commit --amend.
  4. Local latest commit, tracked changes should be discarded: use git reset --hard HEAD^ only if that loss is deliberate.
  5. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.