一、背景介绍?:
某天组长帮你做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 (i的意思是:interactive,HEAD2为在历史的前两个提交,同理,HEAD4就是历史的前四个提交。)
git rebase -i HEAD~2
- vim出现如下所以文件信息,前面两行就是你的提交信息,比如第一行分别对应为:pick(使用提交)、56a06ef(提交的ID)、change 1: remove one blank line(提交的描述信息)
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
- 将第二行的pick改成s, 也就是squash(挤压合并),作用是:使用提交,将此提交与之前的提交合并。 然后保存文件退出vim。
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
- 提交你的代码,git commit -a, vim出现如下所以文件信息:注意看:第4行和第8行分别对应这你第一次和第二次的提交描述信息,这时你就要将这两条描述信息合并为一条。
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 #
- 将之前的两条提交描述信息,修改合并为一条,然后保存退出vim,如下所示:
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://blog.csdn.net/jerechen/article/details/89556281