如何在手动解决冲突后立即获得类似的Git diff?

oalqel3c  于 12个月前  发布在  Git
关注(0)|答案(1)|浏览(99)

我使用Git已经很长时间了,有时需要手动解决冲突。
当合并由于合并失败而停止时,我手动解决冲突。此时我可以调用git diff,它只显示那些冲突并由我手动解决的差异。
但是在合并提交之后,看起来我没有办法再看到这样的diff了。最相似的是三向diff,它显示了两个分支的所有更改,无论它们是冲突和手动解决的,还是自动合并的。
有没有人发现了一些技巧,在合并提交之前立即看到这样的diff - made,- again?
下面是一个测试案例,演示了我的问题。
要在bash或sh中执行的文件test-git-merge.sh,来自为测试创建的空目录:

#/bin/sh

git init
git config user.email "[email protected]"
git config user.name "Vsevolod Novikov"
echo a1>>a.txt
echo a2>>a.txt
echo a3>>a.txt
echo b1>>b.txt
echo b2>>b.txt
echo b3>>b.txt
git add a.txt b.txt
git commit -a -m "Initial"
git checkout -b branch-x
echo a1>a.txt
echo "branch-x" >>a.txt
echo a2>>a.txt
echo a3>>a.txt
echo "branch-x" >>b.txt
git commit -a -m "Branch X"
git checkout master
git checkout -b branch-y
echo "branch-y" >>d.txt
git add d.txt
echo a1>a.txt
echo a2>>a.txt
echo "branch-y" >>a.txt
echo a3>>a.txt
echo "branch-y" >>b.txt
git commit -a -m "Branch Y"
git checkout branch-x
git checkout -b branch-z
git merge branch-y
echo "------------- Diff after merge conflict --------------"
git diff
echo b1>b.txt
echo b2>>b.txt
echo b3>>b.txt
echo "branch-x" >>b.txt
echo "branch-y" >>b.txt
echo "------------- Diff after merge conflict resolve --------------"
git diff
git commit -a -m "Fixed merge"
echo "------------- DIff for merge commit --------------"
git diff HEAD HEAD^@

字符串

  • 合并冲突解决后的差异-
diff --cc b.txt
index dac91e4,22e151d..0000000
--- a/b.txt
+++ b/b.txt
@@@ -1,4 -1,4 +1,5 @@@
  b1
  b2
  b3
 +branch-x
+ branch-y

  • Diff for merge commit -
diff --cc a.txt
index 45f2c54,dd210a3..4164b20
--- a/a.txt
+++ b/a.txt
@@@ -1,4 -1,4 +1,5 @@@
  a1
 +branch-x
  a2
+ branch-y
  a3
diff --cc b.txt
index dac91e4,22e151d..7aaeeec
--- a/b.txt
+++ b/b.txt
@@@ -1,4 -1,4 +1,5 @@@
  b1
  b2
  b3
 +branch-x
+ branch-y


正如你所看到的,后一个diff有一个a.txt文件的额外的不必要的部分,它是自动解析的。

hgb9j2n6

hgb9j2n61#

Diff after merge conflict resolve的时刻,a.txt被分级,而b.txt没有。因此,git diff只输出b.txt的diff。
为了只输出b.txt的diff,我们需要将b.txt附加到git diff HEAD HEAD^@

git diff HEAD HEAD^@ -- b.txt

字符串
但是手动指定b.txt并不明智,我们可以在合并过程中找出有冲突的文件。

git show HEAD --pretty= --remerge-diff --name-only


把它们放在一起。

git diff HEAD HEAD^@ -- $(git show HEAD --pretty= --remerge-diff --name-only)

相关问题