Mercurial
Client
The command for Mercurial is hg
.
Create a repository
Suppose you want to set up version control for the my_code
directory.
cd my_code hg init # creates .hg directory hg add # adds everything from the current directory hg commit # has you enter a text description, then creates a first snapshot of the directory
Commit, Revert, Log
hg commit # has you enter a text description in a text editor, then creates a snapshot of the directory hg commit -m "my comment" # commit with this comment hg revert --all # reverts everything back to the last commit hg log # shows a list of all commits
Status, Diff, Remove, Add
> cp a.txt b.txt > emacs a.txt > rm favicon.ico > hg status M a.txt # Modified ? b.txt # "?" means mercurial doesn't know anything about this file yet ! favicon.ico # "!" means missing > > hg diff a.txt # show differences between last snapshot and current version > hg remove favicon.ico # really remove this file > hg add # add all new files to repository > hg status M a.txt A b.txt # Added R favicon.ico # Removed
You need to clear up all the ?s and !s in order to commit.
Cat, Diff, Update
hg cat a.txt # print current version of a.txt hg cat -r 0 a.txt # print version 0 of a.txt hg diff -r 0:1 a.txt # show differences between versions 0 and 1 of a.txt hg update -r 0 # revert to version 0 of the repository hg update # revert to the latest version of the repository
Working with a server
hg clone http://my_server:8000/ my_copy_of_the_code # name the directory as you like ... commit changes ... hg push # upload latest committed version to the server ... commit more changes ... hg outgoing # show differences between my current version and the server's
Push will fail if conflicting changes have been uploaded to the server in the meantime. If this happens, you need to merge the updates into your version:
hg incoming # show differences between my current version and the server's hg pull # pull down more recent changes from the server hg merge # merge my version with the latest changes from the server hg commit -m "merge" # commit the merge hg push # upload
Pull does not automatically update the files in your directory to the latest version, it just downloads the latest changes. Use parent to see what version number you're currently working with:
hg parent # show the changeset that’s in the working directory
Workflow:
- If you haven’t done so in a while, get the latest version that everyone else is working off of:
- hg pull
- hg up
- Make some changes
- Commit them (locally)
- Repeat steps 2-3 until you’ve got some nice code that you’re willing to inflict on everyone else
- When you’re ready to share:
- hg pull to get everyone else’s changes (if there are any)
- hg merge to merge them into yours
- test! to make sure the merge didn’t screw anything up
- hg commit (the merge)
- hg push
Server
Quick and dirty web server:
mkdir my_code cd my_code hg init hg serve # now accessible from http://my_server:8000/