More Git answers

by Danny
Published 2 min read

Undo your last Git commit without losing work. Learn when to use reset --soft, a mixed reset, amend or revert, and why reset --hard is the wrong tool for this job.

The short answer

#

If you have not pushed the commit yet, run git reset --soft HEAD~1. It moves your branch back one commit and leaves every change from that commit staged, ready to commit again. If you have already pushed it, use git revert HEAD instead so you do not rewrite history that other people may have.


_10
# Undo the last commit, keep the changes staged
_10
git reset --soft HEAD~1

Why it happens

#

A commit is a snapshot of your files plus a pointer to its parent commit. A branch name such as main is only a label that points at the latest commit. So "undoing" a commit means one of two things:

  • Moving the branch label back to the parent commit. This is what git reset does. The commit is no longer part of the branch, but your files can stay exactly as they are.
  • Adding a new commit that does the opposite. This is what git revert does. History keeps both commits.

git reset has three modes that decide what happens to your files when the label moves:

  • --soft moves the branch and keeps your changes staged.
  • --mixed (the default) moves the branch and keeps your changes, but unstages them.
  • --hard moves the branch and overwrites your staging area and working files to match the older commit.

Only --hard touches the files on disk, which is why it is the one to avoid when you want to keep your work.

How to fix it

#

Keep the changes staged with reset --soft

Use this when you want to commit the same work again, perhaps with a different message or an extra file.


_10
git reset --soft HEAD~1
_10
git status # your changes are listed under "Changes to be committed"
_10
git commit -m "A better commit message"

Keep the changes unstaged with a mixed reset

Use this when you want to split the work into several smaller commits.


_10
git reset HEAD~1
_10
# the same as: git reset --mixed HEAD~1
_10
_10
git add -p # stage the changes piece by piece
_10
git commit -m "Add validation helpers"
_10
git add .
_10
git commit -m "Use validation in the signup form"

Fix the commit in place with amend

If you only need to change the message or include a file you forgot, you do not need to undo anything. Amend replaces the last commit with a new one.


_10
git add forgotten-file.ts
_10
git commit --amend --no-edit # keep the existing message
_10
_10
git commit --amend -m "Clearer message" # or replace the message

Amend creates a new commit with a new hash, so it counts as rewriting history in the same way a reset does.

Undo a pushed commit with revert

If the commit is already on a shared branch, revert it. Git creates a new commit that reverses the changes, and you can push it normally.


_10
git revert HEAD
_10
git push

The original changes are still in history, so you can bring them back later with git revert on the revert commit, or with git cherry-pick and the original commit hash.

If you already ran reset --hard

Committed work is usually recoverable. git reflog lists where your branch pointed before the reset, and you can move back to it.


_10
git reflog
_10
# a81b4de HEAD@{0}: reset: moving to HEAD~1
_10
# 3f2a1c9 HEAD@{1}: commit: Add signup form
_10
_10
git reset --hard 3f2a1c9

Changes that were never committed are not recorded in the reflog, so --hard can lose them for good.

Common pitfalls

#
  • Running git reset --hard HEAD~1 to undo a commit. It also throws away the changes you meant to keep.
  • Resetting or amending a commit you have already pushed. Your next push is rejected, and forcing it rewrites the branch for everyone else. Use revert on shared branches.
  • Counting wrongly. HEAD~3 moves back three commits. Check with git log --oneline before you reset.
  • Trying HEAD~1 on the very first commit in a repository. There is no parent, so the command fails. git update-ref -d HEAD removes that first commit and leaves your files staged.
  • Using HEAD^ in Windows Command Prompt, where the caret is an escape character. HEAD~1 avoids the problem.
#

Was this resource helpful?