以编程方式显示合并期间使用的所有git rerere分辨率

时间:2018-09-27 11:59:11

标签: git git-merge git-diff git-rerere

在启用rerere的情况下进行合并,并且所有冲突都通过rerere解决后,它只会显示以下内容:

Resolved 'a.c' using previous resolution.
Resolved 'b.c' using previous resolution.
Resolved 'c.c' using previous resolution.

我想看看之前的差异(带有冲突标记)和之后的差异diff。我知道我可以做到:

git checkout -m a.c
git diff
git rerere
git diff
git checkout -m b.c
git diff
git rerere
git diff
...

可以看到每种分辨率所使用的前后图像。但是,有什么方法可以检索该信息或使用的重指纹,而无需为每个git merge hash消息手动解析Resolved X using previous resolution.输出。

2 个答案:

答案 0 :(得分:1)

感觉就像您要比较我们的他们的分辨率。从here看来git diff将拒绝比较3个文件。我已经修改了该帖子中的示例,以提供此示例;

git diff --name-only master feature |\
    xargs -I % bash -c "diff3 <( git show master:% ) <( cat % ) <( git show feature:% )"

diff3的输出更加复杂,但是您正在做的事情很复杂。

复制步骤

初始化存储库。

Horba@cake ~ $ git init MergeDiff/
Initialized empty Git repository in /home/Horba/MergeDiff/.git/
Horba@cake ~ $ cd MergeDiff/
Horba@cake ~/MergeDiff $ vim README.md
Horba@cake ~/MergeDiff $ git add README.md 
Horba@cake ~/MergeDiff $ git commit -m "Init."
[master (root-commit) 5250b3f] Init.
 1 file changed, 3 insertions(+)
 create mode 100644 README.md
Horba@cake ~/MergeDiff $ git branch feature
Horba@cake ~/MergeDiff $ git config rerere.enabled true
Horba@cake ~/MergeDiff $ git config rerere.autoupdate true
Horba@cake ~/MergeDiff $ vim README.md 
Horba@cake ~/MergeDiff $ git add README.md
Horba@cake ~/MergeDiff $ git commit -m "Work on master."
[master 8c12c43] Work on master.
 1 file changed, 1 insertion(+), 1 deletion(-)
Horba@cake ~/MergeDiff $ git checkout feature
Switched to branch 'feature'
Horba@cake ~/MergeDiff $ vim README.md
Horba@cake ~/MergeDiff $ git commit -m "Work on feature."
[feature 4b8ad3d] Work on feature.
 1 file changed, 1 insertion(+), 1 deletion(-)
Horba@cake ~/MergeDiff $ git checkout master
Switched to branch 'master'

进行初始合并。

Horba@cake ~/MergeDiff $ git merge feature 
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Recorded preimage for 'README.md'
Automatic merge failed; fix conflicts and then commit the result.
Horba@cake ~/MergeDiff $ cat README.md
# MergeDiff

<<<<<<< HEAD
Init - master changes
=======
Init - Work on feature
>>>>>>> feature

存储分辨率。

Horba@cake ~/MergeDiff $ vim README.md
Horba@cake ~/MergeDiff $ git rerere
Recorded resolution for 'README.md'.
Horba@cake ~/MergeDiff $ cat README.md
# MergeDiff

Init - Resolved
Horba@cake ~/MergeDiff $ git merge --abort

使用rerere.autoupdate再次合并。

Horba@cake ~/MergeDiff $ git merge feature 
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Staged 'README.md' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.
Horba@cake ~/MergeDiff $ cat README.md
# MergeDiff

Init - Resolved
Horba@cake ~/MergeDiff $ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

    modified:   README.md

传统差异无济于事。

Horba@cake ~/MergeDiff $ git diff feature master HEAD
diff --cc README.md
index ca2c305,ca2c305..24b898d
--- a/README.md
+++ b/README.md
@@@ -1,3 -1,3 +1,3 @@@
  # MergeDiff

--Init - master changes
++Init - Work on feature

diff3可以做到。

Horba@cake ~/MergeDiff $ diff3 <(git show master:README.md) <(git show feature:README.md) <( cat README.md )
====1
1:1,3c
  # MergeDiff

  Init - master changes
2:0a
3:0a
====2
1:6c
3:3c
  Init - Resolved
2:3c
  Init - Work on feature

答案 1 :(得分:1)

后映像和前映像存储在.git/rr-cache/$hex中。自动合并完成后,我们将拥有一个内容与后图像相同的文件。

这是一个快速且肮脏的bash函数,用于打印X的原像或HEAD版本与原像之间的差异。副作用是将创建悬挂的斑点。

# input: file path
# output: the preimage with conflict markers, 
#         or the diff between HEAD's version and the preimage. 
#         If the file path does not exist in rerere cache, return nothing.
function foo(){
    p=$1
    current=`git hash-object $p`
    for hex in `ls .git/rr-cache`;do
        posthash=`git hash-object .git/rr-cache/$hex/postimage`
        if [ "$posthash" = "$current" ];then
            git diff HEAD:$p $(git hash-object -w .git/rr-cache/$hex/preimage)
            # To print the preimage
            #cat .git/rr-cache/$hex/preimage
            return
        fi
    done
}

foo a.c