使用git rebase合并多次commit

有时候我们因为一些小小的改动就提交了代码,导致产生了很多次commit,而其中一些commit的改动内容可以使用相同的注释,所以我们需要合并这几次提交

查看提交历史

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
git log
commit 5e187c7dbe84af67ad19823a54f3cc3e3f6d6940
Author: yangcs2009 <yangchangsheng@meituan.com>
Date: Thu Jul 30 20:48:15 2015 +0800
add center style indent
commit 6d577eb344080d7e3593733ac8dcb622de22b492
Author: yangcs2009 <yangchangsheng@meituan.com>
Rebasing (4/4)
Date: Thu Jul 30 20:30:20 2015 +0800
add center style
commit f9b9508a3ab634f8c8a2d28ab844a3a87d8e30ab
Author: yangcs2009 <yangchangsheng@meituan.com>
Date: Thu Jul 30 20:16:35 2015 +0800
add center style
commit 111ab9cc26101f7c6972de3dccfef2836a95efe0
Author: yangcs2009 <yangchangsheng@meituan.com>
Date: Thu Jul 30 18:57:46 2015 +0800
update templates

我们看到了四次提交,后三次注释都差不多,有点冗余,我们需要把后三次的commit合并为一次

合并commit

运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
git rebase -i head~3
pick 5e187c7dbe8 add center style indent
pick 6d577eb3440 add center style
pick f9b9508a3ab add center style
# Rebase 150a643..2fad1ae onto 150a643
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

上面是三次提交commit倒序排列,最上面是最早的提交,由注释我们下面两行的pick改为squash,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pick 5e187c7dbe8 add center style indent
squash 6d577eb3440 add center style
squash f9b9508a3ab add center style
# Rebase 150a643..2fad1ae onto 150a643
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

然后保存退出,git会压缩提交提示,如果有冲突需要修改,修改的时候要注意,保留最新的历史,不然我们的修改就丢弃了,冲突改完后

1
2
git add -A .
git rebase --continue

如果你想放弃这次压缩的话就执行

1
git rebase --abort

如果没有冲突,或者冲突已经解决,则会出现如下的编辑窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
# This is a combination of 4 commits.
# The first commit’s message is:
add center style indent
# The 2nd commit’s message is:
add center style
# The 3rd commit’s message is:
add center style
# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.

保留你需要的message,或者把它们都移除,重新写,然后保存退出

同步到远程仓库

此时远程仓库的信息任未改变,所以需要和远程git仓库进行同步

1
git push -f

大功告成!!!!!!

佘伟春 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!

热评文章