How do I undo my last Git commit without losing the changes I made?

0votes

Working on a feature branch in a local Git repository. I ran git commit -m "WIP" before finishing the change, and I have NOT pushed the commit anywhere yet.

I want to remove the last commit from the branch history but keep all the edited files exactly as they were, so I can finish the work and make a clean commit.

I'm not sure whether to use git reset --soft, --mixed, --hard, or git revert, and I'm worried the wrong one will permanently delete my changes.

Question comments

Short clarifications and follow-ups about the question itself.

No comments yet.

1 Answer

Tai · AI answer

AI-generated answer

Because the commit is local and has not been pushed, use git reset --soft HEAD~1 to move the branch back one commit while keeping the changes staged. Your edited files remain in the working tree.

First run git status and inspect the last two commits. Creating a temporary backup branch gives you a name pointing to the current commit before the reset. The commands below assume the branch has a parent commit; HEAD~1 does not exist if this is the repository’s very first commit.

The choices differ:

  • --soft: moves the branch pointer; leaves the index and files unchanged. Best fit when you want to finish the same change and recommit.
  • --mixed: also resets the index, so the preserved changes become unstaged.
  • --hard: changes the working tree as well and can discard tracked edits. It is not what you want here.
  • revert: creates a new commit that reverses an earlier change. It is often suitable for shared history, but does not simply leave that change ready for you to finish.

After the soft reset, inspect git status and git diff --cached, finish the work, stage the intended changes and make the replacement commit. Preserve any uncommitted files separately if you need an additional backup: the backup branch protects committed history, not unsaved edits.

See the official git reset documentation. For shared history, follow the team’s merge/rebase workflow rather than rewriting commits other people may use.

Prepared with AI assistance by Tai. Official sources checked 17 October 2026; interfaces and provider policies may change.

  1. Confirm the commit is unpushed and has a parent.
  2. Create a backup branch pointing to the current commit.
  3. Run git reset --soft HEAD~1.
  4. Inspect the staged changes, finish the work and make a new commit.
git status
git log -2 --oneline
git branch backup-before-wip-reset
git reset --soft HEAD~1
git status
git diff --cached

No comments yet.

Your answer

Sign in to share a solution.