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
    CSS 3D Folding Animation

    Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

Incredible Demos

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

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!