简体   繁体   English

Git-如何合并功能分支,但排除一些提交?

[英]Git - how to merge feature branch, but exclude some commits?

I'm using Git for a long time now, but today I ran into a problem with merging some changes from branch to branch. 我已经使用Git很长时间了,但是今天我遇到了合并分支之间的某些更改的问题。 This situation I'm having is - I got 4 branches: 2 main ones and 2 feature ones. 我遇到的这种情况是-我有4个分支:2个主要分支和2个特征分支。 Let's assume they're called like this: main_branch1 , main_branch2 , feature_branch1 and feature_branch2 . 假设它们的调用方式如下: main_branch1main_branch2feature_branch1feature_branch2 Feature branches are always created from main_branch1 and when you finish your work, you merge them back into it. 要素分支总是从main_branch1创建的,当您完成工作时,将它们合并回去。 Then at some point of a time their changes are merged to main_branch2 as well. 然后在某个时间点,它们的更改也会合并到main_branch2中。 There are no merges between two main branches! 两个主要分支之间没有合并! Here goes the problem. 问题来了。 In my case right now, feature_branch1 is created from main_branch1 at some time, did some changes inside of it and merged it back into the main branch. 在我现在的情况下, feature_branch1有时是从main_branch1创建的,在其中进行了一些更改并将其合并回主分支。 Then I created feature_branch2 again from main_branch1 , did changes there as well and also merged it back into it. 然后,我再次从main_branch1创建了feature_branch2main_branch1那里进行了更改,并将其重新合并到其中。 Now I want to merge feature_branch2 into main_branch2 , but just the changes that it has - because when I created it I got the changes from feature_branch1 . 现在,我想将feature_branch2合并到main_branch2 ,但只合并其中的更改-因为创建它时,我从feature_branch1获得了更改。 I tried with git rebase , but couldn't get the things going and my working tree looked like a complete mess. 我尝试使用git rebase ,但git rebase ,而且我的工作树看起来像是一团糟。

Here is a graph of what I want to achieve and how my tree should look like: 这是我想要实现的图以及树的外观:

o---------------o---------------------o  main_branch1
 \             / \                   /
  o-----------o   \                 /
feature_branch1    \               /
                    \             /
                     o-----------o
                  feature_branch2 \
                                   \
                                    o-----------o  main_branch2

As you can see when I created feature_branch2 , I already got changes from feature_branch1 , which were merged into main_branch1 . 如您所见,当我创建feature_branch2 ,我已经从feature_branch1获得了更改,这些更改已合并到main_branch1 How i can exclude them when I merge feature_branch2 to main_branch2 ? 当我将feature_branch2合并到main_branch2时,如何排除它们? But I will need to add them later on when I decide to merge feature_branch1 into main_branch2 ... 但是,当我决定将feature_branch1合并到main_branch2时,稍后需要添加它们。

Any idea how I can do that? 知道我该怎么做吗? Thanks! 谢谢! :) :)

Have a look in to rebase --onto - it lets you rebase, but gives you a little more control over the specific commits involved. 回顾一下rebase --onto它可以让您重新设置基础,但可以使您对所涉及的特定提交有更多的控制权。

git rebase --onto <new_base_branch> <old_base_branch> <branch_to_rebase>

In your case, the command may look something like: 在您的情况下,该命令可能类似于:

git rebase --onto main_branch2 <hash_of_commit_at_base_of_main_branch2> feature_branch2

This will checkout main_branch2 and one by one apply the commits from feature_branch2 on top of it. 这将检出main_branch2并逐一应用来自feature_branch2的提交。 You can then merge feature_branch2 in to main_branch2 . 然后,您可以将feature_branch2合并到main_branch2

Is that along the lines of what you want to achieve? 这是否符合您想要实现的目标? It's hard to know for sure without a diagram of the current state of the branches! 没有分支的当前状态图就很难确定!

If you want to exclude the changes from feature_branch1 then you'll need to checkout a new branch from before it was created and cherry pick the commits from feature_branch2 then you'll be able to fully merge later. 如果要从feature_branch1排除更改,则需要在创建新分支之前从中签出,然后从feature_branch2挑选提交,然后稍后就可以完全合并。

git checkout -b nofeature1 <SHA before feature_branch1>
git cherry-pick <first SHA feature_branch2>^..feature_branch2
git checkout main_branch2
git merge nofeature1

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

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