简体   繁体   English

将git分支分成多个分支合并到master

[英]Separate git branch into multiple branches to merge to master

My team has been working in a prototype branch off of master.我的团队一直在 master 的原型分支中工作。 I now want to take that work, slice it up into different "feature branches", and merge them individually into master.我现在想接受这项工作,将其分成不同的“功能分支”,然后将它们单独合并到 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 . 1 - 在master 之外创建一个新分支 Feature_1。 Manually copy the code from the Prototype to Feature_1.手动将代码从 Prototype 复制到 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.这意味着当我去制作 Feature_N 并且丢失历史记录时,我必须跟踪我复制的内容。

2 - Create a new branch, Feature_1, off of Prototype . 2 - 在Prototype 之外创建一个新分支 Feature_1。 Somehow revert the code that is not part of the first feature in Feature_1.以某种方式恢复不属于 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.这避免了对 git 撒谎(并保留历史记录),但感觉 Feature_N 合并起来会一团糟,因为我会告诉 master 在我推送 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.如果您的提交是单功能提交,并且不与任何其他功能的提交共享依赖关系,那么@Michael 的答案就是好东西。 If you've mixed work on the two features in any commit, though, you'll want interactive rebase .但是,如果您在任何提交中混合使用这两个功能,您将需要交互式 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.如果功能更改只是有时合并到提交中并且没有跨功能依赖项,为了让生活更轻松,我的第一次尝试是git rebase -i master prototype ,将混合大块的提交分成两个提交,每个提交一个,然后像迈克尔的回答一样用樱桃挑选结束。 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,其中数字表示提交包含哪些功能的代码,对于 `git rebase -i master 原型,您将编辑提交 C12 和 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 to create commit C1 where C12 was, then git commit -a to apply all the remaining hunks and create commit C2 following it. Rebase 将在提交C12后停止,您可以git reset HEAD~然后git add --patch应用所有功能 1 大块, git commitC12所在的位置创建提交C1 ,然后git commit -a应用所有剩余的大块并在它之后创建提交C2 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然后你可以git checkout -b feature1 master; git cherry-pick A1 B1 C1 E1 F1 git checkout -b feature1 master; git cherry-pick A1 B1 C1 E1 F1 and similarly for feature2. git checkout -b feature1 master; git cherry-pick A1 B1 C1 E1 F1和类似的功能2。

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.交互式 rebase 比上面可能会让您相信的要好得多,但到目前为止,了解这一点的最佳方法是坐下来查看联机帮助页,然后在那里寻找一些大块头来取乐。 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.这样做,它可能很快就会达到这样的程度:将其作为发布前的仪式通常比试图让您的实际工作流程在每一个小步骤都可发布更方便。

Create two branches feature_1 and feature_2 off of master and cherry pick the commits from prototype in the regarding feature branch:master创建两个分支feature_1feature_2 ,并在相关功能分支中从prototype中挑选提交:

git checkout -b feature_1 master
git cherry-pick <commit>
git cherry-pick <commit>
…

git checkout -b feature_2 master
git cherry feature_1 prototype | grep "^+" | cut -c3- | xargs git cherry-pick

The last line cherry picks all commits from prototype which are not in feature_1 into the current branch, ie feature_2 .最后一行从prototype挑选出所有不在feature_1提交到当前分支,即feature_2

When you run into conflicts, use git status for hints how to continue.当您遇到冲突时,请使用git status来提示如何继续。

See git-cherry-pick for further documentation.有关更多文档,请参阅git-cherry-pick

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

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