How to Undo Last Git Commits

Undo Last Commits

Undoing One Single Commit

Undoing the last (local-only) bad Git commit needs this command:

This will move the current branch back to the second last commit and store the committed changes in the Index. To reuse the commit message for the next commit, you will have to remember it.

git reset --soft HEAD~

Undoing Multiple Commits

To undo multiple last (local-only) commits, use this command (using the commit ID of the commit that should become HEAD):

This will store all differences between the previous and the new HEAD into the Index.

git reset --soft <commit>

Considerations

  • If you have already pushed your bad commits, they are now part of the remote repository. In that case, undoing commits might not be advisable, as other users may have already based their commits on them. If you still wish to undo the bad commits, you will need to force-push the current branch.

With SmartGit

Undo last commit with SmartGit

Undoing One Single Commit: To undo just the latest commit, simply invoke Local | Undo Last Commit. This will move the changes from the bad commit into the Index, and the commit message will be set to the bad commit’s message.

Unstage the changes that should not have been committed, or modify the commit message and then commit again.

If you have already pushed the bad commit and have not enabled the option in SmartGit preferences to allow modifying pushed commits, SmartGit will not permit you to undo it.

Undo multiple commits with SmartGit

Undoing Multiple Commits: To undo multiple commits, select the desired new HEAD commit and invoke Local | Reset Advanced with the soft option. This will move the changes between the previous and new HEAD into the Index.

Note, that the options of this dialog determine whether or where the differences should be stored. Therefore, proceed with caution!

Try these Git operations with SmartGit's intuitive interface

Download SmartGit
Illustration of Smart Gitty, the SmartGit mascot -- a grey cartoon cat with glasses, sitting behind a laptop