简体   繁体   English

如何创建一个将 master 中的更改和从旧 master 分支出来的另一个分支中的更改都集成在一起的分支?

[英]How to make a branch that integrates both changes in the master and in another branch that was branched off of older master?

So I made a PR in GitHub that got merged to master.所以我在 GitHub 上做了一个 PR 合并到 master。 Now I have another PR that will take some time to merge and was branched off of master before the first PR was merged.现在我有另一个 PR 需要一些时间来合并并且在第一个 PR 合并之前从 master 分支。

Now I want to work on a third PR but it has to be branched off the latest master and also include changes that were made in 2nd PR that I mentioned (one that will take time to merge).现在我想处理第三个 PR,但它必须从最新的 master 分支出来,并且还包括我提到的第二个 PR 中所做的更改(需要时间来合并)。

I imagine I'll need to branch off of master and then rebase 2nd PR's branch on top of the new branch.我想我需要从 master 分支,然后在新分支的顶部重新建立第二个 PR 的分支。 Not sure if that's the right way though, or how to exactly go about doing it.不确定这是否是正确的方法,或者如何准确地去做。

How do I integrate latest version of master and also changes from unmerged branch into a new branch?如何将最新版本的 master 以及未合并分支的更改集成到新分支中?

How do I integrate latest version of master and also changes from unmerged branch into a new branch?如何将最新版本的 master 以及未合并分支的更改集成到新分支中?

Simplest thing to do is to make a new branch off master and the merge in the 2nd PR.最简单的事情是在第二个 PR 中创建一个新的分支和合并。

$ git checkout -b 3rdPR master
$ git merge 2ndPR

After the 2nd PR is merged into master, update the 3rd PR by merging master into the 3rd PR again.在第二个 PR 合并到 master 后,通过再次合并 master 到第三个 PR 来更新第三个 PR。

$ git checkout 3rdPR
$ git merge master

Alternatively, rebase the 3rdPR on top of master.或者,将 3rdPR 重新设置在 master 之上。

$ git checkout 3rdPR
$ git rebase master

This will result in a cleaner history without all the intermediate update merges.这将导致没有所有中间更新合并的更清晰的历史记录。


The more advanced thing to do is to first rebase your 2nd PR onto master.更高级的做法是首先将您的第二个 PR 重新设置为 master。 This is a good idea regardless, it means the 2nd PR is up-to-date during review.无论如何,这是一个好主意,这意味着第二个 PR 在审查期间是最新的。 Then you can simply branch off the 2nd PR.然后你可以简单地分支出第二个 PR。

You have this.你有这个。

     G - H [2ndPR]
    /
A - B ----- F [master]
     \     /
      D - E

Then rebase.然后重新定位。

$ git checkout 2ndPR
$ git rebase master

              G1 - H1 [2ndPR]
             /
A - B ----- F [master]
     \     /
      D - E

(Since you already pushed 2ndPR, you will need to force push the update. Do not use --force , use git push --force-with-lease . Explanation here .) (因为你已经推送了 2ndPR,你需要强制推送更新。不要使用--force ,使用git push --force-with-lease解释在这里。)

Now make a branch off 2ndPR.现在在 2ndPR 上创建一个分支。

$ git checkout -b 3rdPR 2ndPR

              G1 - H1 [2ndPR][3rdPR]
             /
A - B ----- F [master]
     \     /
      D - E

And some work on 3rdPR.还有一些关于 3rdPR 的工作。

                     J - K [3rdPR]
                    /
              G1 - H1 [2ndPR]
             /
A - B ----- F [master]
     \     /
      D - E

If 2ndPR is updated...如果 2ndPR 更新...

                     J - K [3rdPR]
                    /
              G1 - H1 - L - M [2ndPR]
             /
A - B ----- F [master]
     \     /
      D - E

...you can rebase 3rdPR on top of 2ndPR again. ...您可以再次在 2ndPR 之上重新设置 3rdPR。

$ git rebase 2ndPR
                              J1 - K1 [3rdPR]
                             /
              G1 - H1 - L - M [2ndPR]
             /
A - B ----- F [master]
     \     /
      D - E

When 2ndPR is merged...当 2ndPR 合并时...

                              J1 - K1 [3rdPR]
                             /
              G1 - H1 - L - M
             /               \
A - B ----- F --------------- N [master]
     \     /
      D - E

...rebase 3rdPR onto master. ...将 3rdPR 重新设置为 master。

$ git rebase master
              G1 - H1 - L - M   J2 - K2 [3rdPR]
             /               \ /
A - B ----- F --------------- N [master]
     \     /
      D - E

This is more complex, but it results in a more controlled update process, more accurate QA, and a cleaner history no matter how often you update branches.这更复杂,但无论您更新分支的频率如何,它都会导致更可控的更新过程、更准确的 QA 和更清晰的历史记录。 It does require confidence in being able to visualize a Git repository.它确实需要对能够可视化 Git 存储库的信心。

I imagine I'll need to branch off of master and then rebase 2nd PR's branch on top of the new branch.我想我需要从 master 分支,然后在新分支的顶部重新建立第二个 PR 的分支。 Not sure if that's the right way though, or how to exactly go about doing it.不确定这是否是正确的方法,或者如何准确地去做。

Rebasing it will probably take just as long as merging it, and may make things much harder if done improperly.重新定位它可能需要与合并它一样长的时间,并且如果操作不当可能会使事情变得更加困难。 If in doubt, don't rebase, just merge.如果有疑问,不要变基,只需合并。 It's easier to undo later if something goes wrong.如果出现问题,以后更容易撤消。

Basically, in your case, the way to go (IMHO) is to go to branch PR2 (the one that takes long time to merge), create a new branch called PR3 (the one you will work on now) and then checkout PR3, and then ... merge master into PR3.基本上,在您的情况下,要走的路(恕我直言)是转到分支 PR2(需要很长时间才能合并的分支),创建一个名为 PR3 的新分支(您现在将处理的分支),然后结帐 PR3,然后......将master合并到PR3中。

Yeah.是的。 I feel your pain.我感觉到你的痛苦。 It's just like merging PR2 into master.这就像将 PR2 合并到 master 中。 But you will have to merge PR2 to master at some point eventually.但是您最终将不得不合并 PR2 以掌握。 Better now than later.现在比以后更好。 In most cases, the longer you delay it, the worse the conflicts become.在大多数情况下,您拖延的时间越长,冲突就越严重。

Also, of you need the PR3 to have changes both from PR2 and the most recent master... then you need to merge them.此外,您需要 PR3 对 PR2 和最新的主版本进行更改...然后您需要合并它们。 master to PR2/3, PR2/3 into master, it doesn't matter. master转PR2/3,PR2/3转master,无所谓。 Even if you rebase , it still will end up with the same conflicts.即使你rebase ,它仍然会以相同的冲突结束。 It's because you need content from both branches, and it's the content changes from one branch that conflicts with content changes from the other branch.这是因为您需要来自两个分支的内容,并且来自一个分支的内容更改与来自另一个分支的内容更改冲突。 If you want both sets of change, you have to resolve the conflicts..如果您想要两组更改,则必须解决冲突。

The only way to "get away" from resolving conflicts would be to start PR3 off the most recent master, then manually walk through all changes from PR2 and re-apply them manually as new commits on PR3 (hence on master, since we start PR3 on master in this excercise). “摆脱”解决冲突的唯一方法是从最近的 master 启动 PR3,然后手动遍历 PR2 中的所有更改并手动重新应用它们作为 PR3 上的新提交(因此在 master 上,因为我们启动 PR3在这个练习中的主人)。 After doing this (meaning: copying each change to each file and pasting it in the right place, committing, taking next change, etc, etc) you will end up with PR3 having both the most-recent master and changes from PR2...执行此操作后(意思是:将每个更改复制到每个文件并将其粘贴到正确的位置,提交,进行下一个更改等),您最终将获得 PR3 拥有最新的母版和来自 PR2 的更改...

...but in fact, what you did, was doing a rebase -like thing, just manually. ...但实际上,您所做的只是手动进行了类似rebase的操作。 And you manually resolved all conflicts on the fly.而且您手动解决了所有冲突。 So you ended up kind-of manually merging PR2->master, callin it PR3, and then NOT telling git that you merged it all and resolved all problems.所以你最终手动合并了 PR2->master,称之为 PR3,然后没有告诉 git 你合并了它并解决了所有问题。

So.所以。 Seriously.严重地。 I'd advise:我建议:

  • create branch PR3 on PR2在 PR2 上创建分支 PR3
  • checkout PR3结帐 PR3
  • merge master into PR3 //A将 master 合并到 PR3 //A
  • resolve conflicts //B解决冲突 //B
  • commit //C提交 //C
  • work on PR3在 PR3 上工作

and as a bonus, the result of A+B+C is a ready-to-go candidate for a PR called "finally merged PR2 into master"作为奖励,A+B+C 的结果是一个准备好的 PR 候选者,称为“finally merge PR2 into master”

(also note that in the to-do list above there's not a single update to PR2 or master. They stay untouched. If something goes wrong with A/B/C, just delete PR3 branch and start over) (还要注意,在上面的待办事项列表中,PR2 或 master 没有一个更新。它们保持不变。如果 A/B/C 出现问题,只需删除 PR3 分支并重新开始)

暂无
暂无

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

相关问题 如何打印分支从主分支分支出来的子树? - How do I print the subtree where branch branched off master? 从master检出新分支,并从另一个分支(从master分支出来)进行更改? - Check out new branch from master with changes from another branch, which is branched out from master? 我没有从分支机构分支,而是从分支机构分支 - I didnt branch off of master, instead branched off of a branch 从 Master 分支,但包含来自另一个未合并分支的更改 - Branch off Master but include changes from another unmerged branch 意外地分支了错误的分支,当我想合并到主服务器时,我必须合并两个分支 - Accidentally branched off of the wrong branch, and when I want to merge into the master I have to merge both branches 如何重命名master分支并使另一个分支成为master? - How to rename master branch and make another branch as master? 是从本身从 master 分支的 branchA 分支,与从 master 分支相同,然后从 master 的另一个分支合并更改吗? - Is branching from branchA which itself branched from master, same as branching from master, and then merging changes from another branch of master? git 问题:继续将主分支更改拉入从功能分支分支出来的单个开发分支是否更好? - git question : is it better to keep pulling master branch changes into individual dev branch branched out from a feature branch? 如何同时部署master和branch? - How to deploy both master and branch? 如何在没有更改的情况下将分支合并到git中的master分支中? - How to merge the branch with no changes into master branch in git?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM