How to undo the last 5 commits?

git reset HEAD~5

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

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

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

git log --oneline
748ebcf (HEAD -> main) Adding video.mp4

See git reset to learn more about it.

Last modified on November 30, 2021.

You might also like