简体   繁体   English

如何从一个git分支更改到另一个分支

[英]How to take changes from one git branch to another branch

I have the below branches. 我有以下分支。

Branch A : The main branch or the master branch. 分支A:主分支或主分支。

Branch B : Created from A. 分支B:从A创建。

Branch C : Created from B. 分支C:从B创建。

Branch D : Created from A 分支D:从A创建

Now I did some changes to branch C and now I could merge them to branch B. But at this moment a new branch D is created from branch A and now I should merge my changes to branch D. How to take all of my changes to branch D ? 现在,我对分支C进行了一些更改,现在可以将它们合并到分支B。但是,此刻,从分支A创建了一个新的分支D,现在我应该将所做的更改合并到分支D。 D分公司 Since I have lots of commits I can not cherry-pick them one by one. 由于我有很多提交,因此我无法一一挑选。 I tried to rebase branch D on branch C, but I am getting lots of conflicts which I can't resolve manually. 我试图在分支C上重新建立分支D的基础,但是我遇到了很多无法手动解决的冲突。 So what is the best way to merge branch C to branch D ? 那么将分支C合并到分支D的最佳方法是什么?

EDIT 1 : 编辑1:

Let me clarify my problem a bit more. 让我再澄清一下我的问题。 Here branch A is the master branch. 在这里,分支A是主分支。 Branch B is a release branch (which has just ended, so the branch is now in active). 分支B是发行分支(刚刚结束,因此该分支现在处于活动状态)。 Branch C is my topic branch cut from B, branch D is the new release branch cut from A. Ideally we should merge our topic branch only to the release branch and cherry pick those changes to the master branch (A).So we create a topic branch from the current release branch then do our changes and raise merge request. 分支C是从B剪切的主题分支,分支D是从A剪切的新发行分支。理想情况下,我们应该仅将主题分支合并到发行分支,然后将这些更改选择到主分支(A)。因此,我们创建了一个然后从当前发行分支的主题分支进行更改,并提出合并请求。 We should not add anything to the inactive release branch. 我们不应将任何内容添加到非活动的发行分支。 Now I was working on a branch C which was cut from a release branch B which has become inactive. 现在,我正在处理分支C,该分支是从已变为非活动状态的发布分支B剪切而来的。 Now I should create a branch from D and add all my changes there and raise one Merge Request. 现在,我应该从D创建一个分支,并在其中添加所有更改,并引发一个“合并请求”。 I don't want to do that. 我不想那样做。 I want to use my branch C to raise a Merge Request on branch D which will not have any conflict. 我想用我的分支C在分支D上引发一个合并请求,这不会有任何冲突。 Is it possible ? 可能吗 ?

My solution is : 我的解决方案是:

I would create a temporary branch B_temp out of branch B. Squash merge my topic branch C to B_temp. 我将从分支B中创建一个临时分支B_temp。壁球将我的主题分支C合并到B_temp。 Then create a new branch E out of D, cherry pick the commit from B_temp to branch E, resolve any merge conflict (I can resolve those as they are my changes only). 然后从D中创建一个新的分支E,从B_temp到分支E挑选提交,解决任何合并冲突(我可以解决这些冲突,因为它们仅是我的更改)。 Raise merge request for F onto branch D. I am wondering if there is any better solution available instead of creating new branches and cherry picking if I could simply use the old branch C. 将对F的合并请求提高到分支D上。我想知道是否有任何更好的解决方案,而不是创建新分支和樱桃采摘,是否可以简单地使用旧分支C。

Note : We are not allowed to merge anything in the master and release branches without proper Merge Request and approval. 注意:未经适当的合并请求和批准,我们不允许在master和release分支中合并任何内容。

Assume the commit history for all branches as below: 假定所有分支的提交历史如下:

...---A---...---G   branchA, branchD
       \
        B---C---D---M   branchB
             \     /
              E---F branchC

There usually has two ways to merge changes from branchC to branchD . 通常有两种方法可以将更改从branchC合并到branchD

Option 1: merge branchB into branchD 选项1:将branchB合并为branchD

Since you have merged branchC into branchB , so you can merge branchB into branchD , and then the changes from branchC will be applied to branchD : 由于已将branchC合并到branchB ,因此可以将branchB合并到branchD ,然后将branchC的更改应用于branchD

git checkout branchD
git merge branchB

Then the commit history will be: 那么提交历史将是:

             branchA
                |
...---A---...---G-----H   branchD
       \             /
        B---C---D---M   branchB
             \     /
              E---F branchC

Option 2: rebase branchC onto branchA, then merge into branchD 选项2:将branchC重新设置为branchA,然后合并为branchD

Or you can rebase branchC onto branchA firstly by: 或者你可以变基branchCbranchA通过首先:

git rebase --onto branchA branchB branchC

And the commit history will be: 提交历史将是:

                  E'---F' branchC
                 /
...---A---...---G    branchA, branchD
       \
        B---C---D---M   branchB
             \     /
              E---F 

Then merge the rebased branchC into branchD : 然后将重新branchC branchD合并为branchD

git checkout branchD
git merge branchC

And the commit history will be: 提交历史将是:

              branchA
                |
...---A---...---G---E'---F'    branchC, branchD
       \
        B---C---D---M   branchB
             \     /
              E---F 

I want to use my branch C to raise a Merge Request on branch D which will not have any conflict. 我想用我的分支C在分支D上引发一个合并请求,这不会有任何冲突。 Is it possible ? 可能吗 ?

Yes, it possible. 是的,有可能。 First, please check if there has file(s) which was changes both in branchC and branchD by: 首先,请检查在branchCbranchD通过以下方式更改了文件:

git diff branchC branchD --name-only
  • If there has no files changed in both sides, then create a merge request to merge branchC into branchD directly. 如果没有在双方已更改的文件,然后创建一个合并请求合并branchCbranchD直接。
  • Else, overwrite the conflict file version in branchC by using branchD's file version: 否则,使用branchD的文件版本覆盖branchC中的冲突文件版本:

     git checkout branchC git checkout branchD -- filename git commit -m 'overwrite the filename by using the version from branchD' git push origin branchC 

    Then create a merge request to merge branchC into branchD , and there won't has merge confilcts. 然后创建一个合并请求以将branchC合并到branchD ,并且不会有合并冲突。

暂无
暂无

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

相关问题 如何通过git将更改从一个分支拉到另一个分支 - How to pull changes from one branch to another branch through git 如何将更改从一个分支移动到另一个分支 git? - How to move the changes from one branch to another branch git? 如何将更改从一个功能分支复制到从 GIT 中的同一父分支派生的另一个功能分支 - How to copy changes from one feature branch to another feature branch derived from same parent branch in GIT 如何在git中将更改从一个分支合并到另一个分支 - How to merge changes from one branch to another in git Git - 如何有选择地将更改从一个分支应用到另一个分支? - Git - How to selectively apply changes from one branch to another? 如何在Git上将未提交的更改从一个分支移动到另一个分支 - How to move changes as uncommitted from one branch to another on Git 如何在 Git 中仅将更改/差异从一个分支应用到另一个分支 - how to apply only the changes/diff from one branch to another in Git 在git中将更改从一个分支更改为另一个分支,以及如何在自己的分支上工作 - Get changes from one branch into another one in git and how can I work on my own branch Git:如何将一个文件从分支 A 的更改应用到同一 repo 的分支 B 中的另一个文件? - Git: How to apply changes of one file from branch A to another file in branch B of the same repo? GIT 如何将更改从一个存储库的分支拉到另一个存储库的分支 - GIT how to pull changes from a branch in one repository into a branch of another repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM