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

Incredible Demos

  • By
    HTML5 Input Types Alternative

    As you may know, HTML5 has introduced several new input types: number, date, color, range, etc. The question is: should you start using these controls or not? As much as I want to say "Yes", I think they are not yet ready for any real life...

  • By
    Display Images as Grayscale with CSS Filters

    CSS filters aren't yet widely supported but they are indeed impressive and a modern need for web imagery.  CSS filters allow you to modify the display of images in a variety of ways, one of those ways being displaying images as grayscale. Doing so requires 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!