Posted almost 11 years ago. Visible to the public. Linked content.

Git: How to create and apply patches

You can convert git commits into patch files. Those can be used to apply to a different repository [1] or by someone else (e.g. sent when sent to them via e-mail).

Creating a patch

  1. Make your changes and commit them.
  2. Run git format-patch COMMIT_REFERENCE to convert all commits since the referenced commit (not including it) into patch files.

For example, let's say you prepared 2 commits. Run:

git format-patch HEAD~~

This will create 2 files, one for each commit since HEAD~~, like these:

0001-make-stuff-more-awesome.patch 0002-allow-users-to-be-locked.patch

When the commit lives in Gitlab, you may append ".patch" to the commit URL to get a patch-formatted response. Save the page (Ctrl + S) to a patch file and continue below.

Applying patches

You can use git apply some.patch to have the changes from the .patch file applied to your current working directory. They will be unstaged and need to be committed by you.

To apply a patch as a commit (with its commit message), use git am some.patch. \
For all patches to be applied, simply run:

git am *.patch

Note that in some previous version you could pass the latest patch filename of a list of patches to apply all previous patches as well:

git am 0002-allow-users-to-be-locked.patch # May no longer work for you

You then have the 2 unpushed commits from the patch file created earlier.

More information

[1] Note that you can add other git repositories/directories as a remote source. Sometimes you don't want (or can't do) that but can still use patches.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).

Owner of this card:

Arne Hartherz
Last edit:
almost 4 years ago
by Dominik Schöler
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more