How to Undo Recent Commits in Git

One of the greatest advantages of Git is that you can undo almost anything. Let’s have a look at how we can undo recent commits in Git.

Watch a course Git & GitHub - The Practical Guide

How to Undo Commits with git reset

The git reset command is used to undo changes.

git reset --soft HEAD~x

Put the corresponding number instead of ~x. For example, if you specify ~3, the command will affect the three latest commits.

When you use this command you just undo the last commits, but the changes you have made will be stored in your working tree, and on your index, so the git commit command will create a commit with the same changes as the commits you "undo" before. Instead of HEAD~x, you can use a commit hash.

git reset --soft <sha1-commit-hash>

How to Undo Commits with git revert

The following command reverts the changes of the given commit and creates a new commit with the reverted changes.

git revert HEAD~x
It's the best method for undoing commits when working with public shared repositories.

How to Undo Commits with git amend

The git commit --amend is used to edit the latest commits. Instead of creating a completely new commit, you can run this command for combining staged changes with the previous commit. To do that, stage your changes and run the following command:

git commit --amend

This command will open your Editor and let you change the message. The new changes will be added to the amended commit.

If you want to change the latest commit message, you can run this command without any staged changes::

git commit --amend -m "This is the correct message"

If you want to add some changes beside changing the commit message, then you can stage your changes as usual and, then, commit again:

git add some/changed/file.ext 
git commit --amend -m "commit message"

If you have pushed your commit before the last amend, then you should force push your changes with --force (suppose, the name of remote is origin, which is by default):

git push origin <branch-name> --force
Instead of --force, you can use --force-with-lease, which is a safer option that will not overwrite the work done by other developers.

How to Undo Commits with git checkout

You can use the git checkout command to checkout to any previous commit with its hash or by using the HEAD~x syntax. The repository will be set in a "detached HEAD" state, and all your new commits will be orphaned when you change branches back to an established branch. "Detached HEAD" warns you that your activity is "detached" from the project’s development. If you have made several commits in the "detached HEAD" state, then, after checking out to another branch, Git will offer you to create a branch to keep those commits.

From the "detached HEAD" state, you can run:

git checkout -b <new-branch-name>

or

git switch -c <new-branch-name>

This will create a new branch named <new-branch-name> and switch to that. The branch will be on a new history timeline where your last commits don’t exist.