git 合并多个commit
一旦运行了'rebase -i'命令,你所预设的编辑器会被调用,其中含有如下的内容:
pick fc62e55 added file_size pick 9824bf4 fixed little thing pick 21d80a5 added number to log pick 76b9da6 added the apply command pick c264051 Revert "added file_size" - not implemented correctly # 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. #
直接将上边的pick改成 squash ,如果比较多的情况下,使用 (命令用法参考:https://www.cnblogs.com/tovep/articles/2715803.html)
:%s/vivian/sky/g (等同于 :g/vivian/s//sky/g) 替换每一行中所有 vivian 为 sky
进行批量修改,批量修改后,第一行的pick需要保留,也就是第一行被改成psquash的,需要改回来
现在你可以将操作(action)改为'edit'(使用提交,但是暂停以便进行修正)或者'squash'(使用提交,但是把它与前一提交合并),默认是'pick'(使用提交)。你可以对这些行上下移动从而对提交进行重排序。当你退出编辑器时,git会按照你指定的顺序去应用提交,并且做出相应的操作(action)。
pick fc62e55 added file_size squash 9824bf4 fixed little thing squash 21d80a5 added number to log squash 76b9da6 added the apply command squash c264051 Revert "added file_size" - not implemented correctly
你必须基于以下的提交信息创建一个新的提交信息:
# This is a combination of 5 commits. # The first commit's message is: added file_size # This is the 2nd commit message: fixed little thing # This is the 3rd commit message: added number to log # This is the 4th commit message: added the apply command # This is the 5th commit message: Revert "added file_size" - not implemented correctly This reverts commit fc62e5543b195f18391886b9f663d5a7eca38e84.
一旦你完成对提交信息的编辑并且退出编辑器,这个新的提交及提交信息会被保存起来。(这一步,first commit记录可以改成你要编辑的,其他的cimmit如果不需要,可以删除,接下来 :wq 保存,然后git log确认下,或者本地测试下,再 git push -f origin dev 远程分支)
备注:如果保存下来的commit message不是想要的,可以使用命令:
git commit --amend -m "your new message"
进行修改。
更多其他的,参考 https://stackoverflow.com/questions/179123/how-to-modify-existing-unpushed-commits
pick fc62e55 added file_size pick 9824bf4 fixed little thing edit 21d80a5 added number to log pick 76b9da6 added the apply command pick c264051 Revert "added file_size" - not implemented correctly
$ git reset HEAD^ $ git add file1 $ git commit 'first part of split commit' $ git add file2 $ git commit 'second part of split commit' $ git rebase --continue