Undo my last commit (not pushed yet)

careful beginner #undo#reset#amend#commit

When to use this: You committed too early, with the wrong message, or included a file you should not have, and you have not pushed.

Be careful with this one. Only for commits that are NOT pushed. If you already pushed, use the revert scenario instead.

Before you start

Steps

  1. Option A: undo the commit, keep my work staged

    terminal
    git reset --soft HEAD~1

    Moves the branch back one commit. Your changes stay staged, ready to commit again after fixing whatever was wrong.

  2. Option B: undo the commit, keep my work unstaged

    terminal
    git reset HEAD~1

    Same, but the changes go back to being plain edits (--mixed is the default).

  3. Option C: only fix the message or add a forgotten file

    terminal
    git add forgotten-file.txt
    git commit --amend -m "Better message"

    Replaces the last commit with a corrected one. Omit -m to keep the message.

  4. Option D: throw the commit AND its changes away

    terminal
    git reset --hard HEAD~1

    Destroys the changes in the working tree too. Use only if you are sure you do not want that work.

    This is the dangerous one. See Undo below if you regret it.

How to verify it worked

  • git log --oneline -n 3 no longer shows the commit (A, B, D) or shows the fixed one (C).

How to undo it

Reset too far, or used --hard by mistake? The old commit is still in the reflog for a while:

terminal
git reflog
git reset --hard "HEAD@{1}"

Common errors and fixes

After amending a commit that was already pushed, push is rejected Amend rewrites history. If the commit was pushed, do not amend: use the revert scenario. Only for your own private branch: git push --force-with-lease.

Q&A

What is the difference between --soft, --mixed and --hard?

All three move the branch back. --soft keeps changes staged, --mixed keeps them unstaged, --hard deletes them.

What does HEAD~1 mean?

The commit just before HEAD (the latest commit). HEAD~3 is three commits back.

Practice these as flashcards →

Terms in this scenario

Related scenarios

My notes

Saved in this browser only.