Archive:
Subtopics:
Comments disabled |
Tue, 11 Aug 2015
Reordering git commits with git-commit-tree
I know, you want to say “Why didn't you just use Say I have commit A, in which feature X does not exist yet. Then in commit C, I implement feature X. But I realize what I really wanted was to have A, then B, in which feature X was implemented but disabled, and then C in which feature X was enabled. The C I want is just like the C that I have, but I don't have the intervening B. I have:
I want:
One way to do this is to use Now someone wants me to use
Now use interactive What I did instead was rather bizarre, using a plumbing command, but worked well. I wrote the code to disable X, and committed it as B, obtaining this:
Now B and C have the files I want in them, but their parents are wrong. That is, the history is in the wrong order, but if the parent of C was B and the parent of B was A, eveything would be perfect. But we can't just change the parents; we have to create a new commit, say B', which has the same files as B but whose parent is A instead of C, and we have to create a new commit C' which has the same files as C but whose parent is B' instead of A. This is what When we use So I did: % git checkout -b XX A Switched to a new branch 'XX' % git commit-tree -p HEAD B^{tree} 10ddf433039fd3cbc5bec0c64970a45add15482e % git reset --hard 10ddf433039fd3cbc5bec0c64970a45add15482e % git commit-tree -p HEAD C^{tree} ce46beb90d4aa4e2c9fe0e2e3d22eea256edceac % git reset --hard ce46beb90d4aa4e2c9fe0e2e3d22eea256edceac The first
says to make a commit whose tree is the same as B's, and whose parent
is the current Then I do the same thing with C:
makes a new commit whose tree is the same as C's, and whose parent is
the current head, which looks just like B. Again it reads a commit
message from standard input, and prints the SHA of the new commit on
the terminal, and again I use Now I have what I want and I only had to edit the files once. To
complete the task I just reset the head of my working branch to
wherever It seems to me that there have been a number of times in the past
when I wanted to do something like reordering commits, and
[ Thanks to Jeremy Leader for suggesting I write this up and to Jeremy Leader and Rik Signes for advance editing. ] [ Addendum 20150813: a followup article ] [ Addendum 20200531: a better way to accomplish the same thing ] [Other articles in category /prog] permanent link |