How to create and apply patch by git

Source: Internet
Author: User

From: http://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git

Git is quite common nowadays and a lot of people are asking me how they can create a patch file. 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 shoshould 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 IMDB gem. you shoshould 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.

git clone git://github.com/ariejan/imdb.gitcd imdbgit checkout -b fix_empty_poster

Now, in the newfix_empty_posterBranch 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 infix_empty_posterBranch and didNotYet merge it back in tomasterBranch.

Creating the patch

Okay, I 've made some commits, here'sgit logForfix_empty_posterBranch:

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)

In gitx it wowould look like this:

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!

git format-patch master --stdout > fix_empty_poster.patch

This will create a new filefix_empty_poster.patchWith all changes from the current (fix_empty_poster) Againstmaster. Normally, git wocould 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 shoshould take.

First, take a look at what changes are in the patch. You can do this easilygit apply

git apply --stat fix_empty_poster.patch

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.

git apply --check fix_empty_poster.patch

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 usegit amInsteadgit apply. The reason for this is thatgit amAllows youSign offAn Applied patch. This may be useful for later reference.

git am --signoff < fix_empty_poster.patch Applying: Added specs to test empty poster URL behaviourApplying: Added poster URL as part of cli output

Okay, patches were applied cleanly and you're master Branch has been updated. Of course, run your tests again to make sure nothing got borked.

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.

That's all folks!

Are there any other git topics you 'd like covered here? Please let me know!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.