最近在提交git项目中,又get到一项技能,发现自己提交次数有点多,想来合并以前的一些提交记录,减少提交次数。

命令如下:

git rebase -i HEAD~n  //n代表你要查看的提交记录条数

例如执行

git rebase -i HEAD~4

后会出现:

pick d167a86 提交记录1
pick c56d2e4 提交记录2
pick c56d2e4 提交记录3
pick c56d2e4 提交记录4
# Rebase 7a28439..c56d2e4 onto 7a28439 (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

可以看到我们提交的4条记录,假如,我需要将记录2、记录3、记录4合并到记录2(只能将最新的合并,即就是 记录1可以合并记录2,但是记录2不能合并记录1),我们将记录3、记录4中的pick改为squash然后保存更改退出后,然后会进入:

# This is a combination of 3 commits.
# This is the 1st commit message:

提交记录2

# This is the commit message #2:

提交记录3

# This is the commit message #3:
提交记录4

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Oct 12 14:58:12 2018 +0800
#
# interactive rebase in progress; onto 7a28439
# Last commands done (2 commands done):
#    pick d167a86 解决测试中提出的bug,修改etag缓存策略
#    squash c56d2e4 解决测试中的bug和修改UI布局,开发新漫画2.6.0版本
# No commands remaining.
# You are currently rebasing branch 'develope' on '7a28439'.
#
# Changes to be committed:
#       modified:   .gitignore
#       deleted:    .gradle/.DS_Store
#       deleted:    .gradle/2.14.1/.DS_Store
#       deleted:    .gradle/2.14.1/taskArtifacts/cache.properties
#       deleted:    .gradle/2.14.1/taskArtifacts/cache.properties.lock
#       deleted:    .gradle/2.14.1/taskArtifacts/fileHashes.bin
#       deleted:    .gradle/2.14.1/taskArtifacts/fileSnapshots.bin
#       deleted:    .gradle/2.14.1/taskArtifacts/fileSnapshotsToTreeSnapshotsIndex.bin
#       deleted:    .gradle/2.14.1/taskArtifacts/taskArtifacts.bin
#       deleted:    .gradle/2.14.1/tasks/_app_compileDebugJavaWithJavac/localClassSetAnalysis/localClassSetAnalysis.bin
#       deleted:    .gradle/2.14.1/tasks/_app_compileDebugJavaWithJavac/localClassSetAnalysis/localClassSetAnalysis.lock
#       deleted:    .gradle/2.14.1/tasks/_app_compileDebugJavaWithJavac/localJarClasspathSnapshot/localJarClasspathSnapshot.bin#       deleted:    .gradle/2.14.1/tasks/_app_compileDebugJavaWithJavac/localJarClasspathSnapshot/localJarClasspathSnapshot.lock

我们删除上面的"提交记录3、提交记录4"后,保存退出,然后执行:

git rebase --continue

执行完毕后会显示successful信息。