How to use Diff command to create Patch?

This article shows how you can create a diff patch file that can be used in Git repositories.

The diff command line tool shows the difference between the two files.
Such a difference between the two files is called a patch.

The diff syntax is:


diff [options] [original filename] [changed filename]

diff [options] [original directory] [changed directory]

Create two files

Here’s an example:

file1.php


<?php

$name = 'Sam';
$age = 33;
$country = 'USA';
$phone = '1234567890';
$subject = "Hello World!";

file2.php

There are some modifications in file2 as compared to file1.


<?php

$name = 'John';
$age = 25;
$country = 'USA';
$subject = "Hello World!";
$gender = 'M';

Get the diff between the above two files


diff file1.php file2.php

3,4c3,4
< $name = 'Sam';
< $age = 33;
---
> $name = 'John';
> $age = 25;
6d5
< $phone = '1234567890';
7a7
> $gender = 'M';

In the above output, the first line 3,4c3,4 shows the line numbers where there have been changes. Content was changed in line number 3 and 4.

Note the letter c in the first line. It indicates that the change has occurred.

c = Content was changed or replaced
a = Content was added or appended
d = Content was deleted

Create Patch with Custom Label

In the patch file, the label will be used instead of the file name.


diff -u --label a/vendor/module-name/Model/File1.php \
--label b/vendor/module-name/Model/File1.php \
File1.original.php \
File1.modified.php > \
yourPatchFileName.patch

Get case-insensitive diff

By default, the diff command is case-sensitive. You can add the -i option to ignore case.


diff -i file1 file2

Get the diff output in two side-by-side columns with -y option

The option -y can be used for side-by-side output of the differences in two columns.


diff -y file1.php file2.php

<?php                               <?php

$name = 'Sam';                            | $name = 'John';
$age = 33;                            | $age = 25;
$country = 'USA';                       $country = 'USA';
$phone = '1234567890';                        <
$subject = "Hello World!";                  $subject = "Hello World!";
                                  > $gender = 'M';

Get the diff output in lines of context

using -c option

In the output, there are plus, minus & exclamation signs which indicate the following:

  • – (minus) – the line needs to be deleted from the first file.
  • + (plus) – the line needs to be added to the first file.
  • ! (exclamation mark) – the line needs to be changed to the corresponding line from the second file.
  • If there is no symbol, the line remains the same.

diff -c file1.php file2.php

*** file1.php   2022-10-27 18:55:31.000000000 -0400
--- file2.php   2022-10-27 18:53:45.000000000 -0400
***************
*** 1,7 ****
  <?php

! $name = 'Sam';
! $age = 33;
  $country = 'USA';
- $phone = '1234567890';
  $subject = "Hello World!";
--- 1,7 ----
  <?php

! $name = 'John';
! $age = 25;
  $country = 'USA';
  $subject = "Hello World!";
+ $gender = 'M';

Get the diff output in unified format

without redundant context lines
using -u option

  • – (minus) – the line needs to be deleted from the first file.
  • + (plus) – the line needs to be added to the first file.
  • If there is no symbol, the line remains the same.

diff -u file1.php file2.php

--- file1.php   2022-10-27 18:55:31.000000000 -0400
+++ file2.php   2022-10-27 18:53:45.000000000 -0400
@@ -1,7 +1,7 @@
 <?php

-$name = 'Sam';
-$age = 33;
+$name = 'John';
+$age = 25;
 $country = 'USA';
-$phone = '1234567890';
 $subject = "Hello World!";
+$gender = 'M';

Create a Patch

A patch is used to create or override changes in another file.

The command to create a patch is:


diff -u [original filename] [changed filename] > patchfile.patch

diff -u file1.php file2.php > myFilePatch.patch

The above command will output the following:


--- file1.php   2022-10-27 18:55:31.000000000 -0400
+++ file2.php   2022-10-27 18:53:45.000000000 -0400
@@ -1,7 +1,7 @@
 <?php

-$name = 'Sam';
-$age = 33;
+$name = 'John';
+$age = 25;
 $country = 'USA';
-$phone = '1234567890';
 $subject = "Hello World!";
+$gender = 'M';

Apply the Patch

Applying the patch means overriding the files mentioned in the patch with the content present in the patch file.


patch file1.php myFilePatch.patch

The above command will update the file file1.php with the content update present in the patch file for file1.php.
In our example, file1.php will be updated with the content of file2.php as present in the patch file.

Revert/Reverse a Patch

The previously applied patch can be reverted with the following command:


patch -p0 -R -i myFilePatch.patch

Hope this helps. Thanks.