Patches: Ubuntu and Git


A patch is a set of changes to a computer program/code or its supporting data designed to update, fix, or improve it. This includes fixing security vulnerabilities and other bugs, with such patches usually being called bug fixes or bug fixes, and improving the usability or performance. Although meant to fix problems, poorly designed patches can sometimes introduce new problems.


In working directory:

git clone <github-link>

When you cloned it , change directory by using

  cd <directory-name>

Make a branch temporary branch and switch into it

 git branch temp

 git checkout temp

Alternative of above, it creates branch and switch it automatically.

git checkout -b temp

You can use any editor for modification in file. But i use personally

vi <filename>

After modification, we can make patch for this file. For this, follow these commands step by step

git diff                         /// check changes
git add .
git commit -m “message-what-you-changed”
mkdir patches             /// make a directory to save patches
git format-patch master -o patches
ls                                /// check patch dir
git checkout master    /// by default you’re in master but you can change your branch.

Now you are in master branch and you can see a directory named patches in master branch. There is an advantage of git ; you can check patch whether it is valid for your software/code or not. For this, run this command

git apply –check patches/<patch-name.patch>

No error, it means everything is good. For check the status that what changes will occurs;

git apply –stat patches/<patch-name.patch>

Now, you actually want to apply  patch/patches use this command;

git am patches/<patch-name.patch>       //// apply patches

if you are not satisfied for patches/updates , you can undo your patches by using this;

git apply -R patches/<patch-name.patch>     ///reverted the applied patches.

You can see more information about mentioned commands i.e apply, am,checkout,branch  by using this command;

man git-apply

man git-am

That’s it!


There are some of commands to do it.

For ubuntu version or its packages;

sudo apt-get update

sudo apt-get upgrade

sudo apt-get dist-upgrade

For a specific software/code;

To create and to apply patch/patches to a software/code , follow these steps;

  • make a copy of source code by using this command
  •  cp file.cpp file-2.cpp

  • Open source code/file-2.cpp on any editor.
  • Make changes as you like or modify it or fix the bug, do whatever you want but you should know that what you are doing.
  • Create a patch file by using this;
  • diff -u file.cpp file-2.cpp > changes.patch

  • You’ve created patch successfully. For now, you can send it to company/client  to apply it on software/code(old) or you can apply it on old software and send to company a newer version of that software. For applying it, go to the software/codes’  directory and run this command ;
  • patch < changes.patch

  • If you don’t like applied patch changes, you can undo the process by this command keeping in current directory;
  • patch -R < changes.patch

That’s it.

Thank you!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s