If you'd prefer to see the youtube video instead of reading, please click here.
What Is a "Git Patch File"? Why Use One?
What happens if we see a mistake in a project on github/gitlab? Sometimes, we'll be able to make a pull request, meaning we can fix the mistake yourself and update the repository, but there'll be times that we can't because we don't have authorization to do so and the project owner doesn't want to grant it to us.
In that case, we can clone the repository, make the changes in the project that we need too, and send the fixed project back to the owner! But sending the entire project is a bit...much...we only really need to send the owner the changes that we made. That's where git patches come in. We can make changes to a project, create a "patch" file that indicates the change(s) to the project that we made, and send that patch file to the owner. Using that patch file, the project owner can quickly review the changes to ensure it's a legitimate change that won't harm their project, and apply it to the project. How To Create A Git Patch File
To learn how to create a git patch file, let's go through a very simple example. I've prepared a repository with a simply Hello World python file here. The python file has the following code:
Unfortunately, it looks like I've misspelled two words: the '3' in Hello should be an 'e', and the '0' in From should be an 'o'. Won't you download the project, fix the spelling, and email me a git patch file so I can update the project? First, let's clone the git repository and move into the folder: git clone https://github.com/delocalized/wrong cd wrong Well now we have the repository, but it's set on the master branch. Let's create a new branch for all the changes we're going to make. git checkout -b SpellingFixesBranch Great! Now we can start making some changes. However, as we're professionals, we're going to handle each change as a separate commit. (It's never good to change too many things at once.) Open HelloWorld.py and change the '3' in Hello to an 'e'. Save the file, which now looks like this:
Looks good so far. Let's add this change to git and commit it with a useful message. git add . git commit -m "Fixed the spelling of Hello" Okay, now let's open HelloWorld.py up one more time, and change the '0' in From to an 'o'. Save the file and it now looks like:
Perfect. Now let's add and commit this change to git. git add . git commit -m "Fixed the spelling of From" We've finally fixed the project! So let's create the patch file to send to the project owner. We'll run the following command to create the patch file:
git format-patch master --stdout > mychanges.patch
That will create a patch file named "mychanges.patch" that tracks all the changes we made to the project. Email/give the file to the project owner, and they can apply the changes you made! Notice that we've created a single patch file. If we wanted to create individual patch files for each commit that we made, then the following command should be run:
git format-patch master
This will create a patch file for every single commit that we made. It's a little messier, but it also makes it easier for the owner to apply individual changes, in case they're picky and will only accept some of the changes but not others. How To Deploy A Git Patch File
In case you happen to be the project owner who receives a patch file and you'd like to use it to update your project, you just have to use one simple command to do so:
git am mychanges.patch For the youtube video demonstrating this code, please watch the embedded video below:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake Comments are closed.
|
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|