devroom.io/content/posts/2009-10-26-how-to-create-and-apply-a-patch-with-git.md

91 lines
4.2 KiB
Markdown
Raw Normal View History

2015-03-26 11:28:08 +00:00
+++
date = "2009-10-26"
title = "How to create and apply a patch with Git"
2015-06-10 22:54:16 +00:00
tags = ["git", "patch", "sign-off"]
2015-03-26 11:28:08 +00:00
slug = "how-to-create-and-apply-a-patch-with-git"
+++
2015-06-10 22:54:16 +00:00
Creating a patch file with git is quite easy to do, you just need to see how it's done a few times.
This article will show you how to create a patch from the last few commits in your repository. Next, I'll also show you how you can correctly apply this patch to another repository.
### Before you start
To make creating patches easier, there are some common git practices you should follow. It's not necessary, but it will make your life easier.
If you fix a bug or create a new feature do it in a separate branch!
Let's say you want to create a patch for my <a href="http://github.com/ariejan/imdb">imdb</a> gem. You should clone my repository and create a new branch for the fix you have in mind. In this sample we'll do an imaginary fix for empty posters.
2017-03-20 15:35:19 +00:00
``` shell
git clone git://github.com/ariejan/imdb.git
cd imdb
git checkout -b fix_empty_poster
```
2015-06-10 22:54:16 +00:00
Now, in the new <code>fix_empty_poster</code> branch you can hack whatever you need to fix. Write tests, update code etc. etc.
When you're satisfied with all you changes, it's time to create your patch. FYI: I'm assuming you made a few commits in the <code>fix_empty_poster</code> branch and did <em>not</em> yet merge it back in to the <code>master</code> branch.
### Creating the patch
Okay, I've made some commits, here's the <code>git log</code> for the <code>fix_empty_poster</code> branch:
2017-03-20 15:35:19 +00:00
``` shell
git log --pretty=oneline -3
* ce30d1f - (fix_empty_poster) Added poster URL as part of cli output (7 minutes ago)
* 5998b80 - Added specs to test empty poster URL behaviour (12 minutes ago)
* aecb8cb - (REL-0.5.0, origin/master, origin/HEAD, master) Prepare release 0.5.0 (4 months ago)
```
2015-06-10 22:54:16 +00:00
In GitX it would look like this:
![gitx view](/img/imdb_fix_empty_poster_01.jpg)
2015-06-10 22:54:16 +00:00
Okay, now it's time to go and make a patch! All we really want are the two latest commits, stuff them in a file and send them to someone to apply them. But, since we created a separate branch, we don't have to worry about commits at all!
2017-03-20 15:35:19 +00:00
``` shell
git format-patch master --stdout > fix_empty_poster.patch
```
2015-06-10 22:54:16 +00:00
This will create a new file <code>fix_empty_poster.patch</code> with all changes from the current (<code>fix_empty_poster</code>) against <code>master</code>. Normally, git would create a separate patch file for each commit, but that's not what we want. All we need is a single patch file.
Now, you have a patch for the fix you wrote. Send it to the maintainer of the project ...
### Applying the patch
... who will apply the patch you just sent! But, before you do that, there are some other steps you should take.
First, take a look at what changes are in the patch. You can do this easily with <code>git apply</code>
2017-03-20 15:35:19 +00:00
``` shell
git apply --stat fix_empty_poster.patch
```
2015-06-10 22:54:16 +00:00
Note that this command does not apply the patch, but only shows you the stats about what it'll do. After peeking into the patch file with your favorite editor, you can see what the actual changes are.
Next, you're interested in how troublesome the patch is going to be. Git allows you to test the patch before you actually apply it.
2017-03-20 15:35:19 +00:00
``` shell
git apply --check fix_empty_poster.patch
```
2015-06-10 22:54:16 +00:00
If you don't get any errors, the patch can be applied cleanly. Otherwise you may see what trouble you'll run into. To apply the patch, I'll use <code>git am</code> instead of <code>git apply</code>. The reason for this is that <code>git am</code> allows you to <em>sign off</em> an applied patch. This may be useful for later reference.
2017-03-20 15:35:19 +00:00
``` shell
git am --signoff < fix_empty_poster.patch
Applying: Added specs to test empty poster URL behaviour
Applying: Added poster URL as part of cli output
```
2015-06-10 22:54:16 +00:00
2016-07-21 07:38:42 +00:00
Okay, patches were applied cleanly and your master branch has been updated. Of course, run your tests again to make sure nothing got borked.
2015-06-10 22:54:16 +00:00
In you git log, you'll find that the commit messages contain a "Signed-off-by" tag. This tag will be read by Github and others to provide useful info about how the commit ended up in the code.
![Signed off commit](/img/imdb_signed_off.jpg)
2015-06-10 22:54:16 +00:00
That's all folks!
<em>Are there any other git topics you'd like covered here? Please let me know!</em>
2015-03-26 11:28:08 +00:00