简体   繁体   English

如何将所有提交从一个分支合并到另一个分支?

[英]How to merge all commits from one branch to another?

I want to merge branch A to master but when I use "git merge A" on master it will merge the branches but will only record one commit on master where as I commited like 20 on branch A. I would like to merge all the commits from A to master. 我想将分支A合并到主节点,但是当我在主节点上使用“ git merge A”时,它将合并分支,但只会在master上记录一次提交,就像我在分支A上提交20一样。我想合并所有提交从A到主人。 How can I do that? 我怎样才能做到这一点?

You have cross-tagged this as both and 您已将其交叉标记为 , and the answers are different for these two different situations. ,对于这两种不同的情况,答案是不同的。

GitHub offers a single button labeled "Merge pull request", with a dropdown triangle. GitHub提供了一个标记为“合并拉取请求”的按钮,带有一个下拉三角形。 Clicking on the triangle offers more options (at least normally— the GitHub help page says that this "Depend[s] on the merge options enabled for your repository"): 单击三角形可提供更多选项(至少通常是这样-GitHub帮助页面上显示此“取决于为您的存储库启用的合并选项”):

  • Create a merge commit 创建一个合并提交
  • Squash and merge 压合并
  • Rebase and merge 变基并合并

The Git command line is different, being simultaneously simpler and more complicated. Git命令行是不同的,同时更加简单和复杂。 You can run any of these commands: 您可以运行以下任何命令:

  • git merge to get a default action that is situation-dependent; git merge获得依赖于情况的默认操作;
  • git merge --ff-only to prevent merging, but allow a fast-forward action; git merge --ff-only防止合并,但允许快速动作;
  • git merge --squash to force a squash action, without creating a merge; git merge --squash强制执行壁球动作,而不创建合并;
  • git merge --no-ff to force a merge action; git merge --no-ff强制执行合并操作; or 要么
  • git rebase followed by git merge --ff-only to copy the commits such that fast-forwarding is possible—this is the default rebase action—and then to do a fast-forward action. git rebase后跟git merge --ff-onlygit merge --ff-only复制提交以便可以进行快速提交(这是默认的rebase操作),然后执行快速转发操作。

The first of the three GitHub actions, "Create a merge commit", is equivalent to the command-line git merge --no-ff , in terms of final result (the difference is that GitHub won't offer this option at all if there are merge conflicts, but see this page as well). 就最终结果而言,三个GitHub动作中的第一个“创建合并提交”等效于命令行git merge --no-ff (不同之处在于GitHub完全不提供此选项,如果存在合并冲突,但也请参见此页面 )。 The second of the three GitHub actions is equivalent to the command-line git merge --squash and is what you described in your question: GitHub上的三个动作中的第二个等效于命令行git merge --squash ,这就是您在问题中所描述的内容:

... will only record one commit on master where as I committed like 20 on branch A ...只会在master上记录一次提交,就像我在分支A上提交20一样

Note that the new commit has the same effect as the other N commits, but is an ordinary, non-merge, single-parent commit (this is hard to see in the GitHub interface as it tries to hide the nonlinear nature of Git commit graphs). 请注意,新提交与其他N个提交具有相同的效果 ,但是它是普通的,非合并的单亲提交(这在GitHub接口中很难看到,因为它试图隐藏Git提交图的非线性性质。 )。 See this GitHub help page squash diagram . 请参阅此GitHub帮助页面squash图 (Note that the plain-merge diagram on that page is rather poor, but the squash one is decent.) A real merge, made with --no-ff on the command line, has two parents: the first parent is the previous commit on the branch on which you performed the merge (ie, master ), and the second parent is the tip commit of the branch that was merged (branch A in your example). (请注意,该页面上的纯合并图相当差,但壁球比较好。)在命令行上用--no-ff进行的实际合并有两个父级:第一个父级是上一个提交在执行合并的分支(即master )上,第二个父级是已合并分支的尖端提交(在示例中为分支A )。 This means that all N separate commits remain separate: there is just one new commit added that represents the result of combining those separate commits. 这意味着所有N个独立的提交都保持独立:仅添加了一个新的提交,表示合并这些单独的提交的结果。

In many ways, this is the best result: it allows you to view the feature addition as a single unit (the merge commit), or as a series of changes (the N separate commits on the side branch). 在许多方面,这是最好的结果:它使您可以将功能部件添加视图视为单个单元(合并提交)或一系列更改(侧分支上的N个单独提交)。 But it does make for a more tangled history: if the fact that there was a merge is expected to be useless or distracting in the future, while the N separate commits are expected to be useful (for debugging, for instance), it may be better to use a fast-forward operation. 但这确实使历史更加纠结:如果预计合并的事实在将来将是无用的或分散注意力的,而预期N个单独的提交将是有用的(例如,用于调试),则可能是最好使用快进操作。

As I mentioned above, a fast-forward is not a merge at all. 如前所述,快进根本不是合并。 However, fast-forward is only possible if all the "new" commits to be merged are "ahead of" all the current commits. 但是,仅当所有要合并的“新”提交都在所有当前提交“之前”时,才可以进行快进。 Visualizing whether this is the case is, as far as I can tell, impossible 1 on GitHub. 可视化是否是这种情况,据我所知道的,不可能1 GitHub上。

If fast-forward is possible, the command-line command git merge will do it by default. 如果快进可能的,命令行命令git merge将在默认情况下做到这一点。 If it's possible, git merge --ff-only will succeed. 如果可能, git merge --ff-only将成功。 So if you want to attempt a fast-forward, but not create an actual merge commit if it's not possible, you can simply use git merge --ff-only and observe whether it succeeds or fails. 所以,如果你想尝试快进,但不能创建一个实际的合并提交,如果它是不可能的,你可以简单地使用git merge --ff-only ,观察它是否成功或失败。

If a fast-forward is currently not possible, you can use git rebase to make it become possible, after which you can use git merge (optionally with --ff-only , either as a safety check, or to override any configuration you set with git config merge.ff ). 如果当前无法进行快进,则可以使用git rebase使其成为可能,然后可以使用git merge (可选地与--ff-only ,作为安全检查或覆盖您设置的任何配置)使用git config merge.ff )。 Rebasing is somewhat complex, at least when it goes wrong, so as others said you should read up on it first. 至少在出错时,重新定级有些复杂,因此正如其他人所说,您应该首先阅读它。


1 Or at least, too difficult to bother. 1或至少很难打扰。 If there's a clicky button that shows the actual commit graph, that might be interesting. 如果有一个clicky按钮显示实际的提交图,那可能很有趣。

Sounds like rebasing into master is what you're looking for. 听起来像是要扎根于母版,这是您想要的。

Try: 尝试:

git checkout A
git rebase master

I suggest that you read about the difference between merging and rebasing here https://www.atlassian.com/git/tutorials/merging-vs-rebasing 我建议您在https://www.atlassian.com/git/tutorials/merging-vs-rebasing上了解合并和重新定标之间的区别

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

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