Git Patch File


Have you ever made mistakes in a project on GitHub/GitLab? If you are the owner of the repository, then you can make a pull request, meaning you can fix the mistake yourself and update the repository. But oftentimes, there are situations like you don't own the repository, or, you don't have the authorization to update the fixed repository, or, the owner doesn't want to grant authorization to you. Then how would you fix your mistake?
First, you clone the repository, then rectify your mistakes in this new checkout and send the fixed project back to the owner and he could update it. However, sending the entire project is pretty inconvenient. In order to avoid this, we create a patch file that indicates the change(s) to your rectified checkout. Now, instead of sending the entire project, you just need to send this patch file to the owner. Then the project owner can review the changes and apply them to the master branch of the repository. Let's look at how we can create a git patch file.
Before creating the patch file, we need to clone the repository and commit our changes that we need to rectify our mistakes. Then we can either create one patch file indicating the last commit or create patch files indicating all the commits in our checkout. The command prompt syntax is given below:
  1. $ rem git format-patch <branch name> <options>  
  3. $ rem for single patch file   
  4. $ git format-patch master --stdout > patch_filename.patch   
  6. $ rem for individual patch files   
  7. $ git format-patch master  
These patch files make it easier for the owner to apply individual changes, meaning if they review the patch file, they can only accept those changes which they feel are important, but not others. Now, in case you happen to be the project owner who receives a patch file, then how would you apply these changes and update the project? By using one simple command:
  1. $ git am <patch_file>