I commited something wrong twice. How to revert two commits back and commit only good stuff ?
Another way of reverting multiple commits is to use the git reset command.
Revert uncommitted changesIn the Commit tool window Alt+0 , select one or more files that you want to revert, and select Rollback from the context menu, or press Ctrl+Alt+Z .
If you wish to add more changes before committing reverted changes, simply add --no-commit or -n to the revert message. This will prevent git revert from automatically creating a new commit; instead, the revert will appear in the staging index without a commit message.
First, git reset HEAD~2
to discard the top two commits, while leaving your working tree exactly as it is.
Then, simply create a new commit with what you want (e.g., with git add
s and then git commit
).
See Reset Demystified by Scott Chacon for the details on the often confusing git reset
comamnd.
You can do a git rebase -i HEAD~2
and then use the interface to discard the "bad commits" that are there since then and clean up your history. This however alters the project history and if you've already pushed (and others pulled) your changes, there are some social issues to work out.
The other option is to git revert
those changes. Then 2 new commits will get added to the history that makes the fact that you don't want these two commits explicit in the project history. Less clean but easier to work with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With