git 如何将提交从一个分支复制到另一个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33024374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:26:38  来源:igfitidea点击:

How to copy commits from one branch to another

git

提问by Nate

I have two features I'm working on

我正在开发两个功能

Shared -- Commits -- A -- B -- C Feat1

共享 -- 提交 -- A -- B -- C Feat1

Shared -- Commits -- D -- E -- F Feat2

共享 -- 提交 -- D -- E -- F Feat2

The problem is that to test Feat2 I really need Feat1 as well, but I still want them as separate branches because they are distinct. What is a good way to make changes to Feat2 and then quickly make a third branch that is both of them together that doesn't require me to destroy it every time.

问题是为了测试 Feat2,我真的还需要 Feat1,但我仍然希望它们作为单独的分支,因为它们是不同的。有什么好方法可以对 Feat2 进行更改,然后快速创建第三个分支,将它们放在一起,不需要我每次都销毁它。

Shared -- Commits -- Feat1 -- Feat2

共享 -- 提交 -- Feat1 -- Feat2

What I was doing is

我正在做的是

git checkout feat2 git branch -b combo git rebase -i feat1

git checkout feat2 git branch -b combo git rebase -i feat1

But then when I make updates to feat2 I don't know how to merge in those new changes.

但是当我对 feat2 进行更新时,我不知道如何合并这些新更改。

采纳答案by ronalchn

Merging

合并

You can merge instead of rebase. Since your combo branch is just for testing, you don't have to worry about the history being a bit messier.

您可以合并而不是变基。由于您的组合分支仅用于测试,因此您不必担心历史会变得有点混乱。

That is:

那是:

git checkout feat2
git branch -b combo
git merge feat1

Later, when you update feat2:

稍后,当您更新 feat2 时:

git checkout combo
git merge feat2

Whenever you make an update to feat1 or feat2, merge that into combo.

每当您对 feat1 或 feat2 进行更新时,请将其合并到组合中。

The disadvantage is that you'll have to merge all commits in both branches. If there are changes you don't want, you'll have to make a separate commit removing those changes in the combo branch.

缺点是您必须合并两个分支中的所有提交。如果有您不想要的更改,则必须进行单独的提交以删除组合分支中的这些更改。

Rebasing

变基

You can rebase the additional commits from feat2 onto combo.

您可以将 feat2 中的额外提交重新绑定到组合上。

Suppose you add some commits onto feat2, then to move these commits to combo:

假设您在 feat2 上添加了一些提交,然后将这些提交移动到组合:

git rebase --onto combo lastcommittoexcludeonfeat2 feat2

Alternatively, before you make the changes to feat2, add a branch or tag, eg:

或者,在对 feat2 进行更改之前,添加一个分支或标签,例如:

git checkout -b lastfeat2rebase
git checkout feat2
git commit ... # more commits here to feat2
git rebase --onto combo lastfeat2rebase feat2

Please note, that once you go with the rebase option, you cannot use the merge option anymore. If you use the merge option, you can later decide to go with the rebase option.

请注意,一旦使用 rebase 选项,就不能再使用合并选项。如果您使用合并选项,您可以稍后决定使用 rebase 选项。

回答by Ajedi32

There are basically two options here.

这里基本上有两种选择。

  1. You can use cherry-pickto copy the new commits on feat2to combo.
  2. Instead of rebasing, you can merge
  1. 您可以使用cherry-pick将新提交复制feat2combo.
  2. 您可以合并,而不是变基

Cherry Picking

采摘樱桃

To cherry pick new commits on feat2to combo, run the following:

樱桃采摘新提交feat2combo,运行以下命令:

git checkout combo
git cherry-pick <new commit> <new commit> <new commit>

Merging

合并

Or alternately, you can merge instead of rebasing. E.g. To create combo:

或者,您可以合并而不是变基。例如要创建组合:

git checkout -b combo feat1
git merge feat2

Then, to "merge in new changes" from feat2, just...

然后,要从 中“合并新更改” feat2,只需...

git merge feat2

Likewise, to merge in new commits on feat1:

同样,要合并新的提交feat1

git merge feat1