How to undo the last N commits?

git reset HEAD~N

To undo the last N commits use git reset HEAD~N

The reset command resets your current HEAD to a specific commit, without creating a new commit for the revert.

The HEAD~N tells reset to go back N commits, where N is the number of commits you want to revert.

With no additional flags the reverted changes are kept.

Layer 1
Terminal Example
git log --oneline
63592f4 (HEAD -> main) Update README.md
ca16879 Adding README.md
8781cfd Adding image.png
9cfff18 Adding file.txt
git reset HEAD~2

git log --oneline
8781cfd (HEAD -> main) Adding image.png
9cfff18 Adding file.txt

See git reset to learn more about it.

Last modified on November 30, 2021.

You might also like