15

Say I have a diff file looking basically like the following.

+line a
-line b

Is it possible to do one (or both) of the following:

  • Inverse this file (so I'd get)

    -line a
    +line b
    
  • Pass some argument to patch so the end result the same as applying the inversed diff file described above

4
  • You can change the order you put in the files. eg. diff a.txt b.txt -> diff b.txt a.txt.
    – matt
    Commented Feb 11, 2016 at 19:25
  • Are you looking for patch --reverse? Commented Feb 11, 2016 at 19:28
  • @WilliamPursell Does --reverse take a patch file? Commented Feb 11, 2016 at 21:03
  • 1
    @OneTwoThree why don't you try? =) Commented Feb 17, 2016 at 5:19

3 Answers 3

11

You can leave the diff as is and apply in reverse

git apply --reverse backwards-diff
10

To rewrite a reversed / inverted diff file, use interdiff from diffutils:

interdiff -q my-diff-file /dev/null
5

Here is what you should do (assuming newFile.txt is the file you want to apply the reversed diff file on and diffFile.txt is the diff file):

patch -R newFile.txt diffFile.txt -o oldFile.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.