首先我们初始化一个git仓库,进行多次提交。1
2
3
4
5
6
7$ mkdir test
$ cd test
$ git init
<!--重复进行commit操作-->
$ vim test.js
$ git add .
$ git commit -m 'commit +1'
之后我们可以看到使用git log
命令,查看git的历史,如下:1
2
3* commit f40fd657 - (HEAD -> master) commit +3
* commit 46c7ca3d - commit +2
* commit 220020c7 - commit +1
那么如何把f40fd657(commit +3)
、46c7ca3d(commit +2)
合并到一起,并且只保留46c7ca3d(commit +2)
的git message呢?
git rebase -i version
version为新的一个想保留的 Commit。
如上,我们的命令应该为:1
$ git rebase -i 220020c7
也可以通过HEAD~2
来指定对应的版本1
$ git rebase -i HEAD~2
执行后我们会得到如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21pick 46c7ca3 commit +2
pick f40fd65 commit +3
# Rebase 220020c..f40fd65 onto 220020c (2 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
前面两行是我们需要操作的commit和对应的操作command。其中squash
使用该 Commit,但会被合并到前一个 Commit 当中,fixup
和squash
类似,但是会忽略这条commit message。
修改对应的command,如下:1
2pick 46c7ca3 commit +2
s f40fd65 commit +3
执行后进入下一个界面1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23# This is a combination of 2 commits.
# This is the 1st commit message:
commit +2
# This is the commit message #2:
commit +3
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Tue Aug 28 11:18:18 2018 +0800
#
# interactive rebase in progress; onto 220020c
# Last commands done (2 commands done):
# pick 46c7ca3 commit +2
# s f40fd65 commit +3
# No commands remaining.
# You are currently rebasing branch 'master' on '220020c'.
#
# Changes to be committed:
# modified: test.js
这是一个编写 Commit Message 的界面,带 # 的行会被忽略掉,其余的行就会作为我们的新 Commit Message。
给commit +3
加上#,保存并退出。
最后再查看我们的git历史:1
2* commit ce42b094 - (HEAD -> master) commit +2
* commit 220020c7 - commit +1
完成!
一些rebase相关的命令:git rebase --abort
放弃这次压缩git rebase --continue
继续这次压缩
更多重写操作