P. Kabra
Ho

How To Undo Commits

Undo your last N commits with git reset.

GitยทMay 29, 2021

To undo your last commit, use

git reset --soft HEAD~1

Change 1 to the number of commits. To undo the last 3 commits, use git rest --soft HEAD~3.

This git command does not cause any data loss, it only "un-commits" commits.

Example

Have you ever made multiple commits because you forgot to small typo or mistake?

Usually, you'd use git rebase for things like this. However, the commits we want to merge are the latest commits, so it is easier to undo them, then redo them as one commit.

We need to "un-commit" the last 4 commits, so use

git reset --soft HEAD~4

The last 4 commits are now undone:

Now add your new commit:

And you've successfully rewritten history