Undo the Last Commit and Keep the Changes
git reset --soft HEAD~1The commit disappears from the branch, and its changes stay staged, ready to be committed again with a better message or with a forgotten file added. HEAD~1 means "one commit before the current one"; HEAD~3 goes back three.
The Three Modes
| Mode | Staged changes | Changes in files |
|---|---|---|
--soft | Kept, still staged | Kept |
--mixed | Unstaged | Kept |
--hard | Discarded | Discarded |
All three move the branch to the commit you name. --mixed is the default, so git reset HEAD~1 undoes the last commit and leaves its changes in your files, unstaged.
Look Before You Reset
See the state of your files and the recent commits:
git status
git log --oneline -5Any uncommitted change that git status lists is at risk with --hard. If you want to keep it, commit it or put it aside with git stash first.
Unstage a File
Without a commit, reset only touches the staging area:
git reset config.phpThe file stays as it is on disk; it is no longer part of the next commit. git restore --staged config.php does the same.
Throw Away Changes with --hard
Warning: git reset --hard overwrites tracked files. Uncommitted edits are lost and git reflog cannot bring them back, because they were never committed. Check git status and git stash list first.
git reset --hard HEADThis returns every tracked file to the last commit. New files that were never added stay; git clean removes those. Preview with git clean -n before you run git clean -fd, because cleaned files are not in Git and cannot be recovered with it.
Match the Remote on a Server Checkout
Warning: the last line below discards local edits and any local commits on that branch. Fetch and look first: git status shows edits, and git log --oneline origin/main..HEAD shows commits that exist only in this checkout.
git fetch origin
git status
git log --oneline origin/main..HEAD
git reset --hard origin/mainStash or commit anything you want to keep before the reset. See git pull vs git fetch for a gentler routine.
Reset or Revert?
- git reset: rewrites history. Use it for commits that exist only in your clone.
- git revert: adds a new commit that undoes an old one. Use it for commits you have already pushed.
git revert HEADIf you reset a pushed branch, the next push is rejected, and forcing it with git push --force removes those commits from the remote for everyone. Other people's clones then disagree with the remote. Only force-push a branch nobody else uses, and prefer git push --force-with-lease, which refuses if the remote changed since you last fetched.
Recover with git reflog
A reset does not delete commits at once. The reflog lists every position HEAD has had:
git reflog
git reset --hard HEAD@{1}HEAD@{1} is where HEAD was before the last move. Read the reflog and pick the right line rather than guessing.
- Recoverable: any commit you made or checked out, while the reflog keeps it.
- Not recoverable: uncommitted edits lost to
--hard, and untracked files removed bygit clean.
The reflog also brings back deleted branches; see how to delete a Git branch. For files outside Git, a server snapshot is the safety net, as explained in snapshot vs backup.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations