章节 ▾ 第二版

7.9 Git 工具 - Rerere

Rerere

git rerere 功能是一个鲜为人知的特性。它的名字代表“重用记录的解决方案”(reuse recorded resolution),顾名思义,它允许你让 Git 记住你曾如何解决一个块冲突,这样下次 Git 看到同样的冲突时,就可以为你自动解决它。

这种功能在许多场景下都非常有用。文档中提到的一个例子是,当你想要确保一个长期存在的主题分支最终能干净地合并,但又不想在提交历史中留下大量中间合并提交时。启用 rerere 后,你可以尝试偶尔进行合并、解决冲突,然后撤销合并。如果你持续这样做,最后的合并就会变得很容易,因为 rerere 可以自动为你完成所有工作。

如果你想保持一个分支的变基(rebase)状态,以便每次变基时不必处理同样的冲突,也可以使用同样的策略。或者,如果你想对一个已经合并并解决了大量冲突的分支进行变基——你很可能不需要再次处理所有相同的冲突。

rerere 的另一个应用场景是,当你像 Git 项目本身经常做的那样,偶尔将一堆演进中的主题分支合并到一个可测试的头(head)上。如果测试失败,你可以回滚合并,并在不包含导致测试失败的主题分支的情况下重新进行合并,而无需再次解决冲突。

要启用 rerere 功能,只需运行此配置设置:

$ git config --global rerere.enabled true

你也可以通过在特定仓库中创建 .git/rr-cache 目录来开启它,但使用配置设置更直观,且能为你全局启用该功能。

现在让我们看一个简单的例子,类似于之前的那个。假设我们有一个名为 hello.rb 的文件,内容如下:

#! /usr/bin/env ruby

def hello
  puts 'hello world'
end

在一个分支中,我们将“hello”改为“hola”,然后在另一个分支中,我们将“world”改为“mundo”,就像之前一样。

Two branches changing the same part of the same file differently
图 160. 两个分支以不同方式更改同一文件的同一部分

当我们合并这两个分支时,会出现合并冲突:

$ git merge i18n-world
Auto-merging hello.rb
CONFLICT (content): Merge conflict in hello.rb
Recorded preimage for 'hello.rb'
Automatic merge failed; fix conflicts and then commit the result.

你应该注意其中新出现的一行 Recorded preimage for FILE。除此之外,它看起来应该和普通的合并冲突完全一样。此时,rerere 可以告诉我们一些事情。通常情况下,你可能会在此时运行 git status 查看哪些文件冲突了:

$ git status
# On branch master
# Unmerged paths:
#   (use "git reset HEAD <file>..." to unstage)
#   (use "git add <file>..." to mark resolution)
#
#	both modified:      hello.rb
#

然而,git rerere 还会通过 git rerere status 告诉你它记录了哪些合并前的状态:

$ git rerere status
hello.rb

git rerere diff 将显示当前的解决状态——你开始解决时的情况以及你将其解决后的结果。

$ git rerere diff
--- a/hello.rb
+++ b/hello.rb
@@ -1,11 +1,11 @@
 #! /usr/bin/env ruby

 def hello
-<<<<<<<
-  puts 'hello mundo'
-=======
+<<<<<<< HEAD
   puts 'hola world'
->>>>>>>
+=======
+  puts 'hello mundo'
+>>>>>>> i18n-world
 end

此外(这与 rerere 关系不大),你可以使用 git ls-files -u 查看冲突文件以及它们的前身、左侧和右侧版本:

$ git ls-files -u
100644 39804c942a9c1f2c03dc7c5ebcd7f3e3a6b97519 1	hello.rb
100644 a440db6e8d1fd76ad438a49025a9ad9ce746f581 2	hello.rb
100644 54336ba847c3758ab604876419607e9443848474 3	hello.rb

现在你可以将其解决为 puts 'hola mundo',然后再次运行 git rerere diff 查看 rerere 将会记住什么:

$ git rerere diff
--- a/hello.rb
+++ b/hello.rb
@@ -1,11 +1,7 @@
 #! /usr/bin/env ruby

 def hello
-<<<<<<<
-  puts 'hello mundo'
-=======
-  puts 'hola world'
->>>>>>>
+  puts 'hola mundo'
 end

基本意思就是:当 Git 在 hello.rb 文件中看到一个块冲突,且一边是“hello mundo”,另一边是“hola world”时,它会自动将其解决为“hola mundo”。

现在我们可以将其标记为已解决并提交它:

$ git add hello.rb
$ git commit
Recorded resolution for 'hello.rb'.
[master 68e16e5] Merge branch 'i18n'

你可以看到它显示了 "Recorded resolution for FILE"(已记录文件解决方案)。

Recorded resolution for FILE
图 161. 已记录文件解决方案

现在,让我们撤销该合并,然后将其变基到 master 分支之上。我们可以使用 git reset 将分支回滚,正如我们在 重置揭秘 中看到的那样。

$ git reset --hard HEAD^
HEAD is now at ad63f15 i18n the hello

我们的合并已撤销。现在让我们变基该主题分支:

$ git checkout i18n-world
Switched to branch 'i18n-world'

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: i18n one word
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging hello.rb
CONFLICT (content): Merge conflict in hello.rb
Resolved 'hello.rb' using previous resolution.
Failed to merge in the changes.
Patch failed at 0001 i18n one word

现在,我们得到了预期的合并冲突,但请看 Resolved FILE using previous resolution 这一行。如果我们查看该文件,会发现它已经被解决了,文件中没有合并冲突标记。

#! /usr/bin/env ruby

def hello
  puts 'hola mundo'
end

此外,git diff 将向你展示它是如何被自动重新解决的:

$ git diff
diff --cc hello.rb
index a440db6,54336ba..0000000
--- a/hello.rb
+++ b/hello.rb
@@@ -1,7 -1,7 +1,7 @@@
  #! /usr/bin/env ruby

  def hello
-   puts 'hola world'
 -  puts 'hello mundo'
++  puts 'hola mundo'
  end
Automatically resolved merge conflict using previous resolution
图 162. 使用先前的解决方案自动解决合并冲突

你也可以使用 git checkout 重新创建冲突的文件状态:

$ git checkout --conflict=merge hello.rb
$ cat hello.rb
#! /usr/bin/env ruby

def hello
<<<<<<< ours
  puts 'hola world'
=======
  puts 'hello mundo'
>>>>>>> theirs
end

我们在 高级合并 中看到了这个例子。不过现在,让我们通过再次运行 git rerere 来重新解决它:

$ git rerere
Resolved 'hello.rb' using previous resolution.
$ cat hello.rb
#! /usr/bin/env ruby

def hello
  puts 'hola mundo'
end

我们已经使用 rerere 缓存的解决方案自动重新解决了该文件。你现在可以添加(add)并继续执行变基以完成它。

$ git add hello.rb
$ git rebase --continue
Applying: i18n one word

所以,如果你进行了大量的重新合并,或者想在不进行大量合并的情况下使主题分支与 master 分支保持同步,或者你经常进行变基,那么你可以开启 rerere 来让生活轻松一些。