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.
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.

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.
Similar Reads
Git Introduction Git is a powerful and widely used version control system that helps developers track changes in their code, collaborate with others, and manage project history effectively. Whether you are a professional developer or just starting out, understanding Git is important for modern software development.
5 min read
Git - Working Tree Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific ap
4 min read
Saving a File in Git Git allows you to track changes, collaborate with others, and manage codebase efficiently. One of the fundamental tasks when working with Git is saving files, a process that involves several steps, including adding files to the staging area and committing them to the repository. In this article, we
2 min read
What is "origin" in Git? When working with Git, especially in collaboration with others, you will often encounter the term "origin." Understanding what "origin" means and how it functions within Gitâs workflow is important for effective source code management. In this article, we will walk you through everything you need to
4 min read
How to Setup Git Using Git Config? Git is a powerful version control system that helps developers manage their code efficiently. To use Git effectively, you need to configure it properly using the git config command. This setup ensures that Git recognizes your identity, preferred settings, and workflow preferences.How the git configu
3 min read