一、背景介紹?:
某天組長幫你做Code Review,說你代碼里多余的空格行可以刪除了,并且添加個log,方便測試。Easy! 你說好的,喝了口水,然后上了個廁所,回來后刪了空格,commit & push,然后意識到你沒有添加log,然后繼續(xù)在同一個branch上添加log,然后commit & push。
這時,你發(fā)現(xiàn)這是組長交代的一件事,不應(yīng)該用兩個commit來實(shí)現(xiàn),為了掩蓋自己犯下的這個“蠢事”,你就想著把這兩個提交合并為一個。(如下圖所示,目的:將同一個分支上的change 1提交和chang 2提交合并為一個提交)

第一個提交:change 1 第二個提交:change 2
二、OK,進(jìn)入git command實(shí)際操作:
- 命令行輸入:git rebase -i HEAD~2 (i的意思是:interactive,HEAD2為在歷史的前兩個提交,同理,HEAD4就是歷史的前四個提交。)
git rebase -i HEAD~2
- vim出現(xiàn)如下所以文件信息,前面兩行就是你的提交信息,比如第一行分別對應(yīng)為: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出現(xiàn)如下所以文件信息:注意看:第4行和第8行分別對應(yīng)這你第一次和第二次的提交描述信息,這時你就要將這兩條描述信息合并為一條。
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 (注意:因?yàn)闀rrebase操作,所以要加-f, 強(qiáng)制push), 推送完成, 如下所以,完成將兩個提交合并為一個。
成功將change 1和change 2兩個提交合并為一個
三、整理branch上的commit的必要性
之前我一直覺得一個commit提交就是做一件事情,我是在同一個branch上提交的,最后我merge到master或相應(yīng)的branch上時都是會有這些提交記錄的,所以我也一直都不會去整理我的commit提交。所以正因?yàn)槿绱耍业腷ranch上常常會存在這樣的提交,比如:提交1‘finish login feature’,緊接著后面就是提交2‘code review for login feature’。這樣的操作其實(shí)很正常,我們都是在做好功能后,然后再去做code review,但其實(shí)我們完全可以將這兩個提交合并成一個提交,方便自己以及同事查看你的代碼。
概括:整理你的commit提交是一個很好的習(xí)慣,對團(tuán)隊(duì)合作開發(fā)百利無一害,而且整理的過程也是自己對開發(fā)這個功能的回顧思考,So,整理起來吧!
原文鏈接:https://blog.csdn.net/jerechen/article/details/89556281
