美文网首页
git 压缩commit

git 压缩commit

作者: _宋江_ | 来源:发表于2022-06-21 21:46 被阅读0次

步骤 1: 选择你的起始提交

git rebase --interactive HEAD~[N] 命令的缺点就是你必须一个一个的数出准确的提交次数。幸运的是这里还有另一种方式:

git rebase --interactive [commit-hash]

[commit-hash] 就是你要压缩的提交范围的起始提交之前的一次提交的 hash。所以在我的示例中的命令就是:

git rebase --interactive 6394dc

6394dc 是 Feature Y。你可以将这个命令理解为:

对 [commit-hash] 之上的所有提交进行合并。

步骤 2: 选择与压缩

这时你的编辑器会有弹窗,显示出你想要合并的提交列表。注意,一开始可能会感觉有点看不明白,因为是按反序排列的,旧的提交显示在顶部。我通过 --- older commit 和 --- newer commit 进行了说明,在编辑器的窗口中不会显示说明。

pick d94e78 Prepare the workbench for feature Z    --- older commit

pick 4e9baa Cool implementation

pick afb581 Fix this and that 

pick 643d0e Code cleanup

pick 87871a I'm ready!

pick 0c3317 Whoops, not yet...

pick 871adf OK, feature Z is fully implemented      --- newer commit

[...]

在提交列表的底部有一个简短的注释(示例中忽略了),提示了所有的操作选项。你可以在交互式 rebase 中进行各种操作,我们现在只进行一些基本的操作。我们的任务是将所有的提交注释为 squashable,除了第一个(最早的)提交:它将被用作起始点。

你可以通过将任务 pick 修改为 squash (或者简写为 s ,评论中有提示)来将提交标记为 squashable 。最后的结果就是:

pick d94e78 Prepare the workbench for feature Z    --- older commit

s 4e9baa Cool implementation

s afb581 Fix this and that 

s 643d0e Code cleanup

s 87871a I'm ready!

s 0c3317 Whoops, not yet...

s 871adf OK, feature Z is fully implemented      --- newer commit

[...]

保存文件,关闭编辑器。

步骤 3: 创建新的提交

你刚刚告诉了 Git 将全部的 7 次提交合并到列表的第一个提交中。现在要给它添加注释:你的编辑器会再次弹出一个带有默认消息的窗口,内容是压缩的所有提交的注释。

你可以保留默认的提交注释,这样最终的提交信息将会是这些临时提交的注释列表,如下所示:

Prepare the workbench for feature Z

Cool implementation

Fix this and that 

Code cleanup

I'm ready!

Whoops, not yet...

OK, feature Z is fully implemented

通常我不喜欢保留这些信息,所以我会清楚默认消息,使用一些自定义注释,例如 Implemented feature Z。

相关文章

网友评论

      本文标题:git 压缩commit

      本文链接:https://www.haomeiwen.com/subject/cvaevrtx.html