hg-update
Update working directory to a specific revision
TLDR
Update to the tip of the current branch
Update to the specified revision
Update and discard uncommitted changes
Update to the last commit matching a specified date
SYNOPSIS
hg update [OPTIONS] [REVISION]
PARAMETERS
-C, --clean
Discard uncommitted changes. All uncommitted changes will be lost.
USE WITH EXTREME CAUTION
-r REV, --rev REV
Update to the specified revision. REV can be a branch name, tag, changeset ID, or other valid revision identifier. If not specified, it defaults to the 'default' branch.
-q, --quiet
Suppress output.
-v, --verbose
Enable verbose output.
--check
Exit with an error if the destination has uncommitted changes. This is similar to using -C and then checking if the working directory's parent revision changed.
--date DATE
Update to the newest revision before DATE.
DESCRIPTION
The hg update
command in Mercurial (hg
) is used to synchronize the working directory to a specific revision from the repository. It's a fundamental operation for moving between different points in the project's history, collaborating with others, and integrating changes. It can update the working directory to a specific named branch, a tag, a changeset ID, or the tip of the default branch.
The command compares the current state of the working directory with the specified revision and applies the necessary changes to bring it into alignment. This may involve adding, modifying, or removing files.
It's crucial to commit or shelve any outstanding changes in the working directory before running hg update
to avoid data loss or merge conflicts. Mercurial will attempt to merge uncommitted changes if possible, but this can lead to complex merge scenarios.
When updating, Mercurial will try to preserve local modifications if possible. However, if the specified revision contains conflicting changes, the update process may halt and require manual merge conflict resolution.
CAVEATS
Data loss can occur if uncommitted changes are discarded with the --clean
option. Always review the output of the command, especially when merge conflicts occur, to ensure the working directory is in the desired state.
MERGE CONFLICTS
If the update introduces conflicting changes between the working directory's uncommitted changes and the target revision, Mercurial will mark the conflicting files and require manual resolution. This involves editing the files to reconcile the differences and then marking them as resolved with hg resolve
before committing the changes.
DETACHED HEAD STATE
If you update to a specific changeset ID or a revision that is not the tip of a branch, your working directory will be in a "detached HEAD" state. This means that any commits you make will not be associated with a named branch. To create a new branch from this point, use hg branch
before committing.
HISTORY
hg update
has been a core command in Mercurial since its inception. Its purpose has always been to synchronize the working directory to a desired revision, facilitating navigation through the project's history and enabling collaboration between developers by allowing them to easily switch between different versions of the code.