How To Apply A Patch To A File Using Linux

man using computer
Morsa Images/DigitalVision/Getty Images

Introduction

This guide provides a basic overview for applying a patch to a program or file.

The command used for applying patches is the patch command but in order to apply a patch the diff command also needs to be used to supply the differences that need to be patched.

Why Would You Create A Patch

Imagine you had a HTML file as follows:

<html>
<head>
<title>MySite</title>
</head>
<body>
<h1>Welcome To MySite</h1>
</body>
</html>

The above source when loaded into a web browser will show the words "Welcome to MySite". 

It is common when programming for there to be multiple versions of the same file. For instance there would be one version which is the current live version, there would be another version on the development branch which may contain multiple changes, finally there might be another version on a release branch.

The release branch would be the version used by real users (i.e in this case visitors to the MySite website). Let us say that MySite is at version 1.0. After a development phase you will create a release branch which would be in this case 1.1. The development branch will could be ahead of the release branch because it may contain changes that aren't due for release in version 1,2 but will be for version 1.1.

Imagine there was a bug whereby MySite should be called YourSite. You could go to each branch in turn and make changes to the live version, development version and release version of you could make the change as a hot fix in live and then apply the patch to the development and release branches.

 

A Worked Example

Create a file called myfile.html with the following code in it:

<html>
<head>
<title>MySite</title>
</head>
<body>
<h1>Welcome To MySite</h1>
<p>This is a worked example of patching source code.</p>
</body>
</html>

Create another file called myfile_v2.html with the following code in it:

<html>
<head>
<title>YourSite</title>
</head>
<body>
<h1>Welcome To YourSite</h1>
<p>This is a worked example of patching source code.</p>
</body>
</html>

The first file is the original web page with the heading "Welcome to MySite". The second file is the same as the first file except MyFile is renamed to YourFile in the text.

How To Create The Diff File

In order to create a patch we need to work out the differences between myfile.html and myfile_v2.html. 

We can do this with the diff command as follows:

diff -u myfile.html myfile_v2.html > myfile.patch

You can open the myfile.patch file in the nano editor as follows in order to view it:

nano myfile.patch

Lines that are unchanged are shown in blue. Lines that have been removed are shown in red and lines that have been added are shown in green.

How To Apply The Patch Using The Patch Command

To apply the patch to myfile.html use the following command:

patch < myfile.patch

How To Revert A Patch

This will apply the changes created using the diff command to the original file myfile.html.

If you want to revert the patch so that myfile.html is back to the way it was originally use the following command:

patch -R < myfile.patch

Summary

The patch command can be used to apply patches across a whole directory structure.

There are far better tools than the patch command for creating patches and ensuring good source control.

 

There is a reason that so many development projects use GIT for source control. GIT makes it easy to create a consistent branching strategy.

The patch command is ok for applying differences to a bash script or something simple but you could just as easily create a backup copy of the files you wish to patch and simply copy the new files into the same place. 

For more information use the man command:

man patch