合并 Git 存储库的前两次提交?
假设您有包含三个提交 A、B 和 C 的历史记录:
A-B-C
我想合并两个提交 A 和 B 到一个提交 AB:
AB-C
我尝试
git rebase -i A
用以下内容打开我的编辑器:
pick e97a17b B
pick asd314f C
我将其更改为
squash e97a17b B
pick asd314f C
然后 Git 1.6.0.4 说:
Cannot 'squash' without a previous commit
有办法还是这根本不可能?
Suppose you have a history containing the three commits A, B and C:
A-B-C
I would like to combine the two commits A and B to one commit AB:
AB-C
I tried
git rebase -i A
which opens up my editor with the following contents:
pick e97a17b B
pick asd314f C
I change this to
squash e97a17b B
pick asd314f C
Then Git 1.6.0.4 says:
Cannot 'squash' without a previous commit
Is there a way or is this just impossible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用 git rebase -i --root
从 Git 版本 1.7.12 开始。
在交互式变基文件中,将提交的第二行 B 更改为 squash 并将其他行保留在 pick 处:
这将合并两个提交A 和 B 到一个提交 AB。
可以在这个答案中找到。
Use
git rebase -i --root
as of Git version 1.7.12.
In the interactive rebase file, change the second line of commit B to squash and leave the other lines at pick:
This will combine the two commits A and B to one commit AB.
Found in this answer.
您尝试过:
如果您继续使用
edit
而不是squash
,则可以这样开始:然后运行
Done。
You tried:
It is possible to start like that if you continue with
edit
rather thansquash
:then run
Done.
A
是初始提交,但现在您希望B
成为初始提交。 git 提交是整个树,而不是差异,即使它们通常是根据它们引入的差异来描述和查看的。即使 A 和 B、B 和 C 之间存在多次提交,此方法也有效。
A
was the initial commit, but now you wantB
to be the initial commit. git commits are whole trees, not diffs even if they are normally described and viewed in terms of the diff that they introduce.This recipe works even if there are multiple commits between A and B, and B and C.
如果您有数百或数千个提交,则使用 kostmo 的答案 可能
不切实际且缓慢,只是因为变基脚本必须处理大量提交两次,一次用于生成交互式变基编辑器列表(您可以在其中选择每次提交要执行的操作),一次用于实际执行重新应用程序的提交。
这是一个替代解决方案,它可以通过首先不使用交互式变基来避免生成交互式变基编辑器列表的时间成本。 这样一来,它就类似于 Charles Bailey 的解决方案。 您只需从第二次提交创建一个孤儿分支,然后在其之上重新设置所有后代提交的基础:
文档
In the case that you have hundreds or thousands of commits, using kostmo's answer of
can be impractical and slow, just due to the large number of commits that the rebase script has to process twice, once to generate the interactive rebase editor list (where you select what action to take for each commit), and once to actually execute the re-application of commits.
Here is an alternative solution that will avoid the time cost of generating the interactive rebase editor list by not using an interactive rebase in the first place. In this way, it's similar to Charles Bailey's solution. You simply create an orphan branch from the second commit, and then rebase all the descendant commits on top of it:
Documentation
在交互式 rebase 的情况下,你必须在 A 之前执行此操作,这样列表将是:
变为:
如果 A 是初始提交,则在 A 之前必须有一个不同的初始提交。Git 认为存在差异,它会起作用关于(A和B)和(B和C)之间的差异。 因此,南瓜在您的示例中不起作用。
In the case of interactive rebase, you have to do it before A so that the list will be:
to become:
If A is the initial commit, you have to have a different initial commit before A. Git thinks in differences, it will work on the difference between (A and B) and (B and C). Hence the squash not working in your example.
在一个相关的问题中,我设法想出了一种不同的方法来满足压缩第一个提交的需要,也就是说,使它成为第二个提交。
如果您感兴趣: git:如何将提交作为第一个插入,移动所有其他提交?
In a related question, I managed to come up with a different approach to the need of squashing against the first commit, which is, well, to make it the second one.
If you're interested: git: how to insert a commit as the first, shifting all the others?
小队的 Git 命令:
git rebase -i HEAD~[提交次数]
假设您有以下 git 提交历史记录:
pick 5152061 feat:添加了对保存图像的支持。 (A)
pick 39c5a04 修复:错误修复。 (二)
选择 839c6b3 修复:冲突已解决。 (C)
现在您想将 A 和 B 压缩为 AB,请执行以下步骤:
pick 5152061 feat:添加了对保存图像的支持。 (A)
s 39c5a04 修复:错误修复。 (二)
选择 839c6b3 修复:冲突已解决。 (C)
注意:为了压缩提交,我们可以使用squash 或s。
最终结果将是:
pick 5152061 feat:添加了对保存图像的支持。 (AB)
选择 839c6b3 修复:冲突已解决。 (C)
Git command for squad:
git rebase -i HEAD~[number of commits]
Lets say you have below git commit history:
pick 5152061 feat: Added support for saving image. (A)
pick 39c5a04 Fix: bug fixes. (B)
pick 839c6b3 fix: conflict resolved. (C)
Now you want to squash A and B to AB, perform below steps:
pick 5152061 feat: Added support for saving image. (A)
s 39c5a04 Fix: bug fixes. (B)
pick 839c6b3 fix: conflict resolved. (C)
Note: for squashing commit we can use squash or s.
The end result will be:
pick 5152061 feat: Added support for saving image. (AB)
pick 839c6b3 fix: conflict resolved. (C)
您必须执行一些命令行魔法。
这应该会留下一个包含 AB 和 C 作为提交的分支。
You have to perform a bit of command-line magic.
That should leave you with a branch that has AB and C as commits.
我还想知道如果结构是这样的
并且我希望最终的结构是这样的话,
在这种情况下我如何组合A和B?
I'm also wondering what if the structure is like
and I want the final structure to be
In this case how do I combine A and B?