赞
踩
一、背景介绍?:
某天组长帮你做Code Review,说你代码里多余的空格行可以删除了,并且添加个log,方便测试。Easy! 你说好的,喝了口水,然后上了个厕所,回来后删了空格,commit & push,然后意识到你没有添加log,然后继续在同一个branch上添加log,然后commit & push。
这时,你发现这是组长交代的一件事,不应该用两个commit来实现,为了掩盖自己犯下的这个“蠢事”,你就想着把这两个提交合并为一个。(如下图所示,目的:将同一个分支上的change 1提交和chang 2提交合并为一个提交)
第一个提交:change 1 第二个提交:change 2
二、OK,进入git command实际操作:
git rebase -i HEAD~2
- 1 pick 56a06ef change 1: remove one blank line
- 2 pick edbeab5 change 2: add log on MainActivity
- 3
- 4 # Rebase 23198ba..edbeab5 onto 23198ba (2 commands)
- 5 #
- 6 # Commands:
- 7 # p, pick <commit> = use commit
- 8 # r, reword <commit> = use commit, but edit the commit message
- 9 # e, edit <commit> = use commit, but stop for amending
- 10 # s, squash <commit> = use commit, but meld into previous commit
- 11 # f, fixup <commit> = like "squash", but discard this commit's log message
- 12 # x, exec <command> = run command (the rest of the line) using shell
- 13 # b, break = stop here (continue rebase later with 'git rebase --continue')
- 14 # d, drop <commit> = remove commit
- 15 # l, label <label> = label current HEAD with a name
- 16 # t, reset <label> = reset HEAD to a label
- 17 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
- 18 # . create a merge commit using the original merge commit's
- 19 # . message (or the oneline, if no original merge commit was
- 20 # . specified). Use -c <commit> to reword the commit message.
- 21 #
- 22 # These lines can be re-ordered; they are executed from top to bottom.
- 23 #
- 24 # If you remove a line here THAT COMMIT WILL BE LOST.
- 25 #
- 26 # However, if you remove everything, the rebase will be aborted.
- 27 #
- 28 # Note that empty commits are commented out
- 1 pick 56a06ef change 1: remove one blank line
- 2 s edbeab5 change 2: add log on MainActivity
- 3
- 4 # Rebase 23198ba..edbeab5 onto 23198ba (2 commands)
- 5 #
- 6 # Commands:
- 7 # p, pick <commit> = use commit
- 8 # r, reword <commit> = use commit, but edit the commit message
- 9 # e, edit <commit> = use commit, but stop for amending
- 10 # s, squash <commit> = use commit, but meld into previous commit
- 11 # f, fixup <commit> = like "squash", but discard this commit's log message
- 12 # x, exec <command> = run command (the rest of the line) using shell
- 13 # b, break = stop here (continue rebase later with 'git rebase --continue')
- 14 # d, drop <commit> = remove commit
- 15 # l, label <label> = label current HEAD with a name
- 16 # t, reset <label> = reset HEAD to a label
- 17 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
- 18 # . create a merge commit using the original merge commit's
- 19 # . message (or the oneline, if no original merge commit was
- 20 # . specified). Use -c <commit> to reword the commit message.
- 21 #
- 22 # These lines can be re-ordered; they are executed from top to bottom.
- 23 #
- 24 # If you remove a line here THAT COMMIT WILL BE LOST.
- 25 #
- 26 # However, if you remove everything, the rebase will be aborted.
- 27 #
- 28 # Note that empty commits are commented out
- 1 # This is a combination of 2 commits.
- 2 # This is the 1st commit message:
- 3
- 4 change 1: remove one blank line
- 5
- 6 # This is the commit message #2:
- 7
- 8 change 2: add log on MainActivity
- 9
- 10 # Please enter the commit message for your changes. Lines starting
- 11 # with '#' will be ignored, and an empty message aborts the commit.
- 12 #
- 13 # Date: Fri Apr 26 16:25:23 2019 +0800
- 14 #
- 15 # interactive rebase in progress; onto 23198ba
- 16 # Last commands done (2 commands done):
- 17 # pick 56a06ef change 1: remove one blank line
- 18 # squash edbeab5 change 2: add log on MainActivity
- 19 # No commands remaining.
- 20 # You are currently rebasing branch 'master' on '23198ba'.
- 21 #
- 22 # Changes to be committed:
- 23 # modified: app/src/main/java/com/example/jere/retrofit/MainActivity.java
- 24 #
- 1 # This is a combination of 2 commits.
- 2 # This is the 1st commit message:
- 3
- 4 change 1: remove one blank line && change 2: add log on MainActivity
- 5
- 6 # Please enter the commit message for your changes. Lines starting
- 7 # with '#' will be ignored, and an empty message aborts the commit.
- 8 #
- 9 # Date: Fri Apr 26 16:25:23 2019 +0800
- 10 #
- 11 # interactive rebase in progress; onto 23198ba
- 12 # Last commands done (2 commands done):
- 13 # pick 56a06ef change 1: remove one blank line
- 14 # squash edbeab5 change 2: add log on MainActivity
- 15 # No commands remaining.
- 16 # You are currently rebasing branch 'master' on '23198ba'.
- 17 #
- 18 # Changes to be committed:
- 19 # modified: app/src/main/java/com/example/jere/retrofit/MainActivity.java
- 20 #
保存退出后,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://www.jianshu.com/p/d8a9b26a1255
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。