1、 查看提交历史
git log
2、找到你想合并到哪一个commit_id
如:
commit_1
commit_2
commit_3
commit_4
commit_5
3、你想合并前4次,则输入:
git rebase -i commit_5
此时你预设的编辑器将会打开一个文档,其中含有如下类似内容:
pick fc62e55 fix bugs
pick 9824bf4 fixed bugs
pick 21d80a5 add file
pick 76b9da6 add file
# Rebase f408319..b04dc3d onto f408319# Commands:
# p, pick = use commit
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
4、将文档中第2行到第4行的pick 改为s, 保存、退出。
pick fc62e55 fix bugs
s 9824bf4 fixed bugs
s 21d80a5 add file
s 76b9da6 add file
# Rebase f408319..b04dc3d onto f408319# Commands:
# p, pick = use commit
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
5、此时你预设的编辑器将会打开一个文档,其中会包含你提交时的注释,删除后3次,保留最新一次,保存、退出。
6、推送到远程。
git push -f origin branch_name