多人合作开发的时候就会因为本地的多个版本提交的很多杂乱的信息,导致git log查看的时候并不能很快的从一堆无意义的提交记录中找到我们想要的版本。
做完一个小功能随时提交是一个好习惯,但是这写小记录推送到远程就是个不大不小的麻烦

我们通过rebase命令将这些小版本合并成一个大的版本,然后推送到远程,前提是你的代码功能已经达到了。

  1. 查看log信息
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
commit 53097de5638d371da51bc6fef74a90ca1420d967
Author: GPF <5173180@qq.com>
Date:   Sun Dec 25 22:46:13 2016 +0800

    test3

commit 830026adb43506d0bc1172432f84639f84dae087
Author: GPF <5173180@qq.com>
Date:   Sun Dec 25 22:45:56 2016 +0800

    test2

commit 646f5be02ec285e30626f5682e6e7e9437762ac5
Author: GPF <5173180@qq.com>
Date:   Sun Dec 25 22:45:43 2016 +0800

    test1
...

比如我们就合并这前三个提交记录

  1. 开启rebase脚本
1
git rebase -i HEAD~3

HEAD~3的意思就是最近的san tiao三条提交信息

  1. 编辑脚本 执行步骤2的时候就会进入一个编辑脚本如下:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
pick 646f5be test1
pick 830026a test2
pick 53097de test3

# Rebase dbaf38a..53097de onto dbaf38a (3 commands)
#
# 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
# d, drop = remove commit
#
# 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

带#号的都是注释内容,这里提供了很多命令信息。最上面的三行就是我们要执行的命令,pick 查看注释是 use commit 可以简写成 p , 而且你看排列commit记录的顺序是最早的排在最上面,我们这次是为了合并commit,因此我们要用的命令就是squash 简写成 s 将脚本修改成这样:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
pick 646f5be test1
s 830026a test2
s 53097de test3

# Rebase dbaf38a..53097de onto dbaf38a (3 commands)
#
# 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
# d, drop = remove commit
#
# 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

我们合并最早的两个到test1版本当中然后:wq退出,注意合并记录的时候,不能pick最近的一条,会产生报错,如果把报错的话就输入命令git rebase --abort终止这次执行就好

  1. 接下来会直接进入一个commit记录编辑界面,#都是注释过的,将非注释的内容编辑成我们想要的然后:wq保存就完毕了