输出NUM(默认2)行统一上下文 - 这是什么意思?

时间:2015-01-24 02:44:58

标签: git unix diff git-diff file-diffs

我有以下命令:

diff -u filea fileb | grep '^-' | sed 's/^-//' > diff.txt

效果很好;它将每行的差异列表输出到文本文件,然后删除' - '对于每一行,我真的想了解-u正在做什么以及以下是什么意思:

Output NUM (default 2) lines of unified context

我是否正确地认为统一是指仅显示差异而不是差异的背景?

对于我使用上述命令将两个文件之间的差异显示到另一个文件的要求,我是否应该关注任何潜在的问题?

1 个答案:

答案 0 :(得分:0)

这代表它找到的每个差异的“上下文线”数量。

可能最好用一个例子来证明:

$ diff -U 1 hello.txt hello2.txt
--- hello.txt   2015-01-24 16:10:45.000000000 +0100
+++ hello2.txt  2015-01-24 16:11:14.000000000 +0100
@@ -4,3 +4,3 @@
 Hello World.
-Hello World.
+Yo World.
 Hello World.

$ diff -U 2 hello.txt hello2.txt
--- hello.txt   2015-01-24 16:10:45.000000000 +0100
+++ hello2.txt  2015-01-24 16:11:14.000000000 +0100
@@ -3,5 +3,5 @@
 Hello World.
 Hello World.
-Hello World.
+Yo World.
 Hello World.
 Hello World.

$ diff -U 3 hello.txt hello2.txt
--- hello.txt   2015-01-24 16:10:45.000000000 +0100
+++ hello2.txt  2015-01-24 16:11:14.000000000 +0100
@@ -2,7 +2,7 @@
 Hello World.
 Hello World.
 Hello World.
-Hello World.
+Yo World.
 Hello World.
 Hello World.
 Hello World.