How to undo the last 3 commits?

git reset HEAD~3

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

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

The HEAD~3 tells reset to go back 3 commits.

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
e57842a Adding sound.mp3
748ebcf Adding video.mp4
git reset HEAD~3

git log --oneline
9cfff18 (HEAD -> main) Adding file.txt
e57842a Adding sound.mp3
748ebcf Adding video.mp4

See git reset to learn more about it.

Last modified on November 30, 2021.

You might also like