How to use diff and patch in Cygwin on Windows?

17,973

This is almost certainly a line-ending problem. You can fix this by running dos2unix over the files first:

$ dos2unix origin.txt new.txt
$ patch origin.txt < file.patch
patching file origin.txt
Share:
17,973
onmyway133
Author by

onmyway133

I work with iOS, macOS, Android, React, Electron and Nodejs. I actively work on open source with 1.3k+ followers on GitHub, 45k+ apps touched and 3.4m+ downloads on CocoaPods. I also write on Medium with 2.3k+ followers with 90k+ monthly views. Support my apps https://onmyway133.com/apps Open source https://github.com/onmyway133 Writing https://medium.com/@onmyway133

Updated on June 04, 2022

Comments

  • onmyway133
    onmyway133 almost 2 years

    I follow many articles out there but I still don't know how to use diff and patch in Cygwin

    Here I create 2 files in Windows Explorer

    origin.txt

    one
    two
    three
    

    new.txt

    one
    four
    five
    

    Then I diff -u origin.txt new.txt > file.patch which results this

    --- 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
    

    Then I patch origin.txt < file.patch which show errors

    patching file origin.txt
    Hunk #1 FAILED at 1.
    1 out of 1 hunk FAILED -- saving rejects to file origin.txt.rej
    

    How to overcome this? Is this related to Unix and Windows file format?

    P/S: I'm using CYGWIN_NT-6.1-WOW64 and Windows 7 64bit