How to undo the last 4 commits?

git reset HEAD~4

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

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

The HEAD~4 tells reset to go back 4 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~4

git log --oneline
e57842a (HEAD -> main) 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