Using Mercurial’s Shelve Extension to Keep Your Work Safe While Switching Contexts
When a bug lands in the middle of your work, Mercurial’s shelve extension lets you stash uncommitted changes, pull updates, and bring your work back without committing. Learn how to enable, use, and clean up shelves in this practical guide.
23 Jun 2026, 20:26 UTC

Problem: You’re halfway through a feature and a critical bug lands in the main branch
In Mercurial, you often find yourself in the middle of a change that isn’t ready for commit. Suddenly a client reports a regression in the production branch. You need to pull the latest changes, run tests, and maybe even fix a bug—all without losing the work you’re doing.
Thesis: The shelve extension gives you a lightweight, non‑historical stash that lets you temporarily set aside uncommitted changes, pull updates, and then bring your work back.
What the Shelve Extension Does
- Creates a patch file that captures the differences between your working directory and the current revision.
- Stores that patch in
.hg/shelve, outside the normal revision history. - Allows you to
unshelvethe patch later, applying it on top of the latest changes. - Does not require a commit, so the shelf lives only on your machine.
Because the shelf is a simple patch, it works even in a repository that has never had a commit. That’s handy for very small projects or when you’re experimenting locally.
Typical Workflow Example
- Enable the extension (once per user or per repo)
# ~/.hgrc or .hg/hgrc [extensions] shelve= - Create a test repository
hg init testrepo cd testrepo echo "Hello" > file.txt hg add file.txt hg commit -m "Initial file" - Make experimental changes
echo "World" >> file.txt - Shelve the changes
hg shelve -n my-experiment # The patch is now stored in .hg/shelve/my-experiment.patchCheck that the working directory is clean:
hg statusshould show no modifications. - Pull updates from upstream
hg pull -u https://example.com/repoBecause the working directory is clean, the update will succeed without conflicts.
- Unshelve and resolve conflicts
hg unshelve -n my-experimentIf the same line was changed in the new revision, Mercurial will ask you to resolve the conflict. After editing the file, run
hg resolve --mark file.txtand thenhg commit -m "Reapply experiment on top of updated code". - Clean up the shelf
hg shelve --delete my-experimentThis removes the patch file from
.hg/shelveand frees up space.
Trade‑offs & Limitations
- Local‑only: Shelved patches live on your machine and are not pushed or cloned. If you need to share the experiment, you must commit it or export the patch manually.
- Untracked files: Unshelving will fail if the working directory contains untracked files that would be overwritten by the patch. Keep the directory tidy or use
hg purgebefore unshelving. - Performance on large trees: Creating a patch for a huge repository can be slow because the extension must diff every file. For very large codebases, consider using a lightweight commit instead.
- Manual cleanup: Shelves are not removed automatically. Use
hg shelve --listto see what’s pending andhg shelve --deleteto clear them.
Actionable Takeaway
If you frequently need to jump between unfinished work and urgent updates, enable the shelve extension and treat it as your “in‑progress stash.” It’s faster than committing and pushing, and it keeps your history clean. Just remember:
- Add
shelveto your~/.hgrcor use--config extensions.shelve=for one‑off runs. - After shelving, always run
hg statusto confirm the working directory is clean. - Before unshelving, check for untracked files that might block the patch. Clean them with
hg purgeif necessary. - Delete old shelves to avoid clutter—
hg shelve --delete <shelf>.
Give it a try next time you need to pull a hotfix without committing your half‑finished feature. Your workflow will feel smoother, and your repo history stays tidy.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.