Using Mercurial Shelve to Temporarily Set Aside Uncommitted Changes
Learn how to store local edits with hg shelve, work on another task, and restore the changes safely.
05 Sept 2025, 12:38 UTC

Desired outcome
You have uncommitted modifications in your working directory and need to switch to another branch or task without committing incomplete work. The goal is to store those changes temporarily, return to a clean state, perform the alternate work, and then restore the original edits exactly as they were.
Prerequisites
- Mercurial version 2.0 or later (the
shelvecommand was introduced in 2.0). - A repository that contains at least one modified, added, or removed file.
- No active merge, rebase, or bisect operation that would interfere with shelving.
- Read/write access to the repository directory.
Procedure
- Shelve the current changes
From the root of the repository run:
hg shelveThis creates a shelf entry containing all pending modifications, additions, and deletions. The command will prompt for a shelf name if you have multiple shelves; accepting the default creates a name like
default. - Verify a clean working directory
Check that no changes remain:
hg statusThe output should be empty, indicating a clean state.
You can also list shelves to confirm the new entry:
hg shelve --listExpect to see exactly one shelf, e.g.,
default. - Perform the alternate work
Switch branches, apply a patch, or make any other changes needed. Because the working directory is clean, these actions will not interfere with the shelved edits.
- Restore the shelved changes
When you are ready to continue with the original work, run:
hg unshelveIf you used a non‑default shelf name, specify it:
hg unshelve shelf-name.This reapplies the stored modifications to the working directory.
- Optional: Delete the shelf
After confirming the unshelve succeeded, remove the shelf to free space:
hg shelve --delete shelf-nameYou can list shelves again to verify deletion.
Expected checks
- After
hg shelve,hg statusshows no modified, added, or removed files. - After
hg unshelve, runninghg diffproduces the same output as the diff taken before shelving. - The status after unshelving matches the original state (same set of modified/added/removed files).
- No new commit is created during the shelve/unshelve cycle; you can confirm with
hg log -r .showing the same revision identifier before and after.
Recovery options
- Unshelve conflicts: If applying the shelf fails because the working directory has diverged, Mercurial will leave conflict markers in the affected files. Resolve the markers manually, then continue. You can abort the operation with
hg unshelve --abortand retry later. - Discard a problematic shelf: If the shelf is corrupted or you no longer need it, delete it with
hg shelve --delete shelf-nameand start over. - Accidental unshelve of wrong shelf: List shelves (
hg shelve --list) to ensure you target the correct name before unshelving.
Limitations and notes
- Shelves are stored only in the local repository clone; they are not transferred by
hg pushorhg pull. To share work-in-progress, commit changes or use patches. - Avoid shelving while an active merge or rebase is in progress; the resulting shelf may be unclear and cause merge conflicts upon unshelve.
- The shelf mechanism does not version the shelved changes themselves; if you need a history of temporary states, consider committing to a temporary branch instead.
Concrete example
Suppose you are working on a feature branch and have edited src/main.c and added src/new_util.c. You discover a critical typo in the release branch that must be fixed immediately.
- Run
hg shelve; the shelf nameddefaultnow holds your two changes. - Confirm with
hg status(no output) andhg shelve --list(showsdefault). - Switch to the release branch:
hg update release. - Fix the typo, commit, and push.
- Return to your feature branch:
hg update feature. - Restore your work:
hg unshelve. Verify withhg diffthat the edits tosrc/main.cand the addition ofsrc/new_util.care exactly as before. - Optionally delete the shelf:
hg shelve --delete default.
Following these steps lets you pause work, handle urgent tasks, and resume without losing or duplicating changes.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.