Moving Changes to Feature Branches After-the-Fact in Mercurial
I would still love to hear your feedback in the comments below. Enjoy!
At my workplace we use Mercurial. We don’t usually work in feature-branches, but in the default branch instead. This is usually not problematic, but I keep running into the following situation: I started working on feature A (on the default branch), when suddenly I am forced to work on (and ship) feature B instead, for whatever reason. When that happens, I usually say to myself “God damn it! I wish I worked on A in a branch to begin with!”.
Enter hg rebase. Here’s what you do.
First, let’s create a small repo and work on some task:
Here’s how our log looks like now:
Now let’s say we didn’t finish working on task 1 and for some reason we need to put it aside and start working on a different task. We want to move changesets 1 and 2 to a branch for a while. Here’s how to do it:
First, we need to create the branch we want to transfer them to, at the correct point (which is revision 0):
Now we need to create an “empty” commit for the branch. This is needed because as of now the branch doesn’t really exist and it needs to, for the rebase command to work. If you want to transfer the commits to an existing branch (that has some commits already), you don’t need this step.
Now, all there’s left is to perform the rebase command:
--dest option is where to “stick” the changesets and the
--base option defines the changesets that will be moved (it selects the first common ancestor with the revision specified in
--dest and downwards). Here’s how our log looks like now:
We stored task-1 away and we can start working on other stuff in the default branch.Discuss this post at the comment section below.
Follow me on Twitter, Facebook or Google+