Mercurial Distributed version control

Version 7.2.4 · source

Start here

hg status
What changed?
hg add <file>
Track a new file
hg commit -m "<msg>"
Save a changeset
hg pull -u
Get changes and update
hg push
Share changes
hg log -G
See history graph
hg diff
Show edits
hg update <rev>
Switch revision

Concepts

  1. Working dir your files
  2. Local repo changesets
  3. Remote default path

Setup

hg init
Create a repository here
hg clone <url> [<dir>]
Copy a remote repository
hg config --edit
Open your user hgrc
hg config ui.username
Show the configured author
hg paths
List remote aliases
hg root
Print the repository root
hg version
Show the Mercurial version
hg help <command>
Help for one command
hg help -e <ext>
Help for an extension

Snapshot

hg status
List changed files
hg add [<file>]
Track new files
hg commit -m "<msg>"
Commit all changes
hg commit -m "<msg>" <file>
Commit only some files
hg commit -i
Pick hunks interactively
hg commit --amend
Fold changes into the last commit
Only for draft changesets that have not been pushed.
hg diff
Show uncommitted changes
hg diff -c <rev>
Show what one changeset did
hg diff -r <a> -r <b>
Compare two revisions
hg addremove
Add new, forget missing files
hg remove <file>
Delete and untrack a file
hg forget <file>
Untrack but keep the file
hg rename <old> <new>
Move or rename a tracked file
hg copy <src> <dst>
Copy and keep history
hg revert <file>
Discard edits to a file
Leaves a .orig backup unless --no-backup is given.
hg revert --all
Discard all uncommitted edits
hg purge
Remove untracked files
Needs the purge extension enabled.

History

hg log
Show revision history
hg log -G
Show history as a graph
hg log -l <n>
Limit to n entries
hg log <file>
History of one file
hg log -r "<revset>"
Select with a revset
hg log -k <text>
Search commit messages
hg annotate <file>
Who changed each line
hg cat -r <rev> <file>
Show a file at a revision
hg summary
Summarize working directory
hg identify
Current revision and branch
hg heads
List branch heads
hg grep <pattern>
Search tracked files
hg bisect --bad
Mark the revision as bad
hg bisect --good <rev>
Mark a known good revision

Remotes

hg pull
Fetch changesets only
hg pull -u
Fetch and update
hg push
Send changesets to default
hg push -r <rev>
Push up to one revision
hg incoming
Preview what a pull brings
hg outgoing
Preview what a push sends
hg push --new-branch
Allow pushing a new branch
hg serve
Serve the repo over HTTP
hg bundle <file> <base>
Pack changesets into a file
hg unbundle <file>
Apply a bundle file

Update and branches

hg update <rev>
Switch the working dir
hg update -C <rev>
Switch, discarding local edits
hg branch
Show the current branch name
hg branch <name>
Name a new branch
Named branches are permanent in history; bookmarks are lighter.
hg branches
List named branches
hg bookmark <name>
Create a bookmark here
hg bookmarks
List bookmarks
hg update <bookmark>
Switch to a bookmark
hg bookmark -d <name>
Delete a bookmark
hg push -B <bookmark>
Push a bookmark
hg tag <name>
Tag the current revision
hg tags
List tags

Merge and undo

hg merge
Merge the other head into working dir
hg merge <rev>
Merge a specific revision
hg resolve --list
List files and merge state
hg resolve --mark <file>
Mark a conflict as resolved
hg resolve --all --tool <tool>
Re-run merge on conflicts
hg commit
Finish the merge with a commit
hg graft <rev>
Copy a changeset here
hg backout <rev>
Undo a changeset with a new one
hg shelve
Set aside local changes
hg unshelve
Restore shelved changes
hg shelve --list
List shelves
hg rollback
Undo the last commit or pull
Deprecated and dangerous: cannot be undone.
hg abort
Abort an unfinished operation
hg continue
Resume an interrupted operation
hg recover
Fix an interrupted transaction

Rewrite history

hg rebase -d <dest>
Move current branch onto dest
Needs the rebase extension enabled.
hg rebase -s <src> -d <dest>
Rebase a subtree
hg histedit <rev>
Edit, fold or drop commits
Needs the histedit extension enabled.
hg fold --from <rev>
Combine commits into one
hg phase
Show the phase of a revision
hg phase -f --draft <rev>
Make a changeset draft again
hg evolve
Fix up after a rewrite
Needs the evolve extension (separate install).
hg log -r "draft()"
List unpushed local work

Config and extensions

Revset shortcuts
Revset Meaning
tip newest changeset
. working dir parent
draft() not yet public
a::b a to b with ancestors
branch(<name>) on a branch
user(<name>) by an author
Common extensions
Extension Adds
rebase hg rebase
histedit hg histedit
shelve hg shelve
purge hg purge
evolve safe history rewriting
Basic ~/.hgrc
[ui]username = Your Name <you@example.com>[extensions]rebase =histedit =purge =
hg --config <section.key>=<val> <cmd>
Override a setting once
hg --debug <cmd>
Show detailed debug output
hg verify
Check repository integrity
hg manifest
List files in a revision
hg export <rev>
Dump a changeset as a patch
hg import <patch>
Apply a patch as a commit
hg archive <file>
Export a revision without .hg

Recipes

Daily loop
hg pull -u# edit fileshg statushg commit -m "Fix parser"hg push
Feature with a bookmark
hg bookmark feature-x# work and commithg push -B feature-x
Merge two heads
hg pullhg headshg mergehg resolve --listhg commit -m "Merge"
Tidy before pushing
hg log -r "draft()"hg histedit <first-draft>hg rebase -d default