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.

How to Undo Commits with git reset Command

The git reset command is used to undo changes.

git reset --soft HEAD~x (or git reset --soft commit hash).
git reset --soft HEAD~x

Put the corresponding number instead of ~x. For example, if you specify ~3, the command will affect the third commit from the bottom. If you do not determine ~x, git reset --soft HEAD will change your latest commit.

The git reset --soft HEAD command rewinds your current HEAD to the specified revision. When you use this command you just undo the last commit, 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 commit you "undo" before.

How to Undo Commits with the git revert command

The git revert HEAD~x (git reset --hard commit hash) command reverts the changes specified by the last commit in HEAD and creates a new commit with the reverted changes.

git revert HEAD ~x

For example, if your history is as follows:

git log --oneline 
872fa7e try new  feature
a1e8fb5 changes to index.html file

Although 872fa7e commit still exists in history, it is technically “undone”.

It's the best method for undoing commits when working with public shared repositories.

How to Undo Commits with the git amend command

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 make changes, stage them, and commit again to 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 make changes in the commit message, you "commit again" - without any staged changes but with the correct message:

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

If there is anything else to add to that last commit, stage changes as usual and then commit again:

git add some/changed/file.ext 
git commit --amend -m "commit message"
For changing already pushed commit use the following command:
git commit --amend git push origin master --force

The --force feature forces pushing your commit after making changes to update the remote history.

Be careful with running this command, as it will very likely prevent others from syncing with the repository if they already pulled a copy.

How to Undo Commits with the git checkout command

Checking out the previous commit with the git checkout command you put the repository in a state before your last commit (the one you want to undo) happened. 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. Orphaned commits are being deleted automatically by Git garbage collector. “Detached HEAD” warns you that your activity is “detached” from the project’s development. Developing a feature in a “detached HEAD” state, no branch will allow you to get back to it.

From the detached HEAD state, you can run:

git checkout -b new_branch_without_my_last_commit

This will create a new branch named <new_branch_without_my_last_commit> and switch to that state. The repository will be on a new history timeline where your last commit doesn’t exist.

This method is inappropriate in case if the previous branch that you need is your master branch.

Do you find this helpful?

Related articles