-
A1. 附录 A: Git 在其他环境
- A1.1 图形界面
- A1.2 Visual Studio 中的 Git
- A1.3 Visual Studio Code 中的 Git
- A1.4 IntelliJ / PyCharm / WebStorm / PhpStorm / RubyMine 中的 Git
- A1.5 Sublime Text 中的 Git
- A1.6 Bash 中的 Git
- A1.7 Zsh 中的 Git
- A1.8 PowerShell 中的 Git
- A1.9 小结
-
A2. 附录 B: 在应用程序中嵌入 Git
-
A3. 附录 C: Git 命令
7.9 Git 工具 - Rerere
Rerere
git rerere
功能是一个有点隐蔽的特性。这个名字代表“重用已记录的解决方案”(reuse recorded resolution),顾名思义,它允许你让 Git 记住你如何解决一个冲突块(hunk conflict),以便下次遇到相同的冲突时,Git 可以自动为你解决它。
在许多场景中,此功能会非常方便。文档中提到的一个例子是,当你想要确保一个长期存在的话题分支最终能干净地合并,但又不希望有大量中间合并提交来弄乱你的提交历史。启用 rerere
后,你可以偶尔尝试合并,解决冲突,然后撤销合并。如果你持续这样做,那么最终的合并就会很容易,因为 rerere
可以自动为你完成所有操作。
如果你想保持一个分支的 rebase 状态,这样你就不必每次 rebase 时都处理相同的冲突,那么也可以使用同样的策略。或者,如果你已经合并了一个分支并解决了一堆冲突,然后决定改为对其进行 rebase,你很可能不必再处理所有相同的冲突。
rerere
的另一个应用场景是,你偶尔会将一堆正在发展的话题分支合并到一个可测试的 HEAD 上,就像 Git 项目本身经常做的那样。如果测试失败,你可以回滚合并并重新进行,移除导致测试失败的话题分支,而无需再次解决冲突。
要启用 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”,就像之前一样。

当我们合并这两个分支时,我们会遇到一个合并冲突
$ 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'
你可以看到它“为 FILE 记录了解决方案”。

现在,让我们撤销该合并,然后将其 rebase 到我们的 master
分支之上。我们可以使用 git reset
将分支回溯,正如我们在揭秘 Git Reset 中看到的那样。
$ git reset --hard HEAD^
HEAD is now at ad63f15 i18n the hello
我们的合并已撤销。现在让我们 rebase 这个话题分支。
$ 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

你也可以使用 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
缓存的解决方案自动重新解决了文件。你现在可以添加并继续 rebase 来完成它。
$ git add hello.rb
$ git rebase --continue
Applying: i18n one word
因此,如果你经常进行重复合并,或者想要让话题分支与 master
分支保持最新而无需大量合并,或者你经常 rebase,那么可以启用 rerere
来让你的工作更轻松一些。