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 #
- Commit your changes to the git repo first.
- Bring up the Magit Log view. From the Magit status buffer, you would type l l to show the log of the current branch.
- 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
0001-Update-docstrings-for-shortdoc.el.patchcreated in your git repo root.
- You can now email this patch file as an attachment to firstname.lastname@example.org (if contributing to Emacs) or to email@example.com (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):