简体   繁体   English

如何将两个旧提交合并为一个?

[英]How can I merge two old commits into one?

Imagine I have the last three commits: 假设我有最后三个提交:

fwr5678 most recent commit
46545ac fix for old commit
3c337c1 old commit message

I want to rewrite my history so the oldest two ( 46545ac and 3c337c1 ) are merged into the same commit. 我想重写我的历史记录,以便将最旧的两个( 46545ac3c337c1 )合并到同一提交中。 I want my history to look like 我希望我的历史看起来像

fwr5678 most recent commit
3c337c1 fix for old commit + old commit message

How can I do this? 我怎样才能做到这一点?

The easiest way is: 最简单的方法是:

git rebase --interactive HEAD~3

Then fill in: 然后填写:

pick   3c337c1 old commit message
squash 46545ac fix for old commit
pick   fwr5678 most recent commit

Finally, another prompt will show up asking you how to merge the commit messages. 最后,将出现另一个提示,询问您如何合并提交消息。 Simply write: 只需写:

fix for old commit + old commit message

Obviously, all the standard git adages apply. 显然,所有标准git adages都适用。 (eg "Don't rewrite public history.") (例如“请勿重写公共历史记录。”)

I don't think that this is possible to do in Git, by design. 我认为通过设计,在Git中不可能做到这一点。

A commit ID is based on older commits, so if your old commits change, then your newer ones should change as well. 提交ID基于较早的提交,因此,如果您的旧提交发生了更改,则较新的提交也应更改。

In any case, I'm assuming you've already pushed this history to a remote, since you want to keep the top commit ID the same, and that you shouldn't be doing; 无论如何,我假设您已经将此历史记录推送到了远程数据库,因为您希望保持最前面的提交ID不变,并且您不应该这样做。 it'll mess up other people's repos. 它会弄乱别人的仓库。

On the other hand, if you haven't pushed, then simply do a squash . 另一方面,如果您还没有推动,那么只需壁球

...one of the most widely used is the ability to squash commits. ...最广泛使用的一种是压榨提交的能力。 What this does is take smaller commits and combine them into larger ones, which could be useful if you're wrapping up the day's work or if you just want to package your changes differently. 这样做是需要较小的提交,然后将它们组合成较大的提交,如果您要完成当天的工作,或者只是想以不同的方式打包更改,这将很有用。

A word of caution: Only do this on commits that haven't been pushed an external repository. 注意:仅对尚未推送到外部存储库的提交执行此操作。 If others have based work off of the commits that you're going to delete, plenty of conflicts can occur. 如果其他人基于要删除的提交进行工作,则可能会发生许多冲突。 Just don't rewrite your history if it's been shared with others. 如果您的历史记录已与其他人共享,则不要重写。

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

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