先使用git log
查看commit记录,
localhost:appro icourt$ git log
commit cb44e8fea0c201cfeb8cfce7433ab4e65ef70e50 (HEAD -> test)
Merge: b397d25 3fffafc
Author: 张晓阳 <xiaoyangzhang1990@163.com>
Date: Thu Feb 22 19:24:16 2018 +0800
冲突
commit 3fffafcd01411740096892c0c222d2c188471098 (origin/zhangxy-appro-comment-fixbug, zhangxy-appro-comment-fixbug)
Author: 张晓阳 <xiaoyangzhang1990@163.com>
Date: Thu Feb 22 19:22:02 2018 +0800
评论没有通知流程发起人
commit b397d253b8b013c62114c206e0eaf5ec97706ea7
Author: 张晓阳 <xiaoyangzhang1990@163.com>
Date: Thu Feb 22 19:20:16 2018 +0800
评论没有通知流程发起人
commit 13de34376dc4880cd787da790f7c083908c18abb (origin/test)
执行git rebase -i HEAD~3
(这里是你要合并的commit的个数),会出现下面的内容:
pick cb44e8fea0c add center style indent
pick 3fffafcd014 add center style
pick b397d253b8b 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
从第2行开始的提交,将pick
改为squash
,
当前我们只要知道 pick 和 squash 这两个命令即可。
pick 的意思是要会执行这个 commit
squash 的意思是这个 commit 会被合并到前一个commit
输入wq
保存并退出,再使用git log
会发现commit已经合并成了一个新的commit。
如果合并后有冲突,修改冲突后可以使用git rebase --continue
,使合并进行下去。
注意:如果这个过程中有操作错误,可以使用 git rebase --abort
来撤销修改,回到没有开始操作合并之前的状态。
如果要删除某个commit,比如3fffafcd01411740096892c0c222d2c188471098
:
1. 首先找到此次提交之前的一次提交的commit 7753f40d892a8e0d14176a42f6e12ae0179a3210
2. 执行如下命令
git rebase -i 7753f40d892
,
会出现如上图所示的内容,将要删除的commit的pick
改为drop
,输入wq
保存并退出即可,使用git log
查看提交历史,会发现那个commit已经没有 了。