How to undo the last commit?

git reset HEAD~1

To undo your last commit use git reset HEAD~1. The reset command resets your current HEAD to a specific commit, without creating a new commit for the revert.

The HEAD~1 tells reset to go back 1 commit from your current state.

With no additional flags the reverted changes are kept.

Layer 1
Terminal Example
git status
On branch main
nothing to commit, working tree clean
git reset HEAD~1

git status
On branch main
Untracked files:
  (use "git add ..." to include in what will be committed)
    image.png

nothing added to commit but untracked files present (use "git add" to track)

See git reset to learn more about it.

Last modified on November 30, 2021.

You might also like