Git Reset Last Commit: Soft, Mixed and Hard Reset Explained

Table of Contents

To git reset the last commit, run git reset --soft HEAD~1 to keep your changes staged, git reset HEAD~1 to keep them as unstaged edits, or git reset --hard HEAD~1 to throw them away. All three move your branch back one commit. They differ only in what happens to the work inside that commit.

One rule decides which tool is right. Reset is for commits that exist only on your machine. If teammates may already have pulled the commit, use git revert instead. This guide covers the three reset modes with real output, safety checks to run first, what to do after a push, how to recover from a wrong reset with reflog, and the errors people hit most often.

Quick Answer: How to Git Reset the Last Commit

Git reset moves the current branch pointer to a different commit, and depending on the mode it can also update the staging area and your files. When you reset the last commit, you point the branch at its parent, written as HEAD~1, so the latest commit disappears from the branch history.

GoalCommandCommitStaging areaYour files
Undo the commit, keep changes stagedgit reset --soft HEAD~1RemovedKeeps the changes stagedUntouched
Undo the commit, keep changes unstagedgit reset HEAD~1 (same as --mixed)RemovedClearedEdits kept
Undo the commit, discard the changesgit reset --hard HEAD~1RemovedClearedReset to the previous commit
Undo the commit, refuse to overwrite local editsgit reset --keep HEAD~1RemovedClearedKept if it is safe to do so
Reverse a commit others have pulledgit revert HEADKept, with a new commit that undoes itCleanChanges reversed

HEAD is the commit you are on now, and HEAD~1 is the commit before it. If you are not sure which row you need, start with the soft reset. It is the most forgiving and loses nothing.

What Git Reset Does to HEAD, the Index and Your Working Tree

Git tracks your work in three places, and every reset mode is defined by which of them it changes.

  • HEAD and the branch pointer. The commit your branch currently points to.
  • The index, also called the staging area. The snapshot of what will go into your next commit, built with git add.
  • The working tree. The actual files on disk that you edit.

Reset always moves HEAD. The mode decides whether it also touches the other two.

ModeHEAD movesIndex resetWorking tree reset
--softYesNoNo
--mixed (default)YesYesNo
--hardYesYesYes

Here is what the pointer movement looks like. Before the reset, your history is A, B, C and HEAD is on C:

text

A --- B --- C   <- HEAD (main)

After git reset HEAD~1, the branch points at B. Commit C is no longer on the branch:

text

A --- B   <- HEAD (main)
      C   (no longer on the branch)

Two details are worth knowing. First, HEAD~1 and HEAD^ both mean the first parent of HEAD. With a number, HEAD~3 means three commits back along the first-parent line, which matters if your history contains merges. Second, before a reset runs, Git saves the previous tip of your branch in a reference called ORIG_HEAD. You will use that later for recovery.

Before You Reset the Last Commit: Three Safety Checks

A few seconds of checking prevents most lost work.

  1. Confirm which commit you are removing. Look at the recent history:

bash

git status
git log --oneline -5

The commit at the top of the log is the one HEAD~1 will remove.

  1. Find out whether it has been pushed. If git status says your branch is ahead of its upstream by one commit, the commit exists only locally and a reset is safe. If it matches the remote, other people may have it, so read the section on pushed commits below before you continue.
  2. Create a safety net. Put a branch on your current commit so you can always get back:

bash

git branch backup-before-reset

This protects committed work only. It does not save edits you have not committed. If you have uncommitted changes you want to keep, run git stash first.

Git Reset Last Commit and Keep Changes Staged (–soft)

Use a soft reset when the commit itself was the mistake but the work inside it is fine. Typical reasons are a wrong commit message, a forgotten file, or a plan to combine several commits into one.

bash

git reset --soft HEAD~1

Git moves the branch back one commit and leaves both your files and your staging area exactly as they were. Everything from the removed commit now sits in the index, ready to commit again.

Suppose you committed hello.py and README.md, then noticed a bug in hello.py. After the soft reset, check the state:

bash

git status
git diff --staged

Fix hello.py, stage the correction, and commit again:

bash

git add hello.py
git commit -m "Add hello world example"

The staged changes to README.md are still included, so the replacement commit contains both files.

Git’s own documentation describes a handy pattern for redoing a commit while keeping the old message. Because reset saves the old tip in ORIG_HEAD, you can reuse its message:

bash

git reset --soft HEAD^
# edit your files, then:
git commit -a -c ORIG_HEAD

If you only need to change the last commit message and nothing else, git commit --amend is simpler than a reset:

bash

git commit --amend -m "A clearer commit message"

Git Reset Last Commit and Unstage Changes (–mixed)

Use a mixed reset when you want to keep your edits but decide again what goes into the next commit. This is the default mode, so these two commands do the same thing:

bash

git reset --mixed HEAD~1
git reset HEAD~1

Git moves the branch back and resets the staging area to match, but leaves your files alone. Everything from the removed commit is still in your working tree, just unstaged. Files that were first added by that commit will appear as untracked, so read git status as well as git diff.

bash

git status
git diff

This is the right choice when a commit bundled unrelated changes. After the reset, stage and commit them in separate pieces with interactive staging:

bash

git add -p
git commit -m "Fix login redirect"
git add -p
git commit -m "Update footer links"

Git Reset Last Commit and Discard Changes (–hard)

A hard reset removes the commit and also overwrites your index and working tree to match the previous commit. Use it only when you are certain you want the work gone.

Warning: Before running git reset --hard, check git status and save anything you still need. Edits to tracked files that you have not committed will be discarded, and Git cannot reliably bring them back.

bash

git reset --hard HEAD~1

Afterward, confirm the result:

bash

git status
git log --oneline -5

Here is what is and is not recoverable:

  • The removed commit itself is usually recoverable through the reflog, covered below.
  • Uncommitted edits that the reset overwrote are not reliably recoverable through Git.

Hard reset is not a general cleanup tool for untracked files. It handles tracked paths, although untracked files that are in the way of writing a tracked file can be overwritten, and the exact wording in Git’s documentation has varied across versions. If you want to remove untracked files, use git clean, and preview first:

bash

git clean -nd
git clean -fd

The first command shows what would be deleted. Run the second only after you have read that list.

A common related task is making your local branch match the remote exactly, discarding local commits and edits:

bash

git fetch origin
git reset --hard origin/main

Treat that as a dangerous command. Replace main with your branch name and be sure nothing local is worth keeping.

Git Reset –keep and –merge for a Dirty Working Tree

Two lesser-known modes are useful when your working tree has uncommitted changes.

git reset --keep HEAD~1 moves the branch back but stops with an error instead of overwriting local changes you have not committed. When your tree is dirty and you want to move the pointer without risking your edits, --keep is a safer choice than --hard.

git reset --merge exists mainly to reset unmerged index entries, and it aborts if a file has unstaged changes. Git’s documentation uses it to back out of an unwanted pull while keeping local edits:

bash

git reset --merge ORIG_HEAD

Git Reset vs Git Revert for the Last Commit

Both commands undo a commit, but they treat history very differently.

git resetgit revert
What it doesMoves the branch pointer backAdds a new commit that reverses the old one
Rewrites historyYesNo
Safe on shared branchesNoYes
In git log afterwardThe commit is gone from the branchBoth the original and the revert appear

The decision rule is simple. If the commit exists only on your machine, reset it. If it has been pushed to a branch others use, revert it. Rewriting shared history forces everyone else to repair their local copies, which is why agreeing on history rules is a core part of any team’s collaborative development workflows.

The revert workflow for the last commit looks like this:

  1. Update your branch with git pull.
  2. Run git revert HEAD, or git revert --no-edit HEAD to accept the default message.
  3. If there are conflicts, fix them, stage the files, and run git revert --continue. To cancel, run git revert --abort.
  4. Push the new commit with a normal git push.

Reverting a merge commit is different. It requires choosing a mainline parent with the -m option, so check the documentation before reverting a merge.

What If You Already Pushed the Last Commit?

If you reset a commit that was already pushed, a plain git push will be rejected. Your local branch no longer contains the tip of the remote branch, so Git sees the two histories as diverged. You have two legitimate ways forward.

Option 1: Revert (the default for shared branches). Use git revert HEAD and push the new commit as described above. Nobody has to rewrite anything, and the history stays honest.

Option 2: Reset and force push, on a private branch only. If the branch is yours alone, such as a feature branch nobody else has pulled, you can reset and then update the remote with:

bash

git push --force-with-lease

Prefer this over git push --force. Force-with-lease checks that nobody has pushed to the remote branch since you last fetched, and it fails instead of overwriting their work. Also check whether the branch is protected, and tell your team before rewriting any branch they might use.

Secrets warning: If the commit you are removing contains a password or API key, undoing the commit is not enough. A revert leaves the original content in history, and a reset does not remove copies that were already pushed or cloned. Revoke or rotate the credential first, then handle cleanup separately.

If a bad commit has already reached a live site, roll back first and debug afterward. When you would rather have an expert do the debugging, our team offers WordPress bug fixing and troubleshooting for exactly those situations.

Reset More Than One Commit With HEAD~2, HEAD~3 and Squashing

To undo several commits, put a number after the tilde:

bash

git reset --soft HEAD~3

This moves the branch back three commits and keeps the combined changes staged. It is also a quick way to squash commits. Git’s documentation shows the same idea: a soft reset back five commits followed by a single git commit combines them into one.

bash

git reset --soft HEAD~3
git commit -m "Combine related changes"

Use --mixed instead if you want the changes unstaged, or --hard if you want them gone, and apply the same warnings as before. Because HEAD~3 follows the first-parent line, look at the graph before resetting if your history includes merges:

bash

git log --oneline --graph -10

How to Undo a Git Reset With Reflog and ORIG_HEAD

If you reset to the wrong place, the commit is usually not gone. Git keeps a local log of where HEAD has pointed, called the reflog.

  1. List recent movements:

bash

git reflog

You will see entries like this, with the most recent first:

text

3f2c1a0 HEAD@{0}: reset: moving to HEAD~1
8c4f7b2 HEAD@{1}: commit: add payment retry logic
  1. Find the commit from before the reset. In this example it is 8c4f7b2, one step back at HEAD@{1}.
  2. Restore it. You have three options, from most to least direct:

bash

git reset --hard HEAD@{1}
git reset --hard ORIG_HEAD
git branch recovered-work 8c4f7b2

The first two move your branch back to the old commit. The third is the safest, because it creates a new branch at that commit without touching your current files, so you can inspect it before deciding what to do.

Know the limits. The reflog is local to your repository, entries expire after garbage collection, and edits that were never committed and were discarded by a hard reset are not reliably recoverable. Reflog is a recovery aid, not a backup, and production websites need proper backups of their own, which is the point of a dedicated website backup and security setup.

Common Git Reset Errors and Fixes

fatal: ambiguous argument ‘HEAD~1’: unknown revision

This usually means there is no parent commit to move to. Either the repository has only one commit, or it is a shallow clone that does not include the parent. Check git log --oneline first. If you want to undo the very first commit while keeping your files and staged changes, git update-ref -d HEAD removes the branch reference. If you only need to change that commit, git commit --amend is enough.

Updates were rejected after git reset

Your local history no longer contains the remote tip. For a shared branch, use git revert instead. For a private branch, use git push --force-with-lease.

Untracked files are still there after git reset –hard

Reset only handles tracked files. Preview with git clean -nd, then delete with git clean -fd only when you are sure.

Windows Command Prompt asks for more input after git reset HEAD^

In the Windows Command Prompt, the caret is a special character, so HEAD^ may not reach Git the way you expect and the shell can ask for more input. Use HEAD~1 instead, or put the argument in quotes.

If you are working through other developer tooling problems at the same time, our guide to fixing the Cannot connect to the Docker daemon error covers another common one.

Reset the Last Commit in VS Code and Git GUIs

You do not have to use the command line. Graphical tools offer the same idea under friendlier names, and they usually behave like a soft or mixed reset that keeps your changes.

ToolTypical actionCommand line equivalent
VS CodeSource Control menu, Undo Last CommitA reset that keeps your changes
GitHub DesktopUndo on the latest commit in the History tabA reset that keeps your changes
Command linegit reset --soft HEAD~1The reference behavior

Menu names can differ between versions, so check your tool if an option looks different. As always, undoing a commit in a GUI that has already been pushed raises the same shared-history problem described above.

Best Practices for Resetting Commits Safely

  • Inspect before you act. Run git status and git log --oneline -5 so you know which commit you are removing.
  • Prefer soft or mixed. Use --hard only when you are sure the work should go.
  • Make a backup branch. git branch backup-before-reset costs nothing and saves hours.
  • Never reset shared history. Use git revert for anything others have pulled.
  • Use force-with-lease, not force. And only on branches you own.
  • Tell your team. Rewriting any shared branch without warning creates conflicts for everyone.
  • Commit small and often. Smaller commits mean a reset removes less.

If your team wants help designing safer Git workflows or sorting out a tangled repository, our technical consultation service is a good place to start.

Git Reset Last Commit FAQ

Does git reset delete commits?

It removes commits from the current branch history, but they usually still exist in the repository for a while and can be found through the reflog until garbage collection removes them.

What is the difference between git reset –soft, –mixed and –hard?

Soft moves the branch only and keeps your changes staged. Mixed moves the branch and clears the staging area but keeps your edits. Hard moves the branch and also resets your files to match, discarding tracked changes.

How do I undo a git reset?

Run git reflog, find the commit you were on before the reset, and restore it with git reset --hard HEAD@{1}, git reset --hard ORIG_HEAD, or create a recovery branch with git branch recovered-work <hash>.

Can I reset a commit that is already pushed?

You can, but you will need to force push, which rewrites the remote history. On a shared branch, use git revert instead. On a private branch, use git push --force-with-lease.

Does git reset change the remote repository?

No. Reset changes only your local repository. The remote changes only if you push afterward, and a normal push will be rejected if the histories have diverged.

What is the difference between HEAD~1 and HEAD^?

Both point to the first parent of HEAD, so for a single step back they are equivalent. The tilde form accepts a number, as in HEAD~3, which makes it handier, and it avoids the caret problem in the Windows Command Prompt.

Conclusion

To git reset the last commit, choose the mode by what you want to keep. Use --soft to keep changes staged, --mixed to keep them unstaged, and --hard to discard them. Use git revert when the commit has already been shared, and use git reflog when a reset goes wrong.

Bookmark the quick answer table at the top of this page, check git status before every reset, and keep a backup branch for anything you cannot afford to lose.