git合并最近的多个提交

1. 执行 rebase

git rebase -i HEAD~3

这表示对 最近的 3 个提交 进行编辑。

2. 编辑提交列表

会弹出文本编辑器、看到类似:

pick a1b2c3 提交1
pick d4e5f6 提交2
pick g7h8i9 提交3

把后两个改成 s 或 squash:

pick a1b2c3 提交1
s d4e5f6 提交2
s g7h8i9 提交3

3. 编辑提交信息

保存退出后,Git 会再次弹出编辑器编辑合并后的提交说明:

# This is a combination of 3 commits.
# The first commit's message is:

提交1
# The following commit message will also be included:
提交2
# The following commit message will also be included:
提交3

可以改成一句话,比如:

提交1

4. 完成 rebase

保存退出,Git 就会把最近 3 个提交合并成 1 个。

如果遇到冲突,按正常方式解决冲突后:

git add .
git rebase --continue

5. 推送到远程

如果之前这 3 个提交已经推到远程仓库,需要强制推送:

git push -f origin 你的分支名
posted @ 2025-09-28 18:00  [^1]  阅读(9)  评论(0)    收藏  举报