How to undo the last commit and keep the changes in the staging area?
git reset --soft HEAD~1
To undo the last commit and keep the changes in the staging area use git reset --soft HEAD~1
.
The reset command resets your current HEAD to a specific commit, without creating a new commit for the revert.
The --soft flag keeps your reverted changes in the staging area.
The HEAD~1 tells reset to go back 1 commit from your current state.
git status
On branch main
nothing to commit, working tree clean
git reset --soft HEAD~1
git status
On branch main
Changes to be committed:
(use "git restore --staged..." to unstage)
modified: README.md
deleted: image.png
See git reset to learn more about it.