git 合并提交

目录

一、背景介绍?:

二、OK,进入git command实际操作:

三、整理branch上的commit的必要性


一、背景介绍?:

某天组长帮你做Code Review,说你代码里多余的空格行可以删除了,并且添加个log,方便测试。Easy!  你说好的,喝了口水,然后上了个厕所,回来后删了空格,commit & push,然后意识到你没有添加log,然后继续在同一个branch上添加log,然后commit & push。

这时,你发现这是组长交代的一件事,不应该用两个commit来实现,为了掩盖自己犯下的这个“蠢事”,你就想着把这两个提交合并为一个。(如下图所示,目的:将同一个分支上的change 1提交和chang 2提交合并为一个提交)

第一个提交:change 1 第二个提交:change 2

二、OK,进入git command实际操作:

1. 命令行输入:git rebase -i HEAD~2 (i的意思是:interactive,HEAD~2为在历史的前两个提交,同理,HEAD~4就是历史的前四个提交。)

git rebase -i HEAD~2
   
   

2. vim出现如下所以文件信息,前面两行就是你的提交信息,比如第一行分别对应为:pick(使用提交)、56a06ef(提交的ID)、change 1: remove one blank line(提交的描述信息)


   
   
  1. 1 pick 56a06ef change 1: remove one blank line
  2. 2 pick edbeab5 change 2: add log on MainActivity
  3. 3
  4. 4 # Rebase 23198ba..edbeab5 onto 23198ba (2 commands)
  5. 5 #
  6. 6 # Commands:
  7. 7 # p, pick <commit> = use commit
  8. 8 # r, reword <commit> = use commit, but edit the commit message
  9. 9 # e, edit <commit> = use commit, but stop for amending
  10. 10 # s, squash <commit> = use commit, but meld into previous commit
  11. 11 # f, fixup <commit> = like "squash", but discard this commit's log message
  12. 12 # x, exec <command> = run command (the rest of the line) using shell
  13. 13 # b, break = stop here (continue rebase later with 'git rebase --continue')
  14. 14 # d, drop <commit> = remove commit
  15. 15 # l, label <label> = label current HEAD with a name
  16. 16 # t, reset <label> = reset HEAD to a label
  17. 17 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
  18. 18 # . create a merge commit using the original merge commit's
  19. 19 # . message (or the oneline, if no original merge commit was
  20. 20 # . specified). Use -c <commit> to reword the commit message.
  21. 21 #
  22. 22 # These lines can be re-ordered; they are executed from top to bottom.
  23. 23 #
  24. 24 # If you remove a line here THAT COMMIT WILL BE LOST.
  25. 25 #
  26. 26 # However, if you remove everything, the rebase will be aborted.
  27. 27 #
  28. 28 # Note that empty commits are commented out

3. 将第二行的pick改成s, 也就是squash(挤压合并),作用是:使用提交,将此提交与之前的提交合并。   然后保存文件退出vim。


   
   
  1. 1 pick 56a06ef change 1: remove one blank line
  2. 2 s edbeab5 change 2: add log on MainActivity
  3. 3
  4. 4 # Rebase 23198ba..edbeab5 onto 23198ba (2 commands)
  5. 5 #
  6. 6 # Commands:
  7. 7 # p, pick <commit> = use commit
  8. 8 # r, reword <commit> = use commit, but edit the commit message
  9. 9 # e, edit <commit> = use commit, but stop for amending
  10. 10 # s, squash <commit> = use commit, but meld into previous commit
  11. 11 # f, fixup <commit> = like "squash", but discard this commit's log message
  12. 12 # x, exec <command> = run command (the rest of the line) using shell
  13. 13 # b, break = stop here (continue rebase later with 'git rebase --continue')
  14. 14 # d, drop <commit> = remove commit
  15. 15 # l, label <label> = label current HEAD with a name
  16. 16 # t, reset <label> = reset HEAD to a label
  17. 17 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
  18. 18 # . create a merge commit using the original merge commit's
  19. 19 # . message (or the oneline, if no original merge commit was
  20. 20 # . specified). Use -c <commit> to reword the commit message.
  21. 21 #
  22. 22 # These lines can be re-ordered; they are executed from top to bottom.
  23. 23 #
  24. 24 # If you remove a line here THAT COMMIT WILL BE LOST.
  25. 25 #
  26. 26 # However, if you remove everything, the rebase will be aborted.
  27. 27 #
  28. 28 # Note that empty commits are commented out

4. 提交你的代码,git commit -a, vim出现如下所以文件信息:注意看:第4行和第8行分别对应这你第一次和第二次的提交描述信息,这时你就要将这两条描述信息合并为一条。


   
   
  1. 1 # This is a combination of 2 commits.
  2. 2 # This is the 1st commit message:
  3. 3
  4. 4 change 1: remove one blank line
  5. 5
  6. 6 # This is the commit message #2:
  7. 7
  8. 8 change 2: add log on MainActivity
  9. 9
  10. 10 # Please enter the commit message for your changes. Lines starting
  11. 11 # with '#' will be ignored, and an empty message aborts the commit.
  12. 12 #
  13. 13 # Date: Fri Apr 26 16:25:23 2019 +0800
  14. 14 #
  15. 15 # interactive rebase in progress; onto 23198ba
  16. 16 # Last commands done (2 commands done):
  17. 17 # pick 56a06ef change 1: remove one blank line
  18. 18 # squash edbeab5 change 2: add log on MainActivity
  19. 19 # No commands remaining.
  20. 20 # You are currently rebasing branch 'master' on '23198ba'.
  21. 21 #
  22. 22 # Changes to be committed:
  23. 23 # modified: app/src/main/java/com/example/jere/retrofit/MainActivity.java
  24. 24 #

 5. 将之前的两条提交描述信息,修改合并为一条,然后保存退出vim,如下所示:


   
   
  1. 1 # This is a combination of 2 commits.
  2. 2 # This is the 1st commit message:
  3. 3
  4. 4 change 1: remove one blank line && change 2: add log on MainActivity
  5. 5
  6. 6 # Please enter the commit message for your changes. Lines starting
  7. 7 # with '#' will be ignored, and an empty message aborts the commit.
  8. 8 #
  9. 9 # Date: Fri Apr 26 16:25:23 2019 +0800
  10. 10 #
  11. 11 # interactive rebase in progress; onto 23198ba
  12. 12 # Last commands done (2 commands done):
  13. 13 # pick 56a06ef change 1: remove one blank line
  14. 14 # squash edbeab5 change 2: add log on MainActivity
  15. 15 # No commands remaining.
  16. 16 # You are currently rebasing branch 'master' on '23198ba'.
  17. 17 #
  18. 18 # Changes to be committed:
  19. 19 # modified: app/src/main/java/com/example/jere/retrofit/MainActivity.java
  20. 20 #

6. 保存退出后,push代码:git push origin master -f (注意:因为时rebase操作,所以要加-f, 强制push), 推送完成, 如下所以,完成将两个提交合并为一个。

成功将change 1和change 2两个提交合并为一个

三、整理branch上的commit的必要性

之前我一直觉得一个commit提交就是做一件事情,我是在同一个branch上提交的,最后我merge到master或相应的branch上时都是会有这些提交记录的,所以我也一直都不会去整理我的commit提交。所以正因为如此,我的branch上常常会存在这样的提交,比如:提交1‘finish login feature’,紧接着后面就是提交2‘code review for login feature’。这样的操作其实很正常,我们都是在做好功能后,然后再去做code review,但其实我们完全可以将这两个提交合并成一个提交,方便自己以及同事查看你的代码。

概括:整理你的commit提交是一个很好的习惯,对团队合作开发百利无一害,而且整理的过程也是自己对开发这个功能的回顾思考,So,整理起来吧!

 

 

[转载自](https://blog.csdn.net/jerechen/article/details/89556281?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值