git-from-the-bottom-up

Interactive rebasing

When rebase was run above, it automatically rewrote all the commits from W to Z in order to rebase the Z branch onto the D commit (i.e., the head commit of the D branch). You can, however, take complete control over how this rewriting is done. If you supply the -i option to rebase, it will pop you into an editing buffer where you can choose what should be done for every commit in the local Z branch:

The power of this command is hard to appreciate at first, but it grants you virtually unlimited control over the shape of any branch. You can use it to:

I recommend reading the man page for rebase at this point, as it contains several good examples how the true power of this beast may be unleashed. To give you one last taste of how potent a tool this is, consider the following scenario and what you’d do if one day you wanted to migrate the secondary branch L to become the new head of Z:

Rebasing Multiple Branches Part 1

The picture reads: we have our main-line of development, D, which three commits ago was branched to begin speculative development on Z. At some point in the middle of all this, back when C and X were the heads of their respective branches, we decided to begin another speculation which finally produced L. Now we’ve found that L’s code is good, but not quite good enough to merge back over to the main-line, so we decide to move those changes over to the development branch Z, making it look as though we’d done them all on one branch after all. Oh, and while we’re at it, we want to edit J real quick to change the copyright date, since we forgot it was 2008 when we made the change! Here are the commands needed to untangle this knot:

$ git checkout L
$ git rebase -i Z

After resolving whatever conflicts emerge, I now have this repository:

Rebasing Multiple Branches Part 2

Note: In the diagram above, the commit I' represents the rebased version of commit I. The original I was based on commit C, and after rebasing onto Z, I' now contains the same changes but applied on top of Z (which comes after X in the commit history).

As you can see, when it comes to local development, rebasing gives you unlimited control over how your commits appear in the repository.