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
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

Incredible Demos

  • By
    MooTools, Mario, and Portal

    I'm a big fan of video games. I don't get much time to play them but I'll put down the MacBook Pro long enough to get a few games in. One of my favorites is Portal. For those who don't know, what's...

  • By
    jQuery UI DatePicker:  Disable Specified Days

    One project I'm currently working on requires jQuery. The project also features a datepicker for requesting a visit to their location. jQuery UI's DatePicker plugin was the natural choice and it does a really nice job. One challenge I encountered was the...

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!