-
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 记住你如何解决一个代码块冲突,以便下次它看到相同的冲突时,Git 可以自动为你解决它。
在很多情况下,这个功能非常有用。 文档中提到的一个例子是,当你想要确保一个长期存在的主题分支最终能干净地合并,但你不想让一堆中间合并提交弄乱你的提交历史。 启用 rerere
后,你可以尝试偶尔合并,解决冲突,然后退出合并。 如果你持续这样做,那么最终的合并应该很容易,因为 rerere
可以自动为你完成所有事情。
如果你想保持一个分支变基,这样你就不必每次都处理相同的变基冲突,你也可以使用相同的策略。 或者,如果你想采用你合并并修复了一堆冲突的分支,然后决定对其进行变基,你可能不必再次处理所有相同的冲突。
rerere
的另一个应用是,你偶尔会将一堆不断发展的主题分支合并到一个可测试的头中,就像 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
记录的 pre-merge 状态
$ 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
查看冲突的文件以及 before、left 和 right 版本
$ 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
文件中看到一个 hunk 冲突,其中一侧是 “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”。

现在,让我们撤消该合并,然后将其变基到我们的 master
分支之上。 我们可以使用 git reset
将我们的分支移回,正如我们在 Reset Demystified 中看到的那样。
$ 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

你也可以使用 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
缓存的解决方案自动重新解析了该文件。 你现在可以添加并继续变基以完成它。
$ git add hello.rb
$ git rebase --continue
Applying: i18n one word
因此,如果你进行了大量的重新合并,或者希望使主题分支与你的 master
分支保持同步而没有大量的合并,或者你经常变基,则可以启用 rerere
以帮助你更好地生活。