Open In App
Related Articles

Using Patches in Git

Like Article
Save Article
Report issue

GIT patch or GIT diff is used to share the changes made by you to others without pushing it to main branch of the repository. This way other people can check your changes from the GIT patch file you made and suggest the necessary corrections. After making all the corrections you can push the changes to main branch of the repository. 

There are different ways to create GIT patches. We will go through them one by one. Here, we have taken a demo repository for this tutorial named git-patch-tut in which there is one text file named gfg-intro.txt which contains few lines of description. We will perform all the operations on this text file to make the understanding better. 

Creating a patch for a single file

Suppose the description in gfg.txt file is changed, git diff –cached gfg.txt is done to check the changes made. Changes can be seen with git-diff –cached after a file is staged. If a new file is inserted in the repository it will not show the changes with git-diff, unless –cached is used with it.  

Now, suppose user wants to create a patch for this single file that was edited. git diff > gfg-intro.patch will be used where gfg-intro is the patch name.  

Creating patch for a binary image

If a binary image is added like a jpg or a png file in the repository(Project Folder). The changes of a binary file can be seen with git diff –staged –binary > image.jpg where “image.jpg” is the filename. Here –staged is used as we first add the file in the staging area then use git-diff to see changes.  

Now, to create a patch for this newly added binary file, git diff –staged –binary > binary.patch can be used where binary is the patch name. It is same like the above, the only difference is using –binary while creating the patch.  

Creating patches form the commits

To create the patch files for all the commits made in a particular branch, git format-patch -1 test-patchcan be used, where “test-patch” is the branch name and -1 is the number of commits that are to be included for creating the patch. In the below example we have used -1 and the resultant patch is named as 0001-Add-description.patch. Note that the name of file is based on the name of commit on that particular branch. In this way multiple patch files for multiple commits can be created in a particular branch. In below example it has generated only 1 patch file for the latest commit. You can also use git format-patch -1 HEAD, where HEAD is sha of the commit where header is pointed. 

To create a patch file from one commit to other i.e a patch file in which changes include all the commits from starting commit and end commit. It is done by git diff starting-commit-sha ending-commit-sha myPatch.patch, where “myPatch” is the patch name and the starting and ending sha of the commits are included.  

Applying a Patch

Suppose there is a patch file with changes done, here in our example for gfg.txt file. If the user wants to check these changes are correct or not, so a different branch is created from the master and applied the patch to check the changes. This is how the patch is applied in the new branch. 

Applying is done by git apply 0001-Add-description.patch where “0001-Add-description.patch” is the patch name that is to be applied. After applying the patch, the related file will be modified with the changes done in that patch file and can be reviewed, before pushing it to the master branch of the main repository.

Last Updated : 11 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads