Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to revert the second to last commit but not the last commit?

Tags:

git

Say I have four commits A-->B-->C-->D but C was terribly misguided. How can I undo C without losing the changes from D? I understand the basic functionality of git-revert and undoing commits, but I can't figure out how (if it is even possible) to undo a specific commit (not the latest) without having to redo the changes that came after it.

like image 269
amflare Avatar asked Jan 11 '17 20:01

amflare


1 Answers

Revert

The git revert command is designed to do exactly this.

git revert <hash for C> 

This will create a new commit which reverses the change in C.

Rewrite History

You can also rewrite history. This is not generally recommended, and should only be used if you have a good reason to actually remove a commit from history (say, if it contains a password or something).

git rebase -i <hash for B> 

In the editor, just delete the line with the hash for C. The usual caveats apply if you have already pushed.

Non-interactive

Technically, all of these options involve some kind of merge resolution which means they cannot truly be non-interactive. The only difference is the resulting history. With git revert you get a history that looks like this:

A -> B -> C -> D -> E                     ^                     +--- reverts C 

With git rebase, you end up with a history that looks like this:

A -> B -----------> E 

You can of course just do git rebase --onto B C, instead of git rebase -i, but this is still an interactive process because you have to manually resolve any merge conflicts that can't be automatically resolved by the merge strategy.

like image 127
Dietrich Epp Avatar answered Oct 05 '22 17:10

Dietrich Epp