简体   繁体   English

如何在 Git 中仅将更改/差异从一个分支应用到另一个分支

[英]how to apply only the changes/diff from one branch to another in Git

I created a "branch_b" and ended up applying two commits.我创建了一个“branch_b”并最终应用了两个提交。 When i tried to merge with the master i realised that i haven't pulled my master since ages so there were so many conflicts (some of them irrelevant to my commits) to the point not worth resolving them (way too much effort).当我试图与主人合并时,我意识到我很久以来就没有拉过我的主人,所以有很多冲突(其中一些与我的提交无关)到了不值得解决的地步(太多的努力)。

What i want to do is to create a new "branch_X" from the updated master and apply the changes that i made on "branch_b".我想要做的是从更新的主创建一个新的“branch_X”并应用我在“branch_b”上所做的更改。

Is it possible to achieve that?有可能实现吗?

I have tried:我努力了:

  1. cherry-pick the commits but when i try to apply them on branch_X i get the same conflicts when trying to merge "branch_b" to master.挑选提交,但是当我尝试在 branch_X 上应用它们时,我在尝试将“branch_b”合并到 master 时遇到了相同的冲突。 As stated previously, if i start resolving all the conflicts it would provably take up until year 2050.如前所述,如果我开始解决所有冲突,它可能会持续到 2050 年。

  2. Create a patch (using diff command) and tried to apply on "branxh_X".创建一个补丁(使用 diff 命令)并尝试应用于“branxh_X”。 That one also failed with a simple message from git "error: patch failed:"那个也失败了,来自 git 的简单消息“错误:补丁失败:”

UPDATE1更新1

So far any attempt resulted with conflicts which is something that i am trying to avoid.到目前为止,任何尝试都会导致冲突,这是我试图避免的。

Lets assume that i create a new branch and i start from the beginning making the same changes i did on branch_b "manually".让我们假设我创建了一个新分支,并且我从头开始进行与“手动”在 branch_b 上所做的相同的更改。 Instead of doing this manually, is there anyway to do it via git?不是手动执行此操作,而是通过 git 执行此操作吗? without rebasing or cherry-pick(those will create conflicts no matter what i try)?没有变基或挑选(无论我尝试什么都会产生冲突)?

That can be done like this:可以这样做:

git checkout --detach branch_b
git rebase --onto branch_X branch_X HEAD

That should apply the two revisions on top of branch_X..... but that will bring up just as many (if not more) conflicts.这应该在 branch_X 之上应用两个修订......但这会带来同样多(如果不是更多)的冲突。 It's not like rebase can magically get rid of the conflicts.不像 rebase 可以神奇地摆脱冲突。 One general rule would say (and you can quote me on this): You could skip conflicts on rebase by merging.... it doesn't work the other way around .一条一般规则会说(你可以引用我的话): You could skip conflicts on rebase by merging.... it doesn't work the other way around

If you like the result after you rebase如果你喜欢 rebase 后的结果

git branch -f branch_b # ser branch_b on it's new position

Then push if/as needed然后根据需要推送

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

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