Some notes about patch workflows

Patch Workflows

Thus far, all previous workflows rely heavily on branches. And every developer needs their own public repo. However, it’s also possible to collaborate by sending directly your patch to the maintainer.

A patch file consists a single set of changes(i.e., a commit) that can be applied to any branch, in any order.

Role: Developer

After you changed, staged and committed, you can create a patch from the new commit(s) using the git format-patch command.

$ git format-patch master

This created a file called 0001-<some-text>.patch which contents as below:

index 98e10a1..828dd1a 100644
--- a/pink.html
+++ b/pink.html
@@ -7,8 +7,7 @@
   <h1 style="color: #F0F">The Pink Page</h1>
-  <p>Pink is <span style="color: #F0F">girly,
-  flirty and fun</span>!</p>
+  <p>Only <span style="color: #F0F">real men</span> wear pink!</p>

   <p><a href="index.html">Return to home page</a></p>

While you don’t have to know the ins-and-outs to make use of patches, just know that a single patch file represents a complete commit. Since it’s a normal file and also an email, it’s much easier to pass around than a Git branch.

You can make several commits and the same amount patch files.

Now you can send them to the project maintainer.

There are two ways:

  1. Sending the patch file as an attachment to a normal email by hand.
  2. Using the convenient git send-email command, the guide can be refer to How to send patch files by git send-email.

Role: Maintainer

After getting the patch files, using the git am command, we can use the patches to add the new commits to our repository.

$ git am 0001-<some-text>.patch

Then we can integrate the patches:

$ git checkout master
$ git merge patch-integration
$ git branch -d patch-integration
$ git clean -f
$ git push

Note: git clean to get rid of the patch files.

The final patch workflow resembles the following:

Some notes about patch workflows


The patch workflow is just a simpler way to accept contributions than the previous workflows. Only the maintainer needs a public repository.

As a programmer, you’re most likely to use patches when you want to fix a bug in someone else’s project. After fixing it, you can send them a patch of the resulting commit. For this kind of one-time-fix, it’s much more convenient for you to generate a patch than to set up a public Git repository.

Quick Reference

  • git format-patch <branch-name>
    Create a patch for each commit contained in the current branch but not in <branch-name>. You can also specify a commit ID instead of <branch-name>.

  • git am [<] <patch-file>
    Apply a patch to the current branch.

Reference book

Ry’s Git Tutorial

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

访问:11.4 万