简体   繁体   English

将git分支的一部分合并到master

[英]Merging part of git branch into master

I have a simple branch with multiplpe changes: 我有一个带有multiplpe更改的简单分支:

Master ---A---B---C
                   \
              Foo   E---F---G---H

Now I need to merge into the Master only a part of my changes. 现在,我只需要将部分更改合并到Master中即可。 I am new to git ( currently using it with SourceCode on Windows ), and was thinking of doing it like this: 我是git的新手(目前在Windows上将其与SourceCode一起使用),并正在考虑这样做:

Master ---A---B---C---?---J---?---?---
                   \     /
              Foo   E---G'
                         \
                    Bar   F'---H'---

I need only certain parts from the branch. 我只需要分支中的某些部分。 I've been working on 4 different manufacturer APIs to implement. 我一直在努力实现4种不同的制造商API。 Now, only 1 needs to go in production and the rest are on hold. 现在,只有1个需要投入生产,其余的则被搁置。 Let's say there are total of 19 files modified, but I only need 7 of them. 假设总共修改了19个文件,但我只需要其中7个。 Now, I need to split those 7 away, synchronize with the master and push as a different branch, so that the CTO can merge them into Master later on. 现在,我需要将这7个拆分,与master同步并作为另一个分支推送,以便CTO稍后可以将它们合并到Master中。

Is that the way to do it? 那是这样做的方式吗? And how do I do it correctly? 以及我该如何正确执行呢?

If we breakdown your problem, you need to: 如果我们解决您的问题,您需要:

  1. Split 4 (E,F,G,H) commits from Foo into 2 branches, Foo will have only: E, G, but Bar will have all 4, E -> G -> F -> H. 拆分4(E,F,G,H)从Foo提交到2个分支中,Foo将只有:E,G,但是Bar将拥有全部4,E-> G-> F->H。
  2. Remove some commits : ie on Foo, remove commit F and H. 删除一些提交:即在Foo上,删除提交F和H。
  3. I am not sure if you want to "Re-order" the commits on the branch Bar, ie have E,G,F,H --> I am assuming this solves no purpose, so I will leave it out of my answer. 我不确定您是否要“重新排序”分支Bar上的提交,即具有E,G,F,H->我假设这没有任何目的,所以我将其排除在答案之外。

There are many ways of doing it, but I will keep it to the simplest flow that I can think of, since you are new to git. 这样做的方法很多,但由于您是git的新手,所以我将其保持在我能想到的最简单的流程中。 so, let us look at splitting the branch: 因此,让我们看一下拆分分支:

From your current Foo (head = H) do git checkout -b Bar This will create the branch Bar with commits E, F, G, H 从您当前的Foo(head = H)执行git checkout -b Bar这将创建带有提交E,F,G,H的分支Bar

Now, there are two ways you can reset your Foo branch: 现在,有两种方法可以重置Foo分支:
#1 Discard Foo and re-create it git branch -D Foo (to delete from local) git push origin :Foo (to delete from remote, if you have already pushed it) #1丢弃Foo并重新创建它 git branch -D Foo (从本地删除) git push origin :Foo (从远程删除,如果您已经将其删除)

Then you need to create a new Foo from commit "C" of your master: 然后,您需要根据主机的提交“ C”创建一个新的Foo:

git checkout -b Foo <CommitIdShaForC>

Then you can cherry-pick commits E and G either from git gui by doing gitk Bar and then right clicking on the commit and choosing cherry-pick, or from console by: 然后,您可以通过执行gitk Bar从git gui来选择提交E和G,然后右键单击提交并选择Cherry-pick,或者从控制台通过以下操作:

git cherry-pick <Commit Id for E> <Commit Id for G>

In case of merge issues, look at this answer 如果出现合并问题,请查看此答案

#2 Revert commits from Foo If you don't mind having history of F and H in your foo, you can simply revert them in git, essentially git creates a reverse patch of your commits F and H and applies them as new commits: #2从Foo还原提交如果您不介意在foo中包含F和H的历史记录,则可以简单地在git中还原它们,本质上git为您的提交F和H创建一个反向补丁,并将它们作为新的提交应用:

  • You are on branch Foo and it has E -> F -> G -> H 您在Foo分支上,它具有E -> F -> G -> H
  • git revert F
  • Your branch should now show E -> F -> G -> H -> F' where F' is undoing what the commit F added 现在,您的分支应显示E -> F -> G -> H -> F' ,其中F'撤消了F添加的内容
  • git revert H would lead to: E -> F -> G -> H -> F' -> H' whose end result in the source code would be: same as having only commits E and G. git revert H将导致: E -> F -> G -> H -> F' -> H'其在源代码中的最终结果将是:与仅提交E和G相同。

Alternatively still, you can use the --no-commit flag on git revert to revert both F and H in 1 commit: 另外,您也可以在git revert上使用--no-commit标志来在1次提交中同时还原F和H:

git revert --no-commit F
git revert --no-commit H
git commit -a -m "reverting commits F and H"

This would lead to: 这将导致:

E -> F -> G -> H -> I where I is the commit which undoes F and H E -> F -> G -> H -> I I是撤销F和H的提交

#Update 1 After writing a lengthy answer, I see that OP wants to have "part of the commits" into few branches. #Update 1写完冗长的答案后,我看到OP希望将“部分提交”放入几个分支中。 For this case, I would highly recommend an interactive rebase so that the commits can be split. 对于这种情况,我强烈建议您使用交互式变基,以便可以拆分提交。 Please refer to the official documentation about git rebase, in particular the section about "Splitting Commits". 请参考有关git rebase的官方文档 ,尤其是有关“拆分提交”的部分。 Also, I think you will find this question particularly helpful 另外,我认为您会发现这个问题特别有帮助

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

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