4

I have two files, x_original.txt and x_updated.txt.

I used the following command to obtain a patch file:

git diff --no-index x_original.txt x_updated.txt > fix_something.patch

I now want to apply this patch to a file called x.txt.

The following is worth noting:

  • x.txt is not in a git repo
  • the filenames x_original.txt and x_updated.txt are included in fix_something.patch which makes no sense because these files won't exist when I'm applying the patch

What git command can I use to apply this patch? Or another utility if necessary? And what modifications should I make to the patch file?

David Callanan
  • 5,601
  • 7
  • 63
  • 105
  • `x_original.txt` makes sense as the file you apply the patch to would need to be equal the `x_original.txt`, as it was, when the patch-file was made. – Andreas Louv Sep 09 '21 at 11:52
  • @andlrc I want to apply it to a file regardless of it's name. – David Callanan Sep 09 '21 at 11:57
  • I understand that, I was just stating that you need to know which file the patch was made from, in order to know which file to apply it to. In your case you don't look at the filename as you can just provide the file that needs to be patches, as shown in your answer below. – Andreas Louv Sep 09 '21 at 12:01
  • @andlrc Yes that was the idea :) – David Callanan Sep 09 '21 at 12:05

1 Answers1

4

I found out how to do it using the patch command:

patch -p1 x.txt fix_something.patch

This utility appears to ignore the filenames specified in the patch file, so there's no need to modify anything there.

David Callanan
  • 5,601
  • 7
  • 63
  • 105