使用 git 同时提交到多个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4532063/
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 04:51:57  来源:igfitidea点击:

Commit to multiple branches at the same time with git

gitbranchcommitautocommit

提问by Leif Andersen

I have two branches A and B in a project that I am working on. B differs from A by a single commit, which is a section of the code completely independent from what I'm working on for the next while (aka, I will have many commits I want to push to both branch A and B).

我正在处理的一个项目中有两个分支 A 和 B。B 与 A 的不同之处在于单个提交,这是代码的一部分,完全独立于我接下来的工作(也就是说,我将有许多提交,我想同时推送到分支 A 和 B)。

Is there any way in git that I can commit to both branch A and branch B at the same time, without having to commit it to one branch, checkout the other, and try to cherry pick out the commit(s).

在 git 中有什么方法可以同时提交到分支 A 和分支 B,而不必将其提交到一个分支,检查另一个分支,然后尝试挑选出提交。

采纳答案by VonC

You could:

你可以:

  • make all your commits on A
  • rebase Bon top of A(if you haven't pushed B already, that is)
  • 做出你所有的承诺 A
  • 重新建立BA(如果你还没有推 B 已经,那就是)

That way, Bwill include all commits from A, plus its single commit.

这样,B将包括来自 的所有提交A,以及它的单个提交。



If you have shared B(pushed to a common remote repo), the idea is more to add any commit made on Ato B(that is, "on top of B).

如果您有共享B(推到一个共同的远程回购),这个想法是更添加任何提交上进行A,以B(在顶部即” B)。

The simplest way would be to merge Aon B, if you don't mind having only one commit on Brepresenting all commits from A.
I would prefer that to any solution involving cherry-picking would mean different SHA1 for each commit recreated on B, which would make any future merge back to Acomplicated (because Git would go back a long way to find a common ancestor)

最简单的方法是合并AB,如果你不介意的话只有一个提交的B代表来自所有提交A
我更希望任何涉及樱桃采摘的解决方案都意味着在 上重新创建的每个提交都有不同的 SHA1 B,这将使任何未来的合并A变得复杂(因为 Git 会回溯很长一段路来找到共同的祖先)

回答by spoutnik

the cherry-pick feature is a better way to do this, check answer at

樱桃挑选功能是一种更好的方法,请查看答案

Git: Commit to multiple branches at the same time

Git:同时提交到多个分支