Posted in

How to generate a patch file in Mercurial?

If you’ve ever found yourself stuck trying to get your custom code changes, bug fixes, or feature tweaks across to a dev team or project maintainers, you know how frustrating it can be if you fumble the process. For me, as someone who’s spent the last five years running a small patch supply business, this is basically day-to-day work. I’ve lost count of how many times a dev has reached out panicking, saying “my Mercurial changes are a mess and I need a patch stat,” only to walk them through a process that’s way simpler than most people make it out to be. Let’s cut through the jargon and break this down like we’re chatting over a coffee—no stuffy manual fluff, just real, usable steps. Patch

First off, let’s make sure we’re on the same page about what a Mercurial patch even is. If you use Git, you’re used to committing your changes and pushing them, but Mercurial works a little differently (and honestly, I prefer it for patch work because it keeps changes way more organized for handoffs). A patch file is just a plain text file that lists exactly what lines you added, removed, or modified in your code, right down to the line numbers. That means when you send it to someone, they can apply those exact changes to their own repo without messing up their existing work—super useful if you’re selling patches for niche software, fixing security holes in old projects, or rolling out custom mods that the original dev team hasn’t gotten around to yet.

Now, before you do anything, let’s get your local repo in order. This is the #1 mistake I see people make: they have half-finished changes, uncommitted junk, or old files lying around that they don’t mean to include, and end up sending a patch that’s full of random extra garbage. Here’s my go-to pre-patch checklist that I’ve refined over hundreds of orders, because trust me, if your patch is messy, the devs you’re working with will flag it and you’ll have to redo it.

First, make sure your working directory is clean. That means no uncommitted changes, no untracked files sitting around. To check that, just open your terminal, cd into your Mercurial project folder, and run hg status. If you see anything listed as “M” (modified) or “?” (untracked), stop right there. For modified files, you can either commit them (if they’re final) or shelve them (that’s a Mercurial command that tucks unfinished changes away temporarily) if you don’t want them in the patch. For untracked files, if they’re not meant to be part of your changes, just delete them or ignore them with a .hgignore file—no one wants a patch that adds a random debug.log or a temp folder full of test files.

Next, make sure you know exactly which changeset you’re working from. A changeset in Mercurial is just a single, unique set of changes you made, identified by a long hex string or a shorter nickname. If you made your changes a few commits back, you don’t want to include every single commit you’ve made since that point—you want to target just the change or group of changes that make up your patch. For example, if you fixed a login bug three commits ago, and those three commits are all related to the user auth system, that’s your target.

Now, let’s get to the actual patch generation. There are a few ways to do this, depending on whether you want a patch for your latest change, a range of changes, or just a single specific changeset. I’ll walk you through each one, since different clients need different things.

First, the simplest one: generating a patch for your very last, uncommitted change. Wait—hold on, you don’t have to commit your changes first to make a patch? Yep, that’s right. A lot of devs think you have to commit everything first, but that’s not true, and it’s a waste of time if you’re just testing a change. To make a patch from your current working directory (the changes you haven’t committed yet), just run hg diff > my-patch-name.patch. That’s it. Let me break that down: hg diff outputs all the changes between your working files and the last committed changeset, and the > symbol tells your terminal to save that output into a file named my-patch-name.patch. Name that file something descriptive, too—don’t call it patch1.patch (I’ve gotten so many of those from new devs that I have no idea what’s in them). Call it login-bug-fix-v2.patch or ssl-cert-security-update-for-v1.2.0.patch—specific names save everyone time.

But what if your change is already committed? That’s super common. If you made a changeset and committed it, you can still make a patch from it. Let’s say you want the patch for just that one specific commit. First, run hg log to list all your recent commits. You’ll see each commit has an ID—usually like changeset: 123:abc123def456—the number is 123 here, and the hex part is the unique ID. If you just want that one commit as a patch, run hg export 123 > my-commit-patch.patch. That’s perfect for when you need to sell a patch for a single bug fix, or want to send only that change without all the others in your history.

What if you need multiple commits in a single patch? Maybe you have three small fixes that all relate to a single feature, and the client wants them rolled into one patch to apply all at once. No problem. Mercurial lets you target a range of changesets. Let’s say your commits are numbered 121, 122, 123, and you want to take from commit 121 up to and including 123. The command is hg export 121:123 > three-fixes-patch.patch. Easy peasy. Pro tip here: double-check the range by running hg log -r 121:123 first to make sure you’re grabbing exactly the right commits. I can’t tell you how many times I’ve seen someone accidentally include a random commit from a different feature just because they messed up the range number.

Wait, one more thing: what if you want a patch that shows exactly what changed, even for binary files? Sometimes patches for images, executables, or other non-text files get messed up, but Mercurial has a fix for that. The regular hg diff works fine for text, but for binaries, add the -g flag? No, wait—actually, the hg diff command has a --binary option if you need to include binary changes in your patch. So if you’re patching a custom theme that has image files, run hg diff --binary > theme-update.patch and it will handle the binary data correctly so the patch applies without breaking those files. Most of the time you won’t need this, but it’s a lifesaver when you do.

Now, here’s a trick I learned when I first started as a patch supplier: always test your patch before sending it to a client. There’s nothing worse than selling a patch that doesn’t apply, because you messed up the process. How do you test it? Just make a copy of your repo, apply the patch to the copy, and check if everything works. To apply a patch, run hg import my-patch-name.patch in a test repo. If there are conflicts, Mercurial will tell you right away, so you can fix them before sending it over. I test every single patch I sell—my customers count on that, and it’s why I’ve got a 98% satisfaction rate after all these years.

Let me walk you through a quick example to make this super concrete, like we’re doing it together. Say I’m working on a client’s e-commerce platform, and I fixed a bug where the discount code field was accepting spaces, causing checkout errors. First, I check my repo: I run hg status and it only shows the modified file src/checkout/discount.js—no untracked junk, perfect. I committed that change as changeset 456, with a message “fix: remove spaces from discount code input to prevent checkout errors”. Now I want to make a patch for this client, so I run hg export 456 > discount-code-fix-v1.0.patch. I double-check the patch by opening it in a text editor: it shows exactly the line I changed, the old code, the new code, the commit message—nothing extra. Then I test applying it to a local copy of the client’s repo: I make a new folder, clone their repo, copy the patch over, run hg import discount-code-fix-v1.0.patch, and check the discount.js file to make sure the change is there. Looks good, so I send it off to the client. That’s the whole process, start to finish, in like two minutes.

Wait, a common question I get: can you create a patch that compares two different repos, not just your own? Yeah, sure. If you have two separate Mercurial repos (like an older version of a project and a newer version with your fix), you can generate a patch by running hg diff -r /path/to/old-repo -r /path/to/new-repo > cross-repo-patch.patch. That’s handy if someone has a standalone copy of old software and needs a patch to bring it up to date with a newer version you’ve fixed.

Now, let’s talk about best practices for patch suppliers, because this isn’t just for devs working on their own projects—when you’re selling patches, small details make a huge difference. First, always include a clear commit message in your patch. The hg export command automatically includes the commit message, which is great because it tells the client exactly what the patch does, why it exists, and any important notes (like “only applies to version 2.1.0, will break on older versions”). Second, name your patch file clearly, like I said before—no generic names. Third, include a README or a note with the patch that says what versions it’s compatible with, any known issues, and how to apply it (most clients will know, but new ones appreciate a quick note). Fourth, never include unrequested changes. I once got a request for a patch to fix a calendar bug, and the dev sent me a patch that also changed the font on the entire site. I had to send it back and ask for just the calendar change, which wasted both our time. So stick exactly to what the customer asks for.

If you’re looking for high-quality, tested Mercurial patches tailored to your project needs, we can work together to get exactly what you need—whether it’s bug fixes, security updates, feature tweaks, or custom mods. Reach out to us to discuss your requirements, and we’ll deliver reliable, ready-to-apply patches that save you time and hassle.

When working with Mercurial patches, official documentation and reliable dev resources can help you troubleshoot edge cases, like handling large binary patches, resolving apply conflicts, or understanding advanced range targeting for complex changesets. These references are key for refining your process, especially as you take on more varied patch requests for different projects and versions.

Cosmetic Paper Box References:
Mercurial Official Documentation.
Practical Mercurial: A Guide for New Users.
Version Control with Mercurial.


Xiamen Baihuasheng Printing Technology Co., Ltd.
Xiamen Baihuasheng Printing Technology Co., Ltd. is one of the most professional patch manufacturers and suppliers in China. With abundant experience, we are committed to providing high quality products at competitive prices for global clients. Welcome to buy bulk high-grade patch in stock here and get free sample from our factory.
Address: No.23 Xiangyue Road, Xiang’an District, 361101, Xiamen, Fujian, China
E-mail: suoogan@qq.com
WebSite: https://www.bhsprinting.com/