Creating and applying git patch

To demonstrate the process of creating and applying patch I cloned the BrowserAdapter module of the Isis-project for a simple reason that it is a small module.

Cloning the repo

git clone https://github.com/isis-project/BrowserAdapter.git

I added a couple of print statements in the code, so that I can create a patch.

Creating Git Patch

Command: git diff > Patch_File_Name
If only name is given, patch file will be created in the current working directory. Other path can be given like in the image below.

git diff >my_changes.patch

git diff

git diff

Patch file will be created in the current working directory.

Highlighted file is the patch file.

Highlighted file is the patch file.

Applying a Patch

1. Checking Patch Statistics

Command: git apply –stat Patch_File_Name
If patch file is available in the current working directory providing the name of the file is sufficient otherwise entire path has to be given.

git apply –stat my_changes.patch

git apply --stat

git apply –stat

This command does not apply the patch, but only shows you the stats about what it will do. After peeking into the patch file with your favorite editor, you can see what the actual changes are. The “+” indicates new lines added and “-” indicates the lines removed.

2. Checking Patch
Git allows you to test the patch before you actually apply it.

Command: git apply –check Patch_File_Name
If patch file is available in the current working directory providing the name of the file is sufficient otherwise entire path has to be given.

git apply –check my_changes.patch

If you don’t get any errors, the patch can be applied cleanly.

Otherwise you may see what trouble you’ll run into. Check out the image below (Conflict case).

Can't apply patch

Can’t apply patch

3. Apply Patch

Command: git apply Patch_File_Name
If patch file is available in the current working directory providing the name of the file is sufficient otherwise entire path has to be given.

git apply my_changes.patch

After correcting any issues raised during stage 2, there shouldn’t be any problem in this stage.

use “git diff” to see if the patch is applied. The below image shows the changes made by the patch.

Patch applied successfully!

Patch applied successfully!

If you want to try out these steps. I have attached uploaded the patch here.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s