Git Tips #1: Get there now

Sometimes you’ve played around in the history of a branch, edited some stuff in older commits, perhaps reordered them. You’ve reset’d back a few commits, and now you want to get to the final state you were at initially (or whatever state you want to get to).

Say you’ve kept a branch called “the-end-state” with the working end state you wanted to get back to before you started messing around with the history. You don’t want to merge (merging two alternative histories of the same development wouldn’t make any sense), you don’t want to rebase (it’s about building a new history, not forwarding it on top of the old one). You just want a new commit to bring you to “the-end-state“.

And you want to get there now!

The intuitive way to do this is the following:

$ git diff ..the-end-state > finish.patch
$ patch -p1 < finish.patch
$ git commit -a -s

But why three steps when you can do with just two, using git read-tree to import the tree from “the-end-state” branch into your index (-m to check for unmerged entries) and populate your work tree (-u):

$ git read-tree -m -u the-end-state
$ git commit -a -s

Thanks Junio for the tip on gitml!

Leave a Reply

Your email address will not be published. Required fields are marked *