Evan Martin (evan) wrote in evan_tech,
Evan Martin

rolling back

git revert <HASH> undoes a commit: it commits the inverse of the change done by that commit. Concretely, it's just like a cherry-pick but with a backwards commit. (In git's implementation, the two are implemented by the same file.) How's it work?

The conceptually-simplest way to roll back is to apply an inverse patch, something like:
git diff HASH HASH^ | patch
but that does not work quite right in the presence of complicated operations, like deletes or adds. And if there are conflicts it'd be nice if all of that conflicting information known to git, so it could assist with running merge tools.*

Instead, consider a tree like this, where newer commits are on the right:
Suppose B is the commit we don't want anymore.

Conceptually, you want to go from the state in B back to the state in A, but with all of the stuff in the middle up to HEAD still kept around. Imagine you had created a new branch off of B that contained the state of the world in A, like this:
In that world, the rollback would just be merging that branch into HEAD.

To do this without creating branches, you can just directly run the merge command, which takes a base branch and a set of other branches to merge.
git merge-recursive B -- HEAD A
That says: "using B as the reference, merge as if HEAD and A were our interesting branches.".

More commonly, you've got the hash of a single interesting commit so it's instead:
git merge-recursive H -- HEAD H^

(Note: I'm not certain this is exactly correct; it depends on whether merge-recursive actually looks at the relationship between the branches for doing the merge. I wonder if it may be more correct to use git read-tree -m A B HEAD to read in all three trees into the index and then use git merge-index but both of those are plumbing commands I don't fully understand.)

* You could use git apply instead of patch to help with that, but that is not the subject of this post.
Tags: git

  • megaupload captcha

    Someone make a Javascript-based captcha cracker for megaupload. It's strange to see those captchas again because I idly myself wrote a…

  • zombie ghosd

    I was tickled to discover another IBM developerworks article on one of my abandoned hacks and that both it and its predecessor have been translated…

  • gat, a git clone in haskell

    I've been pretty busy with work lately, so I may as well dump this on the internet before it gets too dusty. Though I think I understand Git decently…

  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.