Compare files, “accept/reject” (like in Word) and merge changes, with diff and patch
When someone sends you back a file they edited, you can use the programme diff to see differences between your original file and the one newly received. Next, to modify edits and integrate those into your original file, you can use the programme patch.
“Accept” all modifications received and integrate those to your original file
To apply (or “accept”, like in Word) the modifications you received from someone, from their file to your file, run:
$ diff yourfile theirfile > file.patch
$ patch yourfile < file.patch
Your file should reflect the modifications from their file.
That was the quick way if you don’t need to review or modify the incoming patch.
You might want to compare and edit the modifications received (that is “accept/reject” modifications like in Word) before you apply the patch.
Compare modifications between “your file” and “their file” before patching
I assume you have yourfile and theirfile.
If you want to download both files you can run $ wget https://yctct.com/yourfile https://yctct.com/theirfile
.
First you might want to compare differences. You can do so side by side by running diff with option -y
:
$ diff -y yourfile theirfile
The terminal should print this, on the left yourfile, on the right theirfile:
This is a first sentence. This is a first sentence.
This is a second sentence. | This is a second sentence with edits you will accept.
This is a third sentence. | This is a third sentence with edits you will reject.
This is a fourth sentence which will get deleted. <
In theirfile, the first sentence is untouched, the second and the third have been edited, the fourth one has been deleted.
You can also compare differences in what is called “a unified output” with the option -u
:
$ diff -u yourfile theirfile
You terminal should print this:
--- yourfile 2023-04-19 18:21:23.182498931 +0200
+++ theirfile 2023-04-19 18:23:30.807054466 +0200
@@ -1,7 +1,6 @@
This is a first sentence.
-This is a second sentence.
+This is a second sentence with edits you will accept.
-This is a third sentence.
+This is a third sentence with edits you will reject.
-This is a four sentence which will get deleted.
That was to view difference between files.
Edit modifications (“reject” some modifications) before patching
To “accept” or “reject” edits you first have to create file.patch by running:
$ diff yourfile theirfile > file.patch
The terminal should print:
file.patch was created.
You can check by running $ ls *patch
which should print:
$ ls *patch
file.patch
Open file.patch with your text editor.
You should see this:
--- yourfile 2023-04-19 18:21:23.182498931 +0200
+++ theirfile 2023-04-19 18:23:30.807054466 +0200
@@ -1,7 +1,6 @@
this is a first sentence.
-this is a second sentence.
+this is a second sentence with edits you will accept.
-this is a third sentence.
+this is a third sentence with edits you will reject.
-this is a four sentence which will get deleted.
You want to accept edits on the second sentence.
You want to reject edits on the third sentence.
The fourth sentence will be deleted and your are okay with that.
To reject modifications on the third sentence you want to modify file.patch as such:
--- yourfile 2023-04-19 18:21:23.182498931 +0200
+++ theirfile 2023-04-19 18:23:30.807054466 +0200
@@ -1,7 +1,6 @@
This is a first sentence.
-This is a second sentence.
+This is a second sentence with edits you will accept.
This is a third sentence.
-This is a four sentence that will get deleted.
To reject edits on the third sentence you do two things:
- replace the hyphen, that is:
-
, by aspace
in front of the third sentence you keep - remove the third sentence with edits
The patch has been modified to your liking. It is ready to be applied to your file.
To apply the path to your file run:
$ patch yourfile < file.patch
The terminal should output:
patching file yourfile
To check that your file was patched run:
$ cat yourfile
The terminal should output:
This is a first sentence.
This is a second sentence with edits you will accept.
This is a third sentence.
The second sentence was edited; the third untouched; the fourth deleted.
You can learn more about diff and patch by running $ man diff
and $ man patch
to display the manuals.
Files I use for the examples
“yourfile” with the following content:
This is a first sentence.
This is a second sentence.
This is a third sentence.
This is a fourth sentence which will get deleted.
And “theirfile” with the following content:
This is a first sentence.
This is a second sentence with edits you will accept.
This is a third sentence with edits you will reject.
You can download both files in a newly created directory by running:
$ cd ~ # go to your /~user directory
$ mkdir diff-patch # create a /diff-patch directory
$ cd diff-patch # move to /diff-patch directory
$ wget \
https://yctct.com/yourfile \
https://yctct.com/theirfile
# download both files
diff patch personal computing gnu linux trisquel command-line interface (cli) text processing wiki office applications shell literacy offline