简体   繁体   中英

Mercurial: How can I create an unplanned branch

My use case is this:

I am working on a new feature and I have several commits on that feature. Since it was a minor feature, I didn't even consider doing the feature in a feature branch.

However. Now my boss comes along and tells me to fix a bug on the same branch that I am working on (default).

To fix that I'd like to create a feature branch for my feature, push all my existing (unpushed) commits into that branch.

So I'd like to create a branch just before my first commit and then somehow move all my commits to that branch.

How can I do this?

For this situation you can fix it by rebasing (which may need enabling in your configuration).

On your branch, update to the revision before the change-sets you want to move:

hg up -r<revison>

This assumes contiguous revisions need moving.

Create a new branch:

hg branch "TempWork"

Put a dummy commit onto it in order to get a new revision:

hg commit -m"New Branch"

Then perform the rebase from the first of the change-sets you want to move (it moves descendants automatically) and specify the new branch revision as the destination:

hg rebase -s<base revision> -d<new branch revision>

Then update back onto your main-line branch.

There's two ways to approach this, depending on your preference:

  1. In a new repository.

    Make a new clone of your repository, and do the bug fix you need to make there. Then push it to the main repository when you're done, and continue where you left off in the original repository. Pull and merge to get the new changes as usual.

  2. In the existing repository.

    Update to the changeset before your local changes, and just start fixing and committing there. This creates a new anonymous branch. When you're done, push using push -r . , this will only push the changes that are included in the working copy. After this, merge with your original branch ( hg merge ) and continue where you left off.

    Note that you can bookmark the feature branch with hg bookmark if you do not feel comfortable with leaving your changes unlabeled. Also you can easily find back any heads you left behind using hg heads .

Personally I prefer to work in a new clean clone, as you don't need to worry about branching and where to leave uncommitted changes. However if your project setup is complicated it may be more convenient to reuse the existing repo.

Fourth method: Using mq-patches

  • You have to have mq extension enabled and initiated for repo

On hotfix moment you

  • convert feature-commits into set of mq-patches ( hg qimport )
  • Unapply all patches in set ( hg qpop -a )
  • Code hotfix, commit
  • ...
  • Finish and test hotfix on clean codebase
  • Apply all patches in set ( hg qpush -a ), fix possible conflicts
  • Convert patches back to changeset ( hg qfinish )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM