mercurial Getting started with mercurial Pushing and Pulling


Mercurial makes it easy to share your work, and to pull in contributions from other developers. This involves three key steps; cloning, pulling, and pushing.


To copy a remote repository to your local disk you "clone" it. To do so simply pass the remote URL you'd like to clone from. To clone the Mercurial source code simply run:

$ hg clone

This creates a local hg directory containing a copy of the Mercurial repository you can build, edit, and commit to (though you can't publish your commits back to the parent repository).


Once you have a repository checked out, you'll want to keep it in sync as others publish changes to it. You can pull down new changes by simply running:

$ hg pull

This pulls in new commits but doesn't update your working directory, so you won't see any changes immediately. To update the contents of the working directory run:

$ hg up

Which updates your working directory to the tip (most recent) revision in the repository.

You can also run:

$ hg pull -u

To pull in new changes and update the working directory in one step.


Assuming you have write-access to the remote repository you can publish any commits you've made locally to the remote repository just as easily with:

$ hg push

This uploads your changes as long as there haven't been any other commits since the last time you pulled. If your push is rejected because it would "create additional heads" that means you need to pull in those new changes and merge them with your own.

$ hg pull
$ hg merge  # this creates a new changeset merging your changes with the remote changes
$ hg commit -m "Merged in remote changes"
$ hg push

Most of the time this is all you'll have to do since Mercurial handles merging your changes automatically, however sometimes you'll need to resolve merge conflicts manually (see merging topic). If you need to you can always cancel a merge and get back to a clean working directory with:

$ hg up -c

But remember this is a destructive operation; any changes in the working directory will be erased.