在推送Git之前合并多个提交

我在本地存储库上有一堆主题上相似的提交。我想在将其推送到远程服务器之前将它们组合为一个提交。我该怎么做?我认为rebase是这样做的,但我无法理解这些文档。



精慕HU
浏览 620回答 3
3回答

森栏

您要执行的操作在git中称为“压缩”。执行此操作时有很多选择(太多吗?),但是如果您只想将所有未按下的提交合并到一个提交中,请执行以下操作:git rebase -i origin/master这将打开一个文本编辑器(-i用于“交互式”),其文件如下所示:pick 16b5fcc Code in, tests not passingpick c964dea Getting closerpick 06cf8ee Something changedpick 396b4a3 Tests passpick 9be7fdb Better commentspick 7dba9cb All done将第一个更改pick为squash(或s),第一个除外:pick 16b5fcc Code in, tests not passingsquash c964dea Getting closersquash 06cf8ee Something changedsquash 396b4a3 Tests passsquash 9be7fdb Better commentssquash 7dba9cb All done保存文件并退出编辑器。然后将打开另一个文本编辑器,使您可以将所有提交中的提交消息合并为一条大提交消息。瞧!谷歌搜索“ git squashing”将为您提供所有其他可用选项的解释。

莫回无

如果提交次数很多,而您只想压榨最近的X次提交,请找到要开始压榨的提交的提交ID,然后执行git rebase -i <that_commit_id>然后按照leopd的答案所述进行操作,将除第一个之外的所有picks 更改为squashes。范例:871adf OK, feature Z is fully implemented&nbsp; &nbsp; &nbsp; --- newer commit --┐0c3317 Whoops, not yet...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|87871a I'm ready!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|643d0e Code cleanup&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|-- Join these into oneafb581 Fix this and that&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |4e9baa Cool implementation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |d94e78 Prepare the workbench for feature Z&nbsp; &nbsp; &nbsp;-------------------┘6394dc Feature Y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--- older commit您可以执行以下操作(写入提交次数):git rebase --interactive HEAD~[7]或者这(写最后的哈希提交你不希望壁球):git rebase --interactive 6394dc
打开App,查看更多内容
随时随地看视频慕课网APP