Undo last Git commits
With Git command line
Undoing One Single Commit
Undoing the last (local-only) bad Git commit needs this command:
-
git reset --soft HEAD~
to reset the current branch to the second last commit and store the committed changes in the Index
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.
Undoing Multiple Commits
To undo multiple last (local-only) commits, use this command
-
git reset --soft <commit>
(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.
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
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.
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!