如何在Windows上的Cygwin中使用diff和patch?

如何在Windows上的Cygwin中使用diff和patch?,cygwin,diff,patch,Cygwin,Diff,Patch,我关注了很多文章,但我仍然不知道如何在Cygwin中使用diff和patch 在这里,我在Windows资源管理器中创建了两个文件 origin.txt one two three one four five new.txt one two three one four five 然后我diff-u origin.txt new.txt>file.patch,结果是 --- origin.txt 2013-03-21 15:53:20.062956800 +0700 +++ new.t

我关注了很多文章,但我仍然不知道如何在Cygwin中使用diff和patch

在这里,我在Windows资源管理器中创建了两个文件

origin.txt

one
two
three
one
four
five
new.txt

one
two
three
one
four
five
然后我
diff-u origin.txt new.txt>file.patch
,结果是

--- origin.txt  2013-03-21 15:53:20.062956800 +0700
+++ new.txt 2013-03-21 15:53:29.191869600 +0700
@@ -1,3 +1,3 @@
 one
-two
-three
\ No newline at end of file
+four
+five
\ No newline at end of file
然后I
patch origin.txt
显示错误

patching file origin.txt
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file origin.txt.rej
如何克服这个问题?这与Unix和Windows文件格式有关吗


p/S:我正在使用CYGWIN\u NT-6.1-WOW64和Windows 7 64位

这几乎肯定是一个行尾问题。您可以通过首先在文件上运行
dos2unix
来解决此问题:

$ dos2unix origin.txt new.txt
$ patch origin.txt < file.patch
patching file origin.txt
$dos2unix origin.txt new.txt
$patch origin.txt
所以在diff和patch之前,我们先做dos2unix,然后再做unix2dos?@entropy:是的。或者,理想情况下,始终保持Unix格式。如果未安装
修补程序
,请使用:
apt cyg安装修补程序
。(在它自己的名为
补丁的包中