Git 清理commits历史记录

1、使用git log列出日志

git log

2、选择需要合并到的commit SHA1码,如下:

commit d6d0394e9d27fcbedcfabfad57270f821829e877
Author: luming <luming@mlogcn.com>
Date:   Mon Nov 28 16:47:19 2016 +0800

    the 4 commit

commit 3245cd1ddd5dce1d28f55d478f7d3a0f83453556
Author: luming <luming@mlogcn.com>
Date:   Mon Nov 28 16:47:03 2016 +0800

    the 3 commit

commit f0898b699dd35cbed48a27886667714abe49faab
Author: luming <luming@mlogcn.com>
Date:   Mon Nov 28 16:46:46 2016 +0800

    the 2 commit

commit 1354bde57f420690d9bc6cc3b9f1818679176456
Author: luming <luming@mlogcn.com>
Date:   Mon Nov 28 16:46:08 2016 +0800

    first commit

在这里我们需要将前三次提交合并成一次提交

git rebase -i 1354bde57f420690d9bc6cc3b9f1818679176456

3、修改弹出文本:

pick f0898b6 the 2 commit
squash 3245cd1 the 3 commit
squash d6d0394 the 4 commit

4、保存退出。

5、在新弹出的窗口中将会看到合并前的提交信息:

# This is a combination of 3 commits.
# The first commit's message is:
the 2 commit

# This is the 2nd commit message:

the 3 commit

# This is the 3rd commit message:

the 4 commit

将信息合并:

修改成:this is a new commit

6、保存退出。然后使用git log查看日志信息:

git log

commit 085cd1cb7334e0e6ea7b8e6e3a4d052c2f9ffe74
Author: luming <luming@mlogcn.com>
Date:   Mon Nov 28 16:46:46 2016 +0800

    this is a new commit

commit 1354bde57f420690d9bc6cc3b9f1818679176456
Author: luming <luming@mlogcn.com>
Date:   Mon Nov 28 16:46:08 2016 +0800

    first commit

至此,我们已经合并完成。