使用git rebase合并多次commit

转自:https://blog.csdn.net/yangcs2009/article/details/47166361

1.首先使用git log查看一下提交历史

[plain] view plain copy
  1. [[email protected]:zh_cn(bugfix/ycs-MOS-1503-notify-template-table-center)]$ git log  
  2. commit 5e187c7dbe84af67ad19823a54f3cc3e3f6d6940  
  3. Author: yangcs2009 <[email protected]>  
  4. Date:   Thu Jul 30 20:48:15 2015 +0800  
  5.   
  6.     add center style indent  
  7.   
  8. commit 6d577eb344080d7e3593733ac8dcb622de22b492  
  9. Author: yangcs2009 <[email protected]>  
  10. Rebasing (4/4)  
  11. Date:   Thu Jul 30 20:30:20 2015 +0800  
  12.   
  13.     add center style  
  14.   
  15. commit f9b9508a3ab634f8c8a2d28ab844a3a87d8e30ab  
  16. Author: yangcs2009 <[email protected]>  
  17. Date:   Thu Jul 30 20:16:35 2015 +0800  
  18.   
  19.     add center style  
  20.   
  21. commit 111ab9cc26101f7c6972de3dccfef2836a95efe0  
  22. Author: yangcs2009 <[email protected]>  
  23. Date:   Thu Jul 30 18:57:46 2015 +0800  
  24.   
  25.     update templates  
这样在git中看到的是4次提交,有点冗余,需要做的是将4次commit合并为一次

2. git 压缩  git rebase -i HEAD~4

该命令执行后,会弹出一个编辑窗口,4次提交的commit倒序排列,最上面的是最早的提交,最下面的是最近一次提交。

[plain] view plain copy
  1. pick 5e187c7dbe8    add center style indent  
  2. pick 6d577eb3440    add center style  
  3. pick f9b9508a3ab    add center style  
  4. pick 111ab9cc261    update templates  
  5. # Rebase 150a643..2fad1ae onto 150a643  
  6. #  
  7. # Commands:  
  8. #  p, pick = use commit  
  9. #  r, reword = use commit, but edit the commit message  
  10. #  e, edit = use commit, but stop for amending  
  11. #  s, squash = use commit, but meld into previous commit  
  12. #  f, fixup = like "squash", but discard this commit's log message  
  13. #  x, exec = run command (the rest of the line) using shell  
  14. #  
  15. # These lines can be re-ordered; they are executed from top to bottom.  
  16. #  
  17. # If you remove a line here THAT COMMIT WILL BE LOST.  
  18. #  
  19. # However, if you remove everything, the rebase will be aborted.  
  20. #  
  21. # Note that empty commits are commented out  
[plain] view plain copy
  1. pick 5e187c7dbe8    add center style indent  
  2. squash 6d577eb3440  add center style  
  3. squash f9b9508a3ab  add center style  
  4. squash 111ab9cc261  update templates  
  5. # Rebase 150a643..2fad1ae onto 150a643  
  6. #  
  7. # Commands:  
  8. #  p, pick = use commit  
  9. #  r, reword = use commit, but edit the commit message  
  10. #  e, edit = use commit, but stop for amending  
  11. #  s, squash = use commit, but meld into previous commit  
  12. #  f, fixup = like "squash", but discard this commit's log message  
  13. #  x, exec = run command (the rest of the line) using shell  
  14. #  
  15. # These lines can be re-ordered; they are executed from top to bottom.  
  16. #  
  17. # If you remove a line here THAT COMMIT WILL BE LOST.  
  18. #  
  19. # However, if you remove everything, the rebase will be aborted.  
  20. #  
  21. # Note that empty commits are commented out  
修改第2-4行的第一个单词pick为squash,当然看一下里面的注释就理解含义了。

然后保存退出,git会压缩提交历史,如果有冲突,需要修改,修改的时候要注意,保留最新的历史,不然我们的修改就丢弃了。修改以后要记得敲下面的命令:

[plain] view plain copy
  1. git add .  
  2. git rebase --continue  

如果你想放弃这次压缩的话,执行以下命令:

[plain] view plain copy
  1. git rebase --abort  

如果没有冲突,或者冲突已经解决,则会出现如下的编辑窗口:

[plain] view plain copy
  1. # This is a combination of 4 commits.  
  2. # The first commit’s message is:  
  3. add center style indent  
  4.   
  5. # The 2nd commit’s message is:  
  6. add center style  
  7.   
  8. # The 3rd commit’s message is:  
  9. add center style  
  10.   
  11. # The 4th commit’s message is:  
  12. update templates  
  13.   
  14. # Please enter the commit message for your changes. Lines starting  
  15. # with ‘#’ will be ignored, and an empty message aborts the commit.  

3.同步到远程git仓库

不过此时远程的信息仍未改变,下面操作会把修改同步到远程git仓库

[python] view plain copy
  1. [[email protected]:zh_cn(bugfix/ycs-MOS-1503-notify-template-table-center)]$ git push -f  
  2. Enter passphrase for key '/home/demo/.ssh/id_rsa':  
  3. Counting objects: 1, done.  
  4. Writing objects: 100% (1/1), 223 bytes | 0 bytes/s, done.  
  5. Total 1 (delta 0), reused 0 (delta 0)  
  6. remote:  
  7. remote: View pull request for bugfix/ycs-MOS-1503-notify-template-table-center => release/1.1.3:  
  8. remote:   http://git.sankuai.com/projects/SA/repos/cloud/pull-requests/1042  
  9. remote:  
  10. To ssh://[email protected]/sa/cloud.git  
  11.  + 5e187c7...8d26431 bugfix/ycs-MOS-1503-notify-template-table-center -> bugfix/ycs-MOS-1503-notify-template-table-center (forced update)  

4.查看远程git仓库效果

使用git rebase合并多次commit使用git rebase合并多次commit

1.首先使用git log查看一下提交历史

[plain] view plain copy
  1. [[email protected]:zh_cn(bugfix/ycs-MOS-1503-notify-template-table-center)]$ git log  
  2. commit 5e187c7dbe84af67ad19823a54f3cc3e3f6d6940  
  3. Author: yangcs2009 <[email protected]>  
  4. Date:   Thu Jul 30 20:48:15 2015 +0800  
  5.   
  6.     add center style indent  
  7.   
  8. commit 6d577eb344080d7e3593733ac8dcb622de22b492  
  9. Author: yangcs2009 <[email protected]>  
  10. Rebasing (4/4)  
  11. Date:   Thu Jul 30 20:30:20 2015 +0800  
  12.   
  13.     add center style  
  14.   
  15. commit f9b9508a3ab634f8c8a2d28ab844a3a87d8e30ab  
  16. Author: yangcs2009 <[email protected]>  
  17. Date:   Thu Jul 30 20:16:35 2015 +0800  
  18.   
  19.     add center style  
  20.   
  21. commit 111ab9cc26101f7c6972de3dccfef2836a95efe0  
  22. Author: yangcs2009 <[email protected]>  
  23. Date:   Thu Jul 30 18:57:46 2015 +0800  
  24.   
  25.     update templates  
这样在git中看到的是4次提交,有点冗余,需要做的是将4次commit合并为一次

2. git 压缩  git rebase -i HEAD~4

该命令执行后,会弹出一个编辑窗口,4次提交的commit倒序排列,最上面的是最早的提交,最下面的是最近一次提交。

[plain] view plain copy
  1. pick 5e187c7dbe8    add center style indent  
  2. pick 6d577eb3440    add center style  
  3. pick f9b9508a3ab    add center style  
  4. pick 111ab9cc261    update templates  
  5. # Rebase 150a643..2fad1ae onto 150a643  
  6. #  
  7. # Commands:  
  8. #  p, pick = use commit  
  9. #  r, reword = use commit, but edit the commit message  
  10. #  e, edit = use commit, but stop for amending  
  11. #  s, squash = use commit, but meld into previous commit  
  12. #  f, fixup = like "squash", but discard this commit's log message  
  13. #  x, exec = run command (the rest of the line) using shell  
  14. #  
  15. # These lines can be re-ordered; they are executed from top to bottom.  
  16. #  
  17. # If you remove a line here THAT COMMIT WILL BE LOST.  
  18. #  
  19. # However, if you remove everything, the rebase will be aborted.  
  20. #  
  21. # Note that empty commits are commented out  
[plain] view plain copy
  1. pick 5e187c7dbe8    add center style indent  
  2. squash 6d577eb3440  add center style  
  3. squash f9b9508a3ab  add center style  
  4. squash 111ab9cc261  update templates  
  5. # Rebase 150a643..2fad1ae onto 150a643  
  6. #  
  7. # Commands:  
  8. #  p, pick = use commit  
  9. #  r, reword = use commit, but edit the commit message  
  10. #  e, edit = use commit, but stop for amending  
  11. #  s, squash = use commit, but meld into previous commit  
  12. #  f, fixup = like "squash", but discard this commit's log message  
  13. #  x, exec = run command (the rest of the line) using shell  
  14. #  
  15. # These lines can be re-ordered; they are executed from top to bottom.  
  16. #  
  17. # If you remove a line here THAT COMMIT WILL BE LOST.  
  18. #  
  19. # However, if you remove everything, the rebase will be aborted.  
  20. #  
  21. # Note that empty commits are commented out  
修改第2-4行的第一个单词pick为squash,当然看一下里面的注释就理解含义了。

然后保存退出,git会压缩提交历史,如果有冲突,需要修改,修改的时候要注意,保留最新的历史,不然我们的修改就丢弃了。修改以后要记得敲下面的命令:

[plain] view plain copy
  1. git add .  
  2. git rebase --continue  

如果你想放弃这次压缩的话,执行以下命令:

[plain] view plain copy
  1. git rebase --abort  

如果没有冲突,或者冲突已经解决,则会出现如下的编辑窗口:

[plain] view plain copy
  1. # This is a combination of 4 commits.  
  2. # The first commit’s message is:  
  3. add center style indent  
  4.   
  5. # The 2nd commit’s message is:  
  6. add center style  
  7.   
  8. # The 3rd commit’s message is:  
  9. add center style  
  10.   
  11. # The 4th commit’s message is:  
  12. update templates  
  13.   
  14. # Please enter the commit message for your changes. Lines starting  
  15. # with ‘#’ will be ignored, and an empty message aborts the commit.  

3.同步到远程git仓库

不过此时远程的信息仍未改变,下面操作会把修改同步到远程git仓库

[python] view plain copy
  1. [[email protected]:zh_cn(bugfix/ycs-MOS-1503-notify-template-table-center)]$ git push -f  
  2. Enter passphrase for key '/home/demo/.ssh/id_rsa':  
  3. Counting objects: 1, done.  
  4. Writing objects: 100% (1/1), 223 bytes | 0 bytes/s, done.  
  5. Total 1 (delta 0), reused 0 (delta 0)  
  6. remote:  
  7. remote: View pull request for bugfix/ycs-MOS-1503-notify-template-table-center => release/1.1.3:  
  8. remote:   http://git.sankuai.com/projects/SA/repos/cloud/pull-requests/1042  
  9. remote:  
  10. To ssh://[email protected]/sa/cloud.git  
  11.  + 5e187c7...8d26431 bugfix/ycs-MOS-1503-notify-template-table-center -> bugfix/ycs-MOS-1503-notify-template-table-center (forced update)  

4.查看远程git仓库效果

使用git rebase合并多次commit使用git rebase合并多次commit