Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to revert two commits back and commit only good stuff?

Tags:

I commited something wrong twice. How to revert two commits back and commit only good stuff ?

like image 558
Damir Avatar asked Oct 28 '11 16:10

Damir


People also ask

How do I revert multiple commits at once?

Another way of reverting multiple commits is to use the git reset command.

How do I revert multiple commits in Intellij?

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 .

How do I revert without committing?

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.


2 Answers

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 adds and then git commit).

See Reset Demystified by Scott Chacon for the details on the often confusing git reset comamnd.

like image 154
Emil Sit Avatar answered Sep 18 '22 16:09

Emil Sit


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.

like image 28
Noufal Ibrahim Avatar answered Sep 19 '22 16:09

Noufal Ibrahim