Emacs, scripting and anything text oriented.

Creating a patch file using Magit

Kaushal Modi

Quick tip on how to create git patch files in Emacs using Magit.

Recently I came across few instances where people were asking questions related to creating patches for contributions to Emacs and Org mode repos here and then here. I was in the same shoes back then when I was about to make my first contribution to Emacs. And so I thought of sharing this tip on how to use Magit to create patch files.

If you have been using both Emacs and git, you might have already heard about the awesome Magit package. If you haven’t 😲, check out this screenshot-annotated review of what Magit is. With that out of the way, and assuming that you already have it installed  (use-package magit :ensure t) , here’s how to create a patch file using Magit ..

Single-file patch #

  1. Commit your changes to the git repo first.
  2. Bring up the Magit Log view. From the Magit status buffer, you would type l l to show the log of the current branch.
  3. Move the point to the commit that you want to send as a patch file, and hit W c c RET.
    • The last RET selects the commit the point is on, in the ∗magit-log∗ buffer.
    • If the first line of the commit log of the selected commit is “Update docstrings for shortdoc.el”, you’ll see a patch file named 0001-Update-docstrings-for-shortdoc.el.patch created in your git repo root.
    • You can now email this patch file as an attachment to bug-gnu-emacs@gnu.org (if contributing to Emacs) or to emacs-orgmode@gnu.org (if contributing to Org mode).

Multi-file patch #

If you need to create a multi-file patch i.e. patch files for a series of commits, select those commits in the ∗magit-log∗ buffer  The commit selection process is the same as how you would select text in any Emacs buffer. For example, if I want to create a series of 5 patches, I would go to the latest commit in the series, hit C-SPC and then C-n 4 times to select 5 rows of commits. , and then use the same W c c binding.

More Resources #

Here are the official contribution guides for Emacs and Org mode:

Here are some more resources that got shared in the Emacsverse recently (within the past year as of writing this):

  1. <2022-04-23 Sat> Contributing patches to Org – Ihor Radchenko
  2. <2022-04-09 Sat> Primer on formatting Git patches with Emacs (Magit) – Protesilaos Stavrou
  3. <2021-08-17 Tue> Contributing to Emacs – Colin Woodbury

Versions used: emacs 28.1.50
This is Day 18 of #100DaysToOffload.