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
Staging in Git
In this article we will try to understand each and every aspect which may help us to understand about Staging in Git. To keep a track of modifications or changes in the file we have to bring that changes to our staging area which we bring by using Staging. Below command and this process of adding th
7 min read
Undoing in Git
Undoing in Git means doing undo just like when we type something in any text editor and delete the same. After that, we think the text that we just deleted is needed, and then we use the undo operation to get back the old text. The same undoing in git is like doing undo in git. Common Scenarios for
6 min read
How To Login Using The Git Terminal?
Git is a widely used version control system that allows developers to collaborate on code. When working with remote repositories hosted on platforms like GitHub, GitLab, or Bitbucket, it's essential to authenticate yourself to perform actions such as cloning, pulling, or pushing code. This article w
3 min read
Using Refs And Reflogs In Git
Git, a popular version control system, helps developers track changes in their codebase. Understanding how to use refs and reflogs in Git can significantly enhance your ability to manage and troubleshoot your repositories. This article will learn about these concepts, providing you with the knowledg
3 min read
What is Git Interactive Rebasing?
In the version control system, Git becomes a powerful and flexible tool for managing project history. Among its list of features, Git interactive rebasing stands as a powerful tool. In this article, we will learn more about Git Interactive Rebasing. Table of Content What is Interactive Rebasing?Prim
4 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
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.
6 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
Git - Status
Git is a powerful version control system that helps you to manage code changes and collaborate efficiently. One of the fundamental commands in Git is git status, which provides important information about the current state of your working directory and staging area. This article will explain the git
3 min read