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

  • blog moved

    As described elsewhere, I've quit LiveJournal. If you're interested in my continuing posts, you should look at one of these (each contains feed…

  • dremel

    They published a paper on Dremel, my favorite previously-unpublished tool from the Google toolchest. Greg Linden discusses it: "[...] it is capable…

  • treemaps

    I finally wrote up my recent adventures in treemapping, complete with nifty clickable visualizations.

  • 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.