将git分支分隔成多个分支以合并为主 [英] Separate git branch into multiple branches to merge to master

查看:160
本文介绍了将git分支分隔成多个分支以合并为主的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队一直在从事大师级的原型分支工作。我现在想要做这件事,将它分成不同的功能分支,并将它们分别合并到主设备中。我看到一些方法可以做到这一点,我也不太喜欢这两种方式:

1 - 在 master 外创建一个新分支Feature_1 。将原型中的代码手动复制到Feature_1。这意味着我必须跟踪我在创建Feature_N并丢失历史记录时所复制的内容。



2 - 创建一个新分支Feature_1,取消的原型即可。以某种方式恢复不属于Feature_1中第一个功能的代码。这样可以避免撒谎(并保留历史记录),但感觉像Feature_N将合并为一团乱七八糟,因为我会告诉主人,当我推送Feature_1时,这些更改已被还原。



我是否缺少更好的方法来做到这一点?

解决方案

@迈克尔的答案是,如果你的提交是单一特性提交,不提供任何其他特性提交的依赖关系,那么答案就是好的。但是,如果您在任何提交中混合使用了这两个功能,您需要交互式重定位。它允许你任意地重新分配变化区块和提交边界,并且它可以跟踪哪些区块尚未提交给当前分支。



如果功能发生变化只是有时候会合并为提交,并且没有跨特性依赖关系,为了让我们的生活变得简单,我的第一次尝试将会是 git rebase -i master prototype ,将混合提交他们分成两个承诺,每个承诺一个承诺,然后像迈克尔的回答一样结束樱桃选择。给定

  A1-B2-C12-D2-E1-F12原型

其中数字表示提交包含代码的哪些功能,对于`git rebase -i master master prototype,您将编辑提交C12和F12, p>

 挑选A1 
挑选B2
编辑C12
挑选D2
挑选E1
编辑F12

(使用每个提交的散列代替这里的说明标记)。



在提交 C12 后,Rebase将停止,您可以 git reset HEAD〜然后 git add --patch 来应用所有的feature-1区块, git commit --amend 来创建提交 C1 位于 C12 的位置,然后 git add -A; git commit``来应用所有剩下的hunks并在它后面创建commit C2`。您将以

  A1-B1-C1-C2-D2-E1-F1-F2 

,然后您可以 git checkout -b feature1 master; git cherry-pick A1 B1 C1 E1 F1 类似于feature2。



在更复杂的情况下,这个方法仍然只有很小的变化。交互式底线比上述可能导致你相信的要好得多,但是到目前为止,找出这个问题的最好方法是坐下来手册页,然后您进入该页面并争取一些快感。做到这一点,它可能很快就会达到这样的地步:作为预先发布仪式做这件事往往比试图让您的实际工作流程在每一个小步骤都可以发布更为方便。


My team has been working in a prototype branch off of master. I now want to take that work, slice it up into different "feature branches", and merge them individually into master. I see a couple ways to do this, neither of which I really like:

1 - Create a new branch, Feature_1, off of master. Manually copy the code from the Prototype to Feature_1. This means I have to keep track of what I've copied when I go to make Feature_N and I lose history.

2 - Create a new branch, Feature_1, off of Prototype. Somehow revert the code that is not part of the first feature in Feature_1. This avoids lying to git (and keeps history), but it feels like Feature_N will be a mess to merge because I will have told master that the changes were reverted when I pushed Feature_1.

Am I missing a nicer way to do this?

解决方案

@Michael's answer is the good stuff if your commits are single-feature commits that don't share dependencies with commits for any other feature. If you've mixed work on the two features in any commit, though, you'll want interactive rebase. It allows you to arbitrarily redistribute change hunks and commit boundaries, and it does keep track of which hunks haven't yet been committed to the current branch.

If the feature changes are just sometimes combined into commits and there are no cross-feature dependencies, to make life easy my first try would be to git rebase -i master prototype, split the commits with mixed hunks into two commits, one for each, and then finish off with the cherry-picks as in Michael's answer. Given

A1-B2-C12-D2-E1-F12    prototype

where the digits signify which feature(s) the commit contains code for, for `git rebase -i master prototype you'd edit commits C12 and F12,

pick A1
pick B2
edit C12
pick D2
pick E1
edit F12

(using each commit's hash instead of its illustrative tag here).

Rebase will stop after committing C12, and you can git reset HEAD~ then git add --patch to apply all the feature-1 hunks, git commit --amend to create commit C1 in C12s place, then git add -A; git commit`` to apply all the remaining hunks and create commitC2` following it. You'll end up with

A1-B1-C1-C2-D2-E1-F1-F2

and you can then git checkout -b feature1 master; git cherry-pick A1 B1 C1 E1 F1 and similarly for feature2.

In more complicated situations this method still works with only very minor changes. Interactive rebase is much better than the above might lead you to believe, but by far the best way to find out about that is to sit down with the manpage while you get in there and scramble some hunks for fun. Do that, and it may soon get to the point where doing this as a prepublication ritual is often enough more convenient than trying to keep your actual workflow publishable at every little step.

这篇关于将git分支分隔成多个分支以合并为主的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆