Using Patches in Git

Git allows developers to manage changes, collaborate with others, and maintain a history of their work. One of the lesser-known but highly useful features in Git is the ability to create and apply patches. This article will guide you through the process of using patches in Git, including creating patches, applying patches, and understanding their benefits.

Table of Content

  • What is a Patch in Git?
  • Creating a patch for a single file
  • Creating patch for a binary image
  • Creating patches form the commits
  • Applying a Patch

What is a Patch in Git?

A patch in Git is a file that contains a set of changes (diffs) between two versions of a repository. Patches are useful for sharing changes without using a central repository or for applying changes from one branch or repository to another.

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.