如何仅查看来自HEAD的未合并文件的差异?

时间:2019-03-05 12:59:35

标签: git

修复合并冲突后,我想添加它们以解决冲突之前,先查看未合并文件的实际差异。

我可以看到每个git diff HEAD文件的差异,但这也向我展示了无冲突文件的差异。

我可以看到带有git diff的未合并文件,但这向我显示了冲突,而不是实际的差异。

我如何才能看到HEAD与实际文件内容之间的差异(仅对于有冲突的文件)?


以下是在空白目录中创建副本的方法:

git init
# Create files foo.txt and bar.txt on master.
git commit --allow-empty -m "Initial commit"
echo 'foo content' > foo.txt
echo 'bar content' > bar.txt
git add .
git commit -m A
# Create a branch "b" that change both files.
git checkout -b b
echo 'Foo content' > foo.txt
echo 'Bar content' > bar.txt
git add .
git commit -m B
# Get back on master and add a commit that change foo.txt.
git checkout -
echo 'foo content and more' > foo.txt
git add foo.txt
git commit -m C
# Try to merge b in master: CONFLICT!
git merge b
# Fix the conflict.
echo 'Foo content and more' > foo.txt

现在,git diff HEAD给了我所有更改的列表,包括bar.txt且没有冲突:

diff --git a/bar.txt b/bar.txt
index 085e7f5..6401b08 100644
--- a/bar.txt
+++ b/bar.txt
@@ -1 +1 @@
-bar content
+Bar content
diff --git a/foo.txt b/foo.txt
index 6633fd8..a65c68b 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1 @@
-foo content and more
+Foo content and more

另一方面,git diff仅给我冲突的文件,但未显示磁盘上实际内容与HEAD之间的实际区别。

diff --cc foo.txt
index 6633fd8,6fc4556..0000000
--- a/foo.txt
+++ b/foo.txt
@@@ -1,1 -1,1 +1,1 @@@
- foo content and more
 -Foo content
++Foo content and more

我想要一个与git diff HEAD -- foo.txt输出相同但没有指定冲突文件的命令。

git diff --magic-flag HEAD

diff --git a/foo.txt b/foo.txt
index 6633fd8..a65c68b 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1 @@
-foo content and more
+Foo content and more

1 个答案:

答案 0 :(得分:0)

此命令似乎可以完成工作:

git diff HEAD -- $(git diff --name-only --diff-filter=U)

我在git dhu中将其别名为~/.gitconfig d iff h ead u 被合并):< / p>

[alias]
dhu = !git diff HEAD -- $(git diff --name-only --diff-filter=U)