{"id":3980,"date":"2026-12-04T00:45:20","date_gmt":"2026-12-03T16:45:20","guid":{"rendered":"http:\/\/www.imeric-valvebags.com\/blog\/?p=3980"},"modified":"2026-12-04T00:45:20","modified_gmt":"2026-12-03T16:45:20","slug":"how-to-generate-a-patch-file-in-mercurial-4561-002fc2","status":"publish","type":"post","link":"http:\/\/www.imeric-valvebags.com\/blog\/2026\/12\/04\/how-to-generate-a-patch-file-in-mercurial-4561-002fc2\/","title":{"rendered":"How to generate a patch file in Mercurial?"},"content":{"rendered":"<p>If you\u2019ve 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\u2019s spent the last five years running a small patch supply business, this is basically day-to-day work. I\u2019ve lost count of how many times a dev has reached out panicking, saying \u201cmy Mercurial changes are a mess and I need a patch stat,\u201d only to walk them through a process that\u2019s way simpler than most people make it out to be. Let\u2019s cut through the jargon and break this down like we\u2019re chatting over a coffee\u2014no stuffy manual fluff, just real, usable steps. <a href=\"https:\/\/www.bhsprinting.com\/patch\/\">Patch<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.bhsprinting.com\/uploads\/48921\/small\/aroma-air-freshener-sticker6b638.jpg\"><\/p>\n<p>First off, let\u2019s make sure we\u2019re on the same page about what a Mercurial patch even is. If you use Git, you\u2019re 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\u2014super useful if you\u2019re selling patches for niche software, fixing security holes in old projects, or rolling out custom mods that the original dev team hasn\u2019t gotten around to yet.<\/p>\n<p>Now, before you do anything, let\u2019s 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\u2019t mean to include, and end up sending a patch that\u2019s full of random extra garbage. Here\u2019s my go-to pre-patch checklist that I\u2019ve refined over hundreds of orders, because trust me, if your patch is messy, the devs you\u2019re working with will flag it and you\u2019ll have to redo it.<\/p>\n<p>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 <code>hg status<\/code>. If you see anything listed as \u201cM\u201d (modified) or \u201c?\u201d (untracked), stop right there. For modified files, you can either commit them (if they\u2019re final) or shelve them (that\u2019s a Mercurial command that tucks unfinished changes away temporarily) if you don\u2019t want them in the patch. For untracked files, if they\u2019re not meant to be part of your changes, just delete them or ignore them with a <code>.hgignore<\/code> file\u2014no one wants a patch that adds a random <code>debug.log<\/code> or a temp folder full of test files.<\/p>\n<p>Next, make sure you know exactly which changeset you\u2019re 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\u2019t want to include every single commit you\u2019ve made since that point\u2014you 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\u2019s your target.<\/p>\n<p>Now, let\u2019s 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\u2019ll walk you through each one, since different clients need different things.<\/p>\n<p>First, the simplest one: generating a patch for your very last, uncommitted change. Wait\u2014hold on, you don\u2019t have to commit your changes first to make a patch? Yep, that\u2019s right. A lot of devs think you have to commit everything first, but that\u2019s not true, and it\u2019s a waste of time if you\u2019re just testing a change. To make a patch from your current working directory (the changes you haven\u2019t committed yet), just run <code>hg diff &gt; my-patch-name.patch<\/code>. That\u2019s it. Let me break that down: <code>hg diff<\/code> outputs all the changes between your working files and the last committed changeset, and the <code>&gt;<\/code> symbol tells your terminal to save that output into a file named <code>my-patch-name.patch<\/code>. Name that file something descriptive, too\u2014don\u2019t call it <code>patch1.patch<\/code> (I\u2019ve gotten so many of those from new devs that I have no idea what\u2019s in them). Call it <code>login-bug-fix-v2.patch<\/code> or <code>ssl-cert-security-update-for-v1.2.0.patch<\/code>\u2014specific names save everyone time.<\/p>\n<p>But what if your change is already committed? That\u2019s super common. If you made a changeset and committed it, you can still make a patch from it. Let\u2019s say you want the patch for just that one specific commit. First, run <code>hg log<\/code> to list all your recent commits. You\u2019ll see each commit has an ID\u2014usually like <code>changeset:   123:abc123def456<\/code>\u2014the number is 123 here, and the hex part is the unique ID. If you just want that one commit as a patch, run <code>hg export 123 &gt; my-commit-patch.patch<\/code>. That\u2019s 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.<\/p>\n<p>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\u2019s say your commits are numbered 121, 122, 123, and you want to take from commit 121 up to and including 123. The command is <code>hg export 121:123 &gt; three-fixes-patch.patch<\/code>. Easy peasy. Pro tip here: double-check the range by running <code>hg log -r 121:123<\/code> first to make sure you\u2019re grabbing exactly the right commits. I can\u2019t tell you how many times I\u2019ve seen someone accidentally include a random commit from a different feature just because they messed up the range number.<\/p>\n<p>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 <code>hg diff<\/code> works fine for text, but for binaries, add the <code>-g<\/code> flag? No, wait\u2014actually, the <code>hg diff<\/code> command has a <code>--binary<\/code> option if you need to include binary changes in your patch. So if you\u2019re patching a custom theme that has image files, run <code>hg diff --binary &gt; theme-update.patch<\/code> and it will handle the binary data correctly so the patch applies without breaking those files. Most of the time you won\u2019t need this, but it\u2019s a lifesaver when you do.<\/p>\n<p>Now, here\u2019s a trick I learned when I first started as a patch supplier: always test your patch before sending it to a client. There\u2019s nothing worse than selling a patch that doesn\u2019t 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 <code>hg import my-patch-name.patch<\/code> 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\u2014my customers count on that, and it\u2019s why I\u2019ve got a 98% satisfaction rate after all these years.<\/p>\n<p>Let me walk you through a quick example to make this super concrete, like we\u2019re doing it together. Say I\u2019m working on a client\u2019s 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 <code>hg status<\/code> and it only shows the modified file <code>src\/checkout\/discount.js<\/code>\u2014no untracked junk, perfect. I committed that change as changeset 456, with a message \u201cfix: remove spaces from discount code input to prevent checkout errors\u201d. Now I want to make a patch for this client, so I run <code>hg export 456 &gt; discount-code-fix-v1.0.patch<\/code>. 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\u2014nothing extra. Then I test applying it to a local copy of the client\u2019s repo: I make a new folder, clone their repo, copy the patch over, run <code>hg import discount-code-fix-v1.0.patch<\/code>, and check the <code>discount.js<\/code> file to make sure the change is there. Looks good, so I send it off to the client. That\u2019s the whole process, start to finish, in like two minutes.<\/p>\n<p>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 <code>hg diff -r \/path\/to\/old-repo -r \/path\/to\/new-repo &gt; cross-repo-patch.patch<\/code>. That\u2019s 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\u2019ve fixed.<\/p>\n<p>Now, let\u2019s talk about best practices for patch suppliers, because this isn\u2019t just for devs working on their own projects\u2014when you\u2019re selling patches, small details make a huge difference. First, always include a clear commit message in your patch. The <code>hg export<\/code> 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 \u201conly applies to version 2.1.0, will break on older versions\u201d). Second, name your patch file clearly, like I said before\u2014no generic names. Third, include a README or a note with the patch that says what versions it\u2019s 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.<\/p>\n<p>If you\u2019re looking for high-quality, tested Mercurial patches tailored to your project needs, we can work together to get exactly what you need\u2014whether it\u2019s bug fixes, security updates, feature tweaks, or custom mods. Reach out to us to discuss your requirements, and we\u2019ll deliver reliable, ready-to-apply patches that save you time and hassle.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.bhsprinting.com\/uploads\/48921\/small\/disposable-nitrile-gloves-packaging-color-boxc564c.jpg\"><\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/www.bhsprinting.com\/color-box\/cosmetic-paper-box\/\">Cosmetic Paper Box<\/a> References:<br \/>\nMercurial Official Documentation.<br \/>\nPractical Mercurial: A Guide for New Users.<br \/>\nVersion Control with Mercurial.<\/p>\n<hr>\n<p><a href=\"https:\/\/www.bhsprinting.com\/\">Xiamen Baihuasheng Printing Technology Co., Ltd.<\/a><br \/>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.<br \/>Address: No.23 Xiangyue Road, Xiang&#8217;an District, 361101, Xiamen, Fujian, China<br \/>E-mail: suoogan@qq.com<br \/>WebSite: <a href=\"https:\/\/www.bhsprinting.com\/\">https:\/\/www.bhsprinting.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019ve ever found yourself stuck trying to get your custom code changes, bug fixes, or &hellip; <a title=\"How to generate a patch file in Mercurial?\" class=\"hm-read-more\" href=\"http:\/\/www.imeric-valvebags.com\/blog\/2026\/12\/04\/how-to-generate-a-patch-file-in-mercurial-4561-002fc2\/\"><span class=\"screen-reader-text\">How to generate a patch file in Mercurial?<\/span>Read more<\/a><\/p>\n","protected":false},"author":982,"featured_media":3980,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3943],"class_list":["post-3980","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-patch-4e0b-00f0cc"],"_links":{"self":[{"href":"http:\/\/www.imeric-valvebags.com\/blog\/wp-json\/wp\/v2\/posts\/3980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.imeric-valvebags.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.imeric-valvebags.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.imeric-valvebags.com\/blog\/wp-json\/wp\/v2\/users\/982"}],"replies":[{"embeddable":true,"href":"http:\/\/www.imeric-valvebags.com\/blog\/wp-json\/wp\/v2\/comments?post=3980"}],"version-history":[{"count":0,"href":"http:\/\/www.imeric-valvebags.com\/blog\/wp-json\/wp\/v2\/posts\/3980\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.imeric-valvebags.com\/blog\/wp-json\/wp\/v2\/posts\/3980"}],"wp:attachment":[{"href":"http:\/\/www.imeric-valvebags.com\/blog\/wp-json\/wp\/v2\/media?parent=3980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.imeric-valvebags.com\/blog\/wp-json\/wp\/v2\/categories?post=3980"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.imeric-valvebags.com\/blog\/wp-json\/wp\/v2\/tags?post=3980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}