简体   繁体   English

Mercurial:如何创建计划外分支

[英]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 . 完成后,使用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. 之后,与原始分支hg mergehg merge )并从上次中断的地方继续。

    Note that you can bookmark the feature branch with hg bookmark if you do not feel comfortable with leaving your changes unlabeled. 请注意,如果您不满意未标记更改,则可以使用hg bookmark书签为Feature分支添加hg bookmark Also you can easily find back any heads you left behind using hg heads . 同样,您也可以使用hg heads轻松地找回留下的任何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 第四种方法:使用mq补丁

  • You have to have mq extension enabled and initiated for repo 您必须启用mq扩展并启动回购

On hotfix moment you 在修补程序时刻,您

  • convert feature-commits into set of mq-patches ( hg qimport ) 将功能提交转换为mq补丁集( hg qimport
  • Unapply all patches in set ( hg qpop -a ) 取消应用集中的所有补丁( hg qpop -a
  • Code hotfix, commit 代码修补程序,提交
  • ... ...
  • Finish and test hotfix on clean codebase 在干净的代码库上完成并测试修补程序
  • Apply all patches in set ( hg qpush -a ), fix possible conflicts 套用所有补丁( hg qpush -a ),修复可能的冲突
  • Convert patches back to changeset ( hg qfinish ) 将补丁转换回变更集( hg qfinish

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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