---
title: "How do I undo the last Git commit but keep my changes?"
description: "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."
url: https://www.danny.engineering/answer/git/how-do-i-undo-the-last-git-commit-but-keep-my-changes
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["Git"]
---

# How do I undo the last Git commit but keep my changes?

## 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.


```bash
# Undo the last commit, keep the changes staged
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.


```bash
git reset --soft HEAD~1
git status   # your changes are listed under "Changes to be committed"
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.


```bash
git reset HEAD~1
# the same as: git reset --mixed HEAD~1

git add -p          # stage the changes piece by piece
git commit -m "Add validation helpers"
git add .
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.


```bash
git add forgotten-file.ts
git commit --amend --no-edit            # keep the existing message

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.


```bash
git revert HEAD
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.


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

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.

## Related reading

- [git-reset documentation](https://git-scm.com/docs/git-reset)
- [git-revert documentation](https://git-scm.com/docs/git-revert)
- [git-commit documentation (see --amend)](https://git-scm.com/docs/git-commit)
