Changes That Should Never Have Been

Most of the time, the hg backout command is exactly what you need if you want to undo the effects of a change. It leaves a permanent record of exactly what you did, both when committing the original changeset and when you cleaned up after it.

On rare occasions, though, you may find that you’ve committed a change that really should not be present in the repository at all. For example, it would be very unusual, and usually considered a mistake, to commit a software project’s object files as well as its source files. Object files have almost no intrinsic value, and they’re big, so they increase the size of the repository and the amount of time it takes to clone or pull changes.

Before I discuss the options that you have if you commit a brown paper bag change (the kind that’s so bad that you want to pull a brown paper bag over your head), let me first discuss some approaches that probably won’t work.

Since Mercurial treats history as accumulative—every change builds on top of all changes that preceded it—you generally can’t just make disastrous changes disappear. The one exception is when you’ve just committed a change, and it hasn’t been pushed or pulled into another repository. That’s when you can safely use the hg rollback command, as detailed in Rolling Back a Transaction.

After you’ve pushed a bad change to another repository, you could still use hg rollback to make your local copy of the change disappear, but it won’t have the consequences you ...

Get Mercurial: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.