October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

Git Undo Last Commit: Keep Changes, Stay Safe

Use git reset --soft HEAD~1 to undo the latest commit and keep its changes staged, or git reset HEAD~1 to keep them unstaged. For pushed commits, use git revert HEAD instead.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To undo your latest commit and keep its changes staged, run git reset --soft HEAD~1. To undo it and keep the changes in your files but unstaged, run git reset HEAD~1. Both commands leave your files alone. The one command to avoid when you want to keep work is git reset --hard.

Check the commit before you reset

Both reset commands move the current branch backward by one commit, so run these checks first:

As an Amazon Associate I earn from qualifying purchases.

  1. Run git status to see whether you have staged, unstaged, or untracked changes you also need to keep.
  2. Run git log --oneline -3 to confirm that the commit you want to undo is the tip of the current branch (the top line).
  3. Confirm that the commit has not been pushed to a shared branch. If it has, skip to the revert section.

HEAD~1 means the parent of the current commit. If the commit you want to undo is a merge commit, HEAD~1 points to its first parent. Check git log --graph --oneline if the history is complicated. If the tip is the repository’s first commit, it has no parent, and HEAD~1 will fail.

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

Which command keeps what

The three reset modes differ only in how far they reach beyond the branch pointer. The table below summarizes the Git project’s git-reset documentation (version 2.53.0 page) alongside the related commands.

Command Moves branch tip Index (staged changes) Working tree files Best use
git reset --soft HEAD~1 Back one commit Unchanged, so the commit’s changes stay staged Unchanged Recommit the same work with a different message or a different grouping of files
git reset HEAD~1 (default --mixed) Back one commit Reset to the new tip, so the changes become unstaged Unchanged Reconsider what to stage before committing again
git reset --hard HEAD~1 Back one commit Reset to the target commit Reset to the target commit, which discards uncommitted edits to tracked files Discarding the commit and its work. Not a preservation command
git commit --amend Replaced by a new tip commit Staged changes are folded into the new commit Unchanged Fixing the message or adding a missed file to the latest commit
git revert HEAD Advances by a new commit that reverses the change Changed by the reversal Changed by the reversal Undoing a commit that others may already have

The core distinction is between --soft and the default mode. Both move the branch back, but --soft keeps the index as it was, while the default mode resets the index to match the new tip.

Keep the changes staged with –soft

Use this when the commit was almost right and you want to change the commit message or regroup the files before committing again.

  1. Run git reset --soft HEAD~1.
  2. Run git status. The files from the undone commit should appear under “Changes to be committed.”
  3. Run git diff --cached to review exactly what will go into the next commit.
  4. Run git commit with a new message, or run git reset HEAD <file> to unstage specific files before committing.

If you already had staged changes before the reset, they will be combined with the undone commit’s changes in the next commit. Check step 3 to be sure that is what you want.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Keep the changes unstaged with the default mode

Use git reset HEAD~1 when you want to revisit the work and decide what to stage. The branch moves back and the index is reset, but the files keep their content.

  1. Run git reset HEAD~1.
  2. Run git status. Modified files appear under “Changes not staged for commit.”
  3. Files that the undone commit added appear under “Untracked files” because the index no longer contains them.
  4. Stage what you want with git add and commit again, or use git add -p to stage individual hunks.

If the commit is already shared, use revert

Once a commit has been pushed and others may have based work on it, rewriting the branch with a reset can cause problems for collaborators. The Git project’s git-commit documentation makes the point directly: you should understand the implications of rewriting history if you amend a commit that has already been published. The same caution applies to resets on shared branches.

git revert HEAD records a new commit that reverses the latest commit’s changes. The existing history stays intact. The git-revert documentation describes this as recording new commits that undo the changes the earlier patches introduce.

  1. Run git status. Revert requires a clean working tree, so commit or stash anything pending first. For example, git stash push -u stashes tracked and untracked changes.
  2. Run git revert HEAD. Git opens your editor with a default message for the reversing commit. Use git revert --no-edit HEAD to accept that message without opening the editor.
  3. Run git log --oneline -2 to confirm the new reversing commit is on top of the original.
  4. Push normally. No force push is needed.

Commands to avoid for this goal

git reset --hard HEAD~1 resets the index and working tree to the target commit. Uncommitted edits to tracked files are discarded. The reset documentation also warns that it can overwrite untracked files. Avoid it unless you have intentionally decided to throw the work away.

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

git commit --amend is for correcting the latest commit, not for removing it. Use it for a message fix or a forgotten file on a local commit that has not been shared.

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

Recover if you reset by mistake

The reset documentation states that reset saves the previous branch tip to ORIG_HEAD. If you realize right away that you did not mean to reset, running git reset --soft ORIG_HEAD returns the branch to the commit you had before. The Git documentation describes ORIG_HEAD as a reference for the previous tip, but it is not a guarantee that works in every situation, because later operations can overwrite it.

For older states, git reflog lists where HEAD has pointed recently. Find the hash of the commit you want, then run git reset --soft <hash> to restore it. The Pro Git book’s Reset Demystified section explains how these reset modes map onto the index and working tree.

Troubleshooting

  • HEAD~1 fails: the tip is the first commit in the repository, which has no parent. Use git update-ref -d HEAD only if you intend to start the branch history over, or create a new commit on top instead.
  • The staged files are not what you expected: run git diff --cached and git diff to see the index and the working tree separately.
  • Revert refuses to run: the working tree is not clean. Commit or stash your changes, then run revert again.
  • You pushed and then reset: do not force push without coordinating with collaborators. Use git revert for the fix, or agree on a force push with everyone who has the branch.

The official reference pages are the git-reset documentation (version 2.53.0), the git-commit documentation, the git-revert documentation, and the Git user manual’s “Fixing mistakes” section. Behavior details can vary slightly between Git versions, so check git --version and the matching documentation if a command behaves differently on your machine.

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

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
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.