871adf OK, feature Z is fully implemented      --- newer commit --┐
0c3317 Whoops, not yet...                                         |
87871a I'm ready!                                                 |
643d0e Code cleanup                                               |-- Join these into one
afb581 Fix this and that                                          |
4e9baa Cool implementation                                        |
d94e78 Prepare the workbench for feature Z     -------------------┘
6394dc Feature Y                               --- older commit
  • 步骤 1:

执行git rebase -i HEAD~7 或者 git rebase -i 6394dc

  • 步骤 2: 选择与压缩
    将任务 pick 修改为 squash (或者简写为 s ):
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: 创建新的commit
  • 步骤 4: 用git push -f命令强制push到远程仓库

以上主要参考:Git 压缩多个commit为单个commit ; git中利用rebase来压缩多次提交

在IntelliJ中操作:

可以在左下方的Git选项卡中直接这样:
在这里插入图片描述

更多推荐