Export and Import Patches with git

By  on  

Most of us that use git probably only have use GitHub -- thus is the popularity of their service.  If you (or a parent project) don't use a service like GitHub, however, you'll need to export patches for review.  Let's have a look at how to export a patch with git!

Exporting a Patch

Let's say you've created a feature branch for your impending changeset, made changes, and committed those changes.  Now it's time to export your commits to a patch file -- you would execute the following:

git format-patch master --stdout > my-patch-file.patch

The command above outputs your commits to a .patch file.  This patch file can be imported into other repositories for testing, application, etc.  Some repositories require the most detailed patch output.  You can provide that with:

git format-patch master --full-index --stdout > my-patch-file.patch

From the git documentation, --full-index signifies: Instead of the first handful of characters, show the full pre- and post-image blob object names on the "index" line when generating patch format output.

Importing a Patch

If you receive a patch file, you'll want to do a few checks before trying to merge it!

Ensure Patch Relevance

You can ensure the patch applies to the current set of work:

# See if patch is applicable
git apply --check my-patch-file.patch

# Ensure patch applies to current index
git apply --index my-patch-file.patch

View Patch Diff Information

If you want to know which files have been changed, added, or removed, you can use the following command:

# See which files have been changed
git apply --stat my-patch-file.patch

Merge the Code

Once you're satisfied with the patch code and want to merge and test (testing should be done on a feature branch), you can execute:

# Signs the patch by merger for history
git am --signoff my-patch-file.patch

Welcome to some of the operations that GitHub (and likewise services) do for us in the background.  I love doing stuff from command line but I'd much rather use  an elegant front-end for this type of stuff.  In the case you're stuck without a UI, however, keep these commands handy!

Recent Features

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

Incredible Demos

  • By
    Introducing MooTools NextPrev

    One thing I love doing is duplicating OS functionalities. One of the things your OS allows you to do easily is move from one item to another. Most of the time you're simply trying to get to the next or the previous item.

  • By
    MooTools Kwicks Plugin

    I wrote a post titled Get Slick with MooTools Kwicks ages ago. The post was quite popular and the effect has been used often. Looking back now, the original code doesn't look as clean as it could. I've revised the original...

Discussion

  1. Aruna

    Hey!! I have to submit a patch on Bugzilla and I want to do it via Git.
    But when I am running the first command that you mentioned and then I made the changes in the corresponding file,( I haven’t cloned the code) it isn’t recording the changes.Could you suggest something??

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!