[英]Merging with another feature branch before rebasing to Master in Git
So I'm trying to understand how Git handles certain processes and what are some already used practices.所以我试图了解 Git 如何处理某些流程以及一些已经使用的实践。
Let's say we have a Git repository with a branch called master
.假设我们有一个 Git 存储库,其中有一个名为master
的分支。 We also have two branches that are created simultaneously from master
.我们还有两个从master
同时创建的分支。 We'll call them branch_one
and branch_two
.我们称它们为branch_one
和branch_two
。
branch_one
already has work completed for a specific feature. branch_one
已经完成了特定功能的工作。 I am currently developing on branch_two
.我目前正在开发branch_two
。 For the sake of this conversation, let's assume that I can't merge branch_one
to master
because it's pending approval from other developers.为了这次对话,让我们假设我无法将branch_one
合并到master
,因为它正在等待其他开发人员的批准。
Here's the issue:这是问题所在:
I need all of the work from branch_one
in order to continue work on branch_two
.我需要branch_one
的所有工作才能继续branch_two
的工作。
Here is my current flow:这是我目前的流程:
1) merge branch_one
into branch_two
. 1)合并branch_one
到branch_two
。
2) work on branch_two
. 2) 处理branch_two
。
3) rebase branch_two
with master
before submitting a pull request. 3) 在提交拉取请求之前将branch_two
与master
变基。
Uh-oh.呃哦。 The rebase has conflicts on 30+ patches. rebase 在 30 多个补丁上有冲突。 I assume this is because the merge (step 1) changes the head of branch_two
.我认为这是因为合并(第 1 步)更改了branch_two
的头部。 I may be assuming incorrectly.我可能假设不正确。
Obviously I would like to avoid a massive conflict resolution step in my version control process.显然,我想在我的版本控制过程中避免大量的冲突解决步骤。
So my questions:所以我的问题:
Is there a better way to handle this type of process, where a feature branch requires changes from another feature branch, that doesn't include massive conflicts?有没有更好的方法来处理这种类型的过程,其中一个功能分支需要来自另一个功能分支的更改,不包含大量冲突?
You need an integration branch, made from master HEAD:您需要一个由主 HEAD 制作的集成分支:
If branch1 needs additional work in the context of its validation, merge the new branch1 commits in integration again.如果 branch1 需要在其验证上下文中进行额外的工作,请再次合并新的 branch1 提交以进行集成。
At some point, branch1 will be merge into master.在某个时候,branch1 将合并到 master 中。
Then, whenever you want to validate branch2, rebase it first on top of the updated integration branch.然后,每当您想要验证 branch2 时,首先将其重新设置在更新的集成分支之上。 Then merge it to integration (using --no-ff
: no fast-forward merge).然后将其合并到集成(使用--no-ff
:无快进合并)。
Finally, merge branch2 to master when ready.最后,准备好后将 branch2 合并到 master。 No conflict there.那里没有冲突。
For more on this workflow, see gitworkflow (far better than Gitflow )有关此工作流程的更多信息,请参阅 gitworkflow (比Gitflow 好得多)
You are running into a common pitfall in GIT.您遇到了 GIT 中的一个常见陷阱。 The solution is always, think ahead.解决方案总是,提前想好。 In your scenario, knowing whether or not you'll need a feature from one branch ahead of time will give you the ability to branch in a way that avoids this issue:在您的场景中,提前知道您是否需要一个分支的功能将使您能够以一种避免此问题的方式进行分支:
create a branch off of master that will be an epic-branch从 master 创建一个分支,这将是一个史诗分支
git checkout -b epicBranch
create both branch_one
& branch_two
off of epicBranch
从epicBranch
创建branch_one
和branch_two
git checkout -b branch_one
git checkout -b branch_two
git checkout -b branch_one
git checkout -b branch_two
Once you need a feature from branch_one
you PR and merge branch_one
into epicBranch
then merge epicBranch
into branch_two
一旦您需要来自branch_one
的功能,您就可以 PR 并将branch_one
合并到epicBranch
,然后将epicBranch
合并到branch_two
git checkout branch_two
git merge epicBranch
Now you have the change from branch_one
in branch_two
without the overhead of closing branches.现在您可以从branch_one
到branch_two
进行更改,而无需关闭分支的开销。
You will have the commit history and shouldn't run into any issues when merge or rebasing.您将拥有提交历史记录,并且在合并或变基时不应遇到任何问题。
You can also save a lot of headache by cherry picking your needed feature commit into a branch of its own that can be shared between branches.您还可以通过将需要的功能提交到一个可以在分支之间共享的自己的分支中来避免很多麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.