git rebase

本文的读者需要已经了解 基本的 Git 操作和开发流程

在我们开发完分支后,一般分支上会有很多 commit —— 少不了诸如 “fix typo”, “sth wrong in the previous commit” 之类的 commit。在合并到主干的时候,往往这类 commit 显得臃肿多余。为了方便别人做 code review,我们希望合并一些不必要的 commit 使我们的分支显得干净一目了然,也方便管理。有 3 种方式可以做到。

一、git rebase

$ git rebase -i origin/master

-i 参数表示互动 interactive,这时 git 会使用你设定的编辑器,让你对 git 历史记录做详细修改。

下面以 Atlassian 的例子来说明

# 开始开发一个新 feature
$ git checkout -b new-feature master
# 改了一些代码
$ git commit -a -m "Start developing a feature"
# 刚刚的修改有点问题,再改一下
$ git commit -a -m "Fix something from the previous commit"
 
# 紧急修复,直接在 master 分支上改点东西
$ git checkout master
# 改了一些代码
$ git commit -a -m "Fix security hole"
 
# 开始交互式地 rebase 了
$ git checkout new-feature
$ git rebase -i master

这时 git 会打开编辑器,你会看到 new-feature 分支上的 2 个最新 commit,以及一些指引提示

pick 32618c4 Start developing a feature
pick 62eed47 Fix something from the previous commit
 
# 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

每个 commit 前有一个操作命令,默认是 pick,表示该行被选中,需要进行 rebase 操作。下面一堆注释的指引中还有几个指令,我们常用到的是以下 2 个

  • squash:将这一行的 commit 与上一个 commit 进行合并
  • fixup:与 squash 相同,只是不会保留这行 commit 的提交 message 信息

比如上面的例子,我们在编辑器中修改指令为

pick 32618c4 Start developing a feature
squash 62eed47 Fix something from the previous commit

这样一改,保存执行后,new-feature 分支就只剩下 1 个 commit 了,这个合并后的 commit 提交的信息包含之前 2 个 commit 的信息

Start developing a feature
Fix something from the previous commit

消除了一个多余的不那么重要的 commit,达到了我们的目的。最后,只需 fast-forward merge 到 master

$ git checkout master
$ git merge new-feature

在别人看来,你就是一个天才开发者,没有出差错地一次实现了 new-feature,项目的 commit 历史记录显得干净而有意义。

如果我们在编辑器中不用 squash 指令,而使用 fixup 指令,这样改

pick 32618c4 Start developing a feature
fixup 62eed47 Fix something from the previous commit

则结果一样,还是只剩 1 个 commit,但是合并后的提交消息,就只有之前第一个 commit 的消息,第二个的 commit 消息被注释掉了

Start developing a feature

这样也达到了我们的目的,消除了曾经发生过 2 个 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

– EOF –

参考文档