Git 将两个提交合并为一个

一、背景介绍?:
某天组长帮你做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,HEAD2为在历史的前两个提交,同理,HEAD4就是历史的前四个提交。)

git rebase -i HEAD~2

  1. 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
  1. 将第二行的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
  1. 提交你的代码,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 #
  1. 将之前的两条提交描述信息,修改合并为一条,然后保存退出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 #
  1. 保存退出后,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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容