How to Create and Apply Git Patch Files
A Patch is a file that contains a set of changes between two files. It is based on the Unix command with the same name. Patches are mainly used to share or suggest changes in someone's repository that you don't have access to. It can also be used to save some changes as a patch and apply them to the files in your repository at an earlier or a later stage or to provide some bug fixes to other branches or repositories. Let's learn more about patches and how to use them in Git.
- A patch is a small file that contains the changes between two files. The content of the file is very similar to the Git Diff command. We can share patches with other developers to suggest changes in their repositories.
- Patches can also be created from the changes that were included in the commits. This will include all the changes that were made to the files and added to that commit.
- Such patches store additional information like the commit hash, commit data, author details, etc. These details are included so that we can replicate that commit and apply it somewhere else. This is very similar to the process of cherry-picking where we choose a commit and add it to some other branch.
Creating a Patch
Patches can be created for files that are in the working directory(not yet committed) or we can create a patch for an entire commit or a group of commits. Let's learn how to create a patch.
For Uncommitted Changes
To create a patch for the changes that are not yet committed we simply use the Git Diff command. As discussed above the content of a patch is similar to the Diff output.
$ git diff > patch_name.patch
The above command will not add the changes of newly created files that are untracked. To overcome this we can add everything to the staging area using the Git Add command and then run the Git Diff command with the --cached option.
$ git add .
$ git diff --cached > patch_name.patch
We can also create a patch for binary files like images or videos by using the --binary option with the Git Diff command.
$ git diff --binary > patch-name.patch
Take a look at the following example where we have created a new patch and we can see that its content is the same as that of the Git Diff command.
For Committed Changes
We can create a patch for changes that were committed previously. We use the Git Format-Patch command for this purpose. This command will include all the commits of our currently checked-out branch which are not a part of our specified branch. For example, in the following case, the Git Format-Patch will only include the two commits of the feature branch as the remaining three commits are also part of the master branch which we have specified in the command.
$ git format-patch <branch-name>
The above command will create a separate patch file for each commit. If we wish to have all the changes of all the included commits in a single patch, then we can use the --stdout option. If we don't specify the patch name then it will just display the content of the patch but will not create any new patch file.
$ git format-patch --stdout <branch-name> > patch-name.patch
We can also create a patch for just a single commit by using the -1 option and the commit hash.
$ git format-patch -1 <commit-hash>
Consider the following example in which we created a patch for a specific commit. We can see that the patch has the output of the Git Diff command but also stores additional information of the commit from which it was created.
Applying a Patch
After saving a patch, we may send it to some other developer or we may need to apply that patch to some other branch or repository on our system. We can apply a patch by using the Git Am command. If the patch was created from some commits then this command will directly add those commits to our branch. But this cannot be used for applying patches that were created from uncommitted changes. Make sure that you are checked out on the branch where you want to apply the patch.
$ git am <patch-name>
We can also apply a patch by using the Git Apply command. However, Git Apply will only apply the patch to the working directory and would not create a new commit. This command can also work for patches that are created from the Git Diff command(uncommitted changes).
$ git apply <patch-name>
Patches are a great way to store and share changes. Patches can be made from committed or uncommitted changes. Their content is the same as the output of the Git Diff command but patches that are created from committed changes store the commit information as well. We can create a patch of uncommitted changes by using the Git Diff command. We need to use the Git Format-Patch command to create a patch that contains committed changes. The format-patch command is the preferred way of creating patches. We can apply a patch by using the Git Apply or the Git Am command according to our needs.