home > developer > MercurialCommandLineTips.html

Saturday, January 9th, 2010

Mercurial Command Line Quick Reference

There are two main ways I’ve been using Mercurial over the last couple years: on the command line and via tortoisehg (right-click) on Windows. This is a quick reference to the command-line interface. Mercurial on the command line is called “hg” (a reference to the element Mercury).

Help

>> hg            % lists basic commands
>> hg help       % lists all commands
>> hg help pull  % gives detailed help on command "hg pull"

Where am I?

>> hg status    % tell which files have been modified
>> hg st        % same as hg status (you can abbrevitate
                %    non-ambiguous commands)
>> hg log -l 5  % log of last 5 commits
>> hg diff      % current changes to your working directory
>> hg diff -c 9 % changes made by revision 9

Committing as you work

>> hg commit -m "change 1"  % regular commit
>> hg add new.c             % tell hg to start tracking a file
>> hg ci -m "change 2"      % "ci" is short for commit (legacy)
>> hg rename new.c blue.c   % rename a file (like copy + remove)
>> hg revert ---all          % oops, I didn't want to rename
>> !del blue.c              % del manually (stop tracking blue.c)
>> hg mv new.c red.c        % "mv" is short for rename
>> hg ci -A -m "change 3"   % "-A" adds/del's files, avoids 
                            %    the "hg add, hg remove" dance

Syncing with another repository

>> hg pull      % grab any new changes from default repo
>> hg update    % update your working directory with the changes
>> hg merge     % merge new code with your code
>> hg ci -m "merge" % commit merge result
>> hg fetch     % short for "hg pull, hg update, hg merge, hg ci"
>> hg push      % send your changes to the default repo

Merging

>> hg heads     % list the potential heads that could be merged
>> hg merge     % auto-merge (specify rev with -r if >2 heads)
>> hg resolve   % if auto-merge failed, retry after manual edits

Repository management

>> hg init          % make a new repo in the current dir
>> hg clone C:\Repo % clone the repo to work on a branch/idea

Advanced, but nice (many require enabling extentions)

>> hg view      % launches a graphical history viewer
>> hg paths     % where this repo pushes and pulls from
>> hg rollback  % undo the last commit (only OK if not pushed)
>> hg shelve    % stash work-in-progress to fix something else
>> hg unshelve  % resume your work-in-progress
>> hg rebase    % move a set of commits to a new baseline
>> hg tip -p    % show latest committed changes

Hope this helps. Please send me ideas for other commands that you like to use often.

/developer   〆   permalink