Science and technology

Collaborate on a file utilizing Linux diff and patch

I edit loads of textual content recordsdata. Sometimes it is code. Other instances it is the written phrase for role-playing video games (RPGs), programming books, or basic correspondence. Sometimes it is good to make a change, however for my collaborator to check my change with what they initially had written. Many folks default to workplace suites, like LibreOffice, utilizing feedback or change monitoring options. Sometimes a less complicated device makes extra sense, although, and for that, you possibly can have a look at programming historical past for instruments like diff and patch, which offer standardized formatting for monitoring and making use of modifications to shared recordsdata.

Even with a easy file, there’s complexity in synchronizing two paperwork. Some objects get modified, others are left alone, new content material will get added, and a few keep the identical however are moved to totally different locations within the doc. It’s troublesome to duplicate modifications with out blissfully accepting that every one modifications are equally legitimate, and changing the outdated file with the brand new one. It’s additionally monolithically opaque. There are so many modifications that it is troublesome to select precisely what’s modified.

With the diff command, you possibly can create a report of how the file has modified, and with patch you possibly can “replay” these modifications over the outdated model to convey it updated with the brand new model.


Suppose you and I are collaborating on a file describing tips on how to make a cup of tea.

So far, the file accommodates uncooked copy-paste:

Boil water.
Warm the teapot.
Add tea and water to the teapot.
Place a tea cosy over the teapot.
Steep for six minutes.
Pour tea into cup.
Add milk.

It appears cheap, however there are at all times optimizations you can also make, so that you ship the file to me for enchancment. In an effort to make clear the tea-making course of, I copy the file as and edit it, ending up with this:

Warm a teapot within the proving drawer of your oven.
Boil water.
Add tea leaves to a tea strainer.
Add strainer and water to teapot.
Steep for six minutes. Keep it heat with a tea cosy.
Pour tea into cup.
Optionally, add heat milk.

As anticipated, some objects (Boil water and Pour tea into cup) are unchanged, whereas different strains (Warm the teapot) have had additions. Some strains are fully new, and a few strains are the identical however in a unique order.

Create a diff

The diff device shows the distinction between two recordsdata. There are a couple of alternative ways to view the outcomes, however I believe the clearest one is the --unified (-u for brief) view, which exhibits which strains received added or subtracted. A line that is modified in any means is handled as a line that received subtracted after which added. By default, diff prints its output to the terminal.

Provide diff with the outdated file after which the brand new file:

$ diff --unified 
---      2021-11-13 10:26:25.082110219 +1300
+++      2021-11-13 10:26:32.049110664 +1300
@@ -1,7 +1,7 @@
+Warm a teapot in the proving drawer of your oven.
 Boil water.
-Warm the teapot.
-Add tea and water to the teapot.
-Place a tea cosy over the teapot.
-Steep for 6 minutes.
+Add tea leaves to a tea strainer.
+Add strainer and water to teapot.
+Steep for 6 minutes. Keep it heat with a tea cosy.
 Pour tea into cup.
-Add milk.
+Optionally, add heat milk.

A plus signal (+) firstly of a line signifies one thing that is gotten added to the outdated file. A minus signal (-) firstly of a line signifies a line that is gotten eliminated or modified.

Create a patch with diff

A patch file is simply the output of the diff --unified command positioned right into a file. You can do that utilizing commonplace Bash redirection:

$ diff -u > tea.patch

The contents of the file are precisely the identical as what was output to the terminal. I wish to view patch recordsdata in Emacs, which color-codes every line relying on whether or not it is gotten added or subtracted.

Applying modifications with patch

Once I’ve a patch file, I may ship it to you so that you can evaluation and, optionally, apply to your outdated file. You apply a patch with the patch command:

$ patch tea.patch

Lines received added, strains received subtracted, and in the long run, you find yourself with a file similar to my model:

$ cat
Warm a teapot in the proving drawer of your oven.
Boil water.
Add tea leaves to a tea strainer.
Add strainer and water to teapot.
Steep for 6 minutes. Keep it heat with a tea cosy.
Pour tea into cup.
Optionally, add heat milk.

There’s no restrict to what number of instances you possibly can patch a file. You may iterate on my modifications, generate a brand new patch, and ship that to me for evaluation. Sending modifications quite than outcomes lets every contributor evaluation what modified, resolve what they wish to hold or remove, and precisely doc the method.


On Linux and macOS, you have already got each the diff and patch instructions. On Windows, you possibly can acquire diff and patch by Cygwin, or use Chocolatey to seek for diffutils and patch.

If you have ever tried to collaborate on recordsdata over e mail or chat, and you’ve got discovered your self making an attempt to describe the place you want a change made, then you definitely’ll love diff and patch. A fastidiously structured file, corresponding to code or line-delimited Markdown, is simple to diff, patch, and preserve. 

Most Popular features the latest multimedia technologies, from live video streaming to audio packages to searchable archives of news features and background information. The site is updated continuously throughout the day.

Copyright © 2017 Breaking Express, Green Media Corporation

To Top