交互式的rebase的场景在分支开发时特别有用, 可以减少没有必要的提交
git rebase -i
-i 参数表示互动 interactive,这时 git 会使用你设定的编辑器,让你对 git 历史记录做详细修改。
下面以在master分支和dev分支之间的切换为例,演示git rebase -i
的使用
// 从master分支上拉取新的dev 分支用作开发
git checkout -b dev
// 在dev分支上修改一些文件并提交
git commit -a -m "feature: add new file"
// 在dev分支上修改bug, 再提交一次
git commit -a -m "fix: fix some bug"
// 这时看git记录,应该是有两次提交了,我们为了向master合并时代码整洁,打算将后面那次提交合并到上面的feature中,这样就需要我们的rebase -i 命令了
// 进行rebase -i
git rebase -i master
此时会显示
# 这里的两行表示最近的两次提交, pick表示保留这两次提交,我们只需要改成别的就行
pick 1d13e23 feature: add new file
pick f20aba9 fix: fix some bug
# Rebase 888d12f..f20aba9 onto 888d12f (2 command(s))
#
# 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 改成 fixup 表示放弃后面这次提交信息
git log查看时,只剩下一次提交信息了,fix那次commit message 被我们清除了
commit e2a543507533208b03f8b5dd84d9ef31703aca95
Author: tianrui1 <tianrui1@sina.com.cn>
Date: Sat Mar 25 23:42:08 2017 +0800
feature: add new file
这样就成功清除一次多余的提交信息,保证我们向master合并时,提交信息是最干净的
git rebase -i 其他交互命令
squash:将这一行的 commit 与上一个 commit 进行合并
fixup:与 squash 相同,只是不会保留这行 commit 的提交 message 信息
- reword: 重新更改提交信息
- edit: 拆分提交,使用edit以后,文件或重新放在暂存区,你可以拆分成多次提交
重排提交
你也可以使用交互式的衍合来彻底重排或删除提交。如果你想删除”added cat-file”这个提交并且修改其他两次提交引入的顺序,你将rebase脚本从这个
pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file
改为这个:
pick 310154e updated README formatting and added blame
pick f7f3f6d changed my name a bit
当你保存并退出编辑器,Git 将分支倒回至这些提交的父提交,应用310154e,然后f7f3f6d,接着停止。你有效地修改了这些提交的顺序并且彻底删除了”added cat-file”这次提交。
git commit –fixup & –autosquash
# 自动标记这一次的 commit 为上一个 commit 的 fix
$ git commit --fixup <commit>
# 自动组织合并两个 commit
$ git rebase -i --autosquash
这种方式和上面的第一种原理是一样的,只是形式上不是在编辑器中调整历史记录,而是直接执行命令,结果等同。
撤销过去的 commit 重建一个新的
$ git reset HEAD~2
$ git add .
$ git commit -am "This is the new feature"
$ git push --force
修改上一次 commit 提交的 message
有时我们提交代码,message 写的太匆忙有错字(不是代码里有错字,而是 commit message 写错),就可以用下面的命令来修正
$ git commit --amend
不过只能修正上一次的 commit。如果很多个 commit 之前就有 message 写错,就得用上我们之前说的 git rebase 了
$ git rebase -i HEAD~4
和之前一样,会打开编辑器,显示最近的 4 次提交
pick 07c5abd Introduce OpenPGP and teach basic usage
pick de9b1eb Fix PostChecker::Post#urls
pick 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend
将 pick 指令改为 reword 指令,就可以修改这一行的 commit message
pick 07c5abd Introduce OpenPGP and teach basic usage
pick de9b1eb Fix PostChecker::Post#urls
reword 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend
其中 HEAD~4 表示前4次提交
git show < commit-id >
查看这次提交修改的具体内容