如何将多个合并压缩为单个合并? [英] How do I compress multiple merges into a single merge?

查看:174
本文介绍了如何将多个合并压缩为单个合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的主题分支。 (分支主题目前指向'a0a0')

I have a topic branch that looks like this. (the branch topic currently points at 'a0a0')


  • a0a0将分支'Master'合并到主题中<$ li
  • b1b1将分支'Master'合并到主题中
  • c2c2将'master from master'合并到主题中
  • d3d3将分支'Master'合并为主题

  • e4e4将分支'主'合并到主题中
  • f5f5将分支'主'合并到主题中 $ 666一个实际的提交

  • [主题分支上有很多历史记录]

  • 9999原始分歧点

  • a0a0 Merge branch 'Master' into topic
  • b1b1 Merge Branch 'Master' into topic
  • c2c2 Merge commit 'something from master' into topic
  • d3d3 Merge Branch 'Master' into topic
  • e4e4 Merge Branch 'Master' into topic
  • f5f5 Merge Branch 'Master' into topic
  • 6666 an actual commit
  • [lots of history on the topic branch]
  • 9999 original divergence point

如何将a0-f5转换为单个合并提交?

How do I turn a0-f5 into a single merge commit?

我试过了: git rebase -i f5f5 ,目的是告诉它到 squash 这5个提交,但是它只是在f5f5和a0a0。所以我把第一个也是最后一个从 pick 转换成 squash ,但是每个单独的提交都变得疯狂并重新发布掌握一个巨大的承诺来代替所有的合并,失去了它是一个合并的事实。这不是我所期望的!

I've tried: git rebase -i f5f5 with the intention of telling it to squash those 5 commits, but instead it just gave every commit on the master branch between f5f5 and a0a0. So I turned all but the first and last from pick into squash, but that went crazy and rebased every single commit from master into a giant commit in place of all the merges, losing the fact that it was a 'merge'. That isn't what I expected!

我可以这样做:

I can do:

  $ git reset 6666
  $ git merge Master

但那需要我重新做任何合并提交。

but that requires me to re-do any merge commits.

那么如何将这些不同的合并压缩成单个合并提交,它知道它是合并,并且还保留了所有合并冲突的内容? / b>

So how do I compress those different merges into a single merge commit, that knows it's a merge, and also retains all the merge-conflict stuff?

推荐答案

这会做到这一点:

This will do it:

git reset --hard $(git commit-tree -p f5f5 -p Master -m "Merge hurricane" a0a0:)

为了理解它是如何工作的,请记住,合并提交绝对不是神奇的。可能很难创建,但一旦创建,精确地重新创建 会非常容易。这是因为git没有记录变更集,它记录了整个树。像> git show 这样的工具显示提交为差异,并将提交合并为非常花哨的差异,但实际上,提交是指向完整树的指针。压缩一些提交是一个简单的事情,重新创建一个包含相同树的新提交,给它一个不同的父和提交信息。

To understand how it works, first remember that there is absolutely nothing magical about a merge commit. It can be hard to create, but once created, it's embarrassingly easy to recreate exactly. This is because git doesn't record changesets, it records entire trees. Tools like git show display commits as diffs, and merge commits as very fancy diffs, but in reality a commit is the pointer to a full-fledged tree. Squashing some commits is a simple matter of recreating a new commit that contains the same tree, giving it a different parent and commit message.

管道命令 git commit-tree 做最后一部分。给定一个父提交( f5f5 ),一个提交消息和一个树ID(很容易用 COMMIT:引用)它会创建一个包含指定树的新提交。如果提交具有单个父代,则它将是一个常规提交。如果它有多个父母,它将是一个合并提交。一旦创建了压扁的合并提交,剩下的就是使用 git reset --hard 指向当前分支到新提交。

The plumbing command git commit-tree does this last part. Given a parent commit (f5f5), a commit message, and a tree ID (easily referenced with COMMIT:), it creates a new commit that contains the specified tree. If the commit has a single parent, it will be a regular commit. If it has multiple parents, it will be a merge commit. Once the squashed merge commit is created, what remains is to point the current branch to the new commit with git reset --hard.

这篇关于如何将多个合并压缩为单个合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆