简体   繁体   English

基于另一个功能分支合并功能分支,回到 master

[英]Merging feature branch based on another feature branch, back to master

As often the case withour team, changes on the next release need to start before the current release is completely tested and merged back into master, from which to start a new branch from for the next release.与我们团队的情况一样,下一个版本的更改需要在当前版本完全测试并合并回 master 之前开始,从那里开始为下一个版本创建一个新分支。

So I can create a new feature branch based on the current feature-branch, and start work on the next release there.所以我可以基于当前的功能分支创建一个新的功能分支,并在那里开始下一个版本的工作。 The problem is that this sometimes gets messy if one doesnt remember that you actually still have another feature branch or something else changes that lead to merge conflicts.问题是,如果人们不记得您实际上还有另一个功能分支或导致合并冲突的其他更改,这有时会变得混乱。

Something like this.像这样的东西。 So in this case, Feature-1 is being tested, but for one reason or another, we wont do a release from here, rather work will continue on Feature-2.因此,在这种情况下,Feature-1 正在测试中,但出于某种原因,我们不会从这里发布,而 Feature-2 的工作将继续进行。 However, notice there is a commit that happened AFTER Feature-2 was created.但是,请注意在创建 Feature-2 之后发生了一次提交。 So, correct me if Im wrong, but one way to handle this if in fact Feature-1 was NOT going to be released, then ... assuming the head is pointed at /Feature2, will the following get the change from Feature-1 AND then everything back into master?所以,如果我错了,请纠正我,但是如果事实上 Feature-1 不会被发布,那么处理这个问题的一种方法,然后......假设头部指向 /Feature2,以下是否会从 Feature-1 获得更改然后一切回到主人?

git merge Feature-1 
git push origin Feature-2
git checkout master
git merge Feature-2
git push origin master

在此处输入图片说明

If you have one "feature branch" per release, they're not feature branches.如果每个版本有一个“功能分支”,它们就不是功能分支。 (Unless you're doing continuous delivery, in which case you just release master.) (除非您正在进行持续交付,在这种情况下,您只需发布 master。)

Feature branches should be about a single feature, preferably one that either isn't going to take very long, or isn't going to mess much with other code.功能分支应该是一个单一的功能,最好是一个不会花费很长时间,或者不会与其他代码混淆的功能。 Once that feature is done it should be tested, merged, and the branch deleted never to be used again.一旦该功能完成,就应该对其进行测试、合并,并且删除的分支不再使用。

Unless you explicitly need something in an incomplete feature branch, don't branch feature branches off other feature branches .除非您在不完整的功能分支中明确需要某些东西,否则不要将功能分支从其他功能分支中分支出来 With short, sharp feature branches this should rarely be an issue.对于短而清晰的特征分支,这应该很少成为问题。 Even then often it's just a bug fix or refactoring.即便如此,它通常也只是一个错误修复或重构。 Try to extract just the bit you need, get that merged, and continue on your way.尝试仅提取您需要的部分,将其合并,然后继续前进。

If you often find yourself needing to branch off other feature branches, your feature branches are doing too much.如果您经常发现自己需要从其他功能分支中分支出来,那么您的功能分支做得太多了。

That's your first problem.那是你的第一个问题。 If you use feature branches correctly, this should be a rare a problem.如果您正确使用功能分支,这应该是一个罕见的问题。 Doing too much in one branch complicates everything.在一个分支中做太多事情会使一切变得复杂。


Let's sort out how Git does branches.下面我们来梳理一下 Git 是如何做分支的。 Unlike other version control systems, commits connect to other commits and branches are just labels.与其他版本控制系统不同,提交连接到其他提交,而分支只是标签。 Branches have no life of their own.分支没有自己的生命。 Your situation looks like this.你的情况是这样的。

                    G - H - I [feature-2]
                   /
          D - E - F - K [feature-1]
         /
A - B - C [master]

The kinks are entirely notional.扭结完全是概念上的。 We can also look at it like this.我们也可以这样看。

                    K [feature-1]
                   /
          D - E - F - G - H - I [feature-2]
         /
A - B - C [master]

Looked at this way it should be more clear what is in feature-2 .这样看应该更清楚feature-2 All of master , most of feature-1 (which is untested), and all of feature-2 .所有master ,大部分feature-1 (未经测试),以及所有feature-2 feature-2 cannot be done until feature-1 is done, and that sort of dependency is undesirable.feature-1完成之前, feature-1 feature-2无法完成,这种依赖是不可取的。


If you're working on top of a branch and want to stay up to date, the simplest thing to do is to git rebase upstream-branch .如果您在分支上工作并希望保持最新状态,最简单的方法是git rebase upstream-branch This will rewrite your commits as if they were written on top of the upstream-branch all along.这将重写您的提交,就像它们一直写在upstream-branch之上一样。 This avoids a bunch of confusing notional merges.这避免了一堆令人困惑的概念合并。

Before
                    G - H - I [feature-2]
                   /
          D - E - F - K [feature-1]
         /
A - B - C [master]

git checkout feature-2
git rebase feature -1

After
                        G1 - H1 - I1 [feature-2]
                       /
          D - E - F - K [feature-1]
         /
A - B - C [master]

feature-2 can then be safely pushed with git rebase --force-with-lease .然后可以使用git rebase --force-with-lease安全地推送feature-2 People can safely update their copy of feature-2 if they pull with rebase, instead of merging.如果人们使用 rebase 拉取而不是合并,他们可以安全地更新他们的feature-2副本。 git pull --rebase or setting pull.rebase = merges in your Git config. git pull --rebase或设置pull.rebase = merges在您的 Git 配置中pull.rebase = merges

Again, this should be rare .同样,这应该很少见


But, as above, feature-2 should probably never have been built on top of feature-1 at all.但是,如上所述, feature-2可能根本不应该构建在feature-1之上。 If they were small and independent features, it would look like this.如果它们是小而独立的特征,它会是这样的。

          D - E - F - K [feature-1]
         /
A - B - C [master]
         \
          G - H - I [feature-2]

Now you can have multiple independent features being developed and tested simultaneously.现在,您可以同时开发和测试多个独立的功能。 When they're done, in any order, they're merged into master and deleted.完成后,以任何顺序将它们合并到 master 中并删除。 master is always in a known good state and can be released at any time. master始终处于已知良好状态,可以随时释放。 Releases are tracked with tags, not branches.发布是用标签来跟踪的,而不是分支。

git checkout master
git merge feature-2
git branch -d feature-2

          D - E - F - K [feature-1]
         /
A - B - C ---------- J [master]
         \         /
          G - H - I

That's referred to as a "feature bubble".这被称为“功能泡沫”。

Everyone else then rebases their feature branch on top of master to update, deals with any conflicts or breakage, and goes about their business.然后其他人将他们的功能分支重新建立在master之上以进行更新,处理任何冲突或损坏,并继续他们的业务。

git checkout master
git pull
git checkout feature-1
git rebase master

                       D1 - E1 - F1 - K1 [feature-1]
                      /
A - B - C ---------- J [master]
         \         /
          G - H - I

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

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