Mercurial for Beginners: The Definitive Practical Guide
Solution 1
How do you configure it to ignore files?
Ignore is configured in a normal text file called .hgignore in the root of your repository. Add it just like a normal file with:
hg add .hgignore
There are two syntax options available for file matching, glob and regexp. glob is unix-like filename expansion and regexp is regular expressions. You activate each by adding syntax: glob
or syntax: regexp
on a line by itself. All lines following that will use that syntax, until the next syntax marker. You can have as many syntax markers as you want. The default syntax is regexp, so if you only use regexp you don't need any syntax marker.
You can add comments with #
Example:
# python temporary files
syntax: glob
*.pyc
#editor autosaves
*~
# temporary data
syntax: regexp
temp
Ignore only applies to unmanaged files (i.e. files that are not already checked in). To ignore files that are under version control, you can use the switches -I and -X.
Solution 2
How do you see what's uncommitted, or the status of your current codebase?
To see a list of files that have been changed:
$ hg status
This will print each file that has been changed along with its status, which can include:
M
- Modified. The file has been changed and the changes have not been committed.A
- Added. The file was not tracked before, but if you commit Mercurial will begin tracking it.R
- Removed. The file was tracked before, but if you commit Mercurial will cease tracking it in this and future commits.?
- Unknown. The file is not currently tracked by Mercurial. Committing will have no effect on it unless you usehg add
to add it.!
- Missing. The file was tracked but Mercurial cannot find it in the working copy.
To see the changes that have actually been made to the files:
$ hg diff
Solution 3
How do you create a new project/repository?
$ hg init my-repository
Solution 4
How do I interface with Subversion?
There are three ways:
The convert extension will clone an existing Subversion repository into a Mercurial one. It comes with Mercurial. It works roughly like this:
hg convert <Subversion URL or directory> <path to new Mercurial repository>
For example this will grab the trunk of the SixApart memcached repository.
hg convert http://code.sixapart.com/svn/memcached/trunk
The extension can incrementally bring in new revisions from a Subversion repository into the Mercurial one (a little like pull). However it does not support taking Mercurial revisions and sending them back to Subversion (no push). [XXX: Correct this if it is wrong].
The hgsubversion extension. It is in many ways the most sophisticated solution as it uses the Subversion API to communicate with the Subversion repository. It aims to become the hg-svn bridge. It allow full round-tripping of revisions (full clone, pull, and push), However as of this writing [XXX: Amend this if/when it becomes incorrect] it is still in development and there are not yet official releases. As a consequence it works with only the most up-to-date Mercurial (1.3 as of this writing).
- It maps tags and branches (preceding all tags with
tags/
to distinguish them from equivalently named branches). - It maintains a special branch
closed-branches
for closing off branches which are removed in Subversion. - It requires that the Subversion repository be laid out according to the convention of trunk/branches/tags.
- The command set is typically
hg svn <subcommand>
though it aims at being integrated to the point that you don't need the 'svn' part (i.e. it wants to treat a Subversion clone as much as possible like any other Mercurial repository).;
It works like this:
clone:
hg svnclone <Subversion URL>
OR (only for svn://
URLs)
hg clone <svn:// URL>
pull:
hg svn pull
push:
hg svn push
incoming:
hg svn incoming
outgoing:
hg svn outgoing
Checking out an entire repository:
hg svnclone http://code.sixapart.com/svn/memcached
The hgsvn utility (bitbucket tree). Up until recently this only let you clone and pull a Subversion repository, but as of hgsvn 0.1.7
it supports push. [I do not know how well it does push. Anyone with more experience should update this.] It has the following notable features:
- It generates a Mercurial tag for every SVN tag.
- It puts a local tag on every changeset to mark its SVN revision.
- It puts every Mercurial revision on a named branch named after its SVN branch. For example
branches/some-feature
would be likehg branch some-feature
. It puts the trunk ontrunk
(i.e. nothing is on the Mercurial default branch, unless the user explicitly switches to it.) - It will try to identify branches and tags, and create them but if it can't it just skips them. This is handy when the Subversion repository is not following the conventional trunk/branches/tags layout.
It works like this:
clone:
hgimportsvn <Subversion URL>
pull:
hgpullsvn
push:
hgpushsvn
incoming:
hgpullsvn -n
outgoing:
hgpushsvn -n
Checking out an entire repository:
hgimportsvn http://code.sixapart.com/svn/memcached
Checking out just the trunk:
hgimportsvn http://code.sixapart.com/svn/memcached/trunk
Solution 5
How do you compare two revisions of a file, or your current file and a previous revision?
Both use hg diff
. When hg diff
is used all changes in the working copy and the tip (the latest commit) is displayed.
For "How do you compare two revisions of a file?"
$ hg diff -r{rev1} -r{rev2} {file.code}
The above command will show different between rev1 and rev2 of "file.code".
For "How do you compare your current file and a previous revision?"
$ hg diff {file.code}
The above command will show different between the current version of "file.code" and the lastest revision (the lastest commited).
:D
Related videos on Youtube
Laz
Updated on June 27, 2020Comments
-
Laz about 4 years
Inspired by Git for beginners: The definitive practical guide.
This is a compilation of information on using Mercurial for beginners for practical use.
Beginner - a programmer who has touched source control without understanding it very well.
Practical - covering situations that the majority of users often encounter - creating a repository, branching, merging, pulling/pushing from/to a remote repository, etc.
Notes:
- Explain how to get something done rather than how something is implemented.
- Deal with one question per answer.
- Answer clearly and as concisely as possible.
- Edit/extend an existing answer rather than create a new answer on the same topic.
- Please provide a link to the the Mercurial wiki or the HG Book for people who want to learn more.
Questions:
Installation/Setup
- How to install Mercurial?
- How to set up Mercurial?
- How do you create a new project/repository?
- How do you configure it to ignore files?
Working with the code
- How do you get the latest code?
- How do you check out code?
- How do you commit changes?
- How do you see what's uncommitted, or the status of your current codebase?
- How do you remove files from the repository?
- How do you destroy unwanted commits?
- How do you compare two revisions of a file, or your current file and a previous revision?
- How do you see the history of revisions to a file or repository?
- How do you handle binary files (visio docs, for instance, or compiler environments)?
- How do you merge files changed at the "same time"?
- How do you revert a Changeset?
- How do you go back to a previous version of the code?
- How do you extract a patch from a specific changeset?
- How do you record that you renamed or deleted a file without using the Mercurial command?
Tagging, branching, releases, baselines
- How do you 'mark' 'tag' or 'release' a particular set of revisions for a particular set of files so you can always pull that one later?
- How do you pull a particular 'release'?
- How do you branch?
- How do you merge branches?
- How do you merge parts of one branch into another branch?
Other
- Good GUI/IDE plugin for Mercurial? Advantages/disadvantages?
- Any other common tasks a beginner should know?
- How do I interface with Subversion?
Other Mercurial references
- Mercurial: The Definitive Guide
- Mercurial Wiki
- Meet Mercurial | Peepcode Screencast
- Mastering Mercurial | TekPub Screencast
- Hg Init - ground-up Mercurial tutorial
-
Steve Losh almost 15 yearsThis is the heavyweight way that git people always make fun of Mercurial people for recommending. A lighter, more familiar way to beginners would be
hg branch my-branch
-
Mathieu almost 15 yearsYou must note, that these are two completely different operations.
-
Martin Geisler almost 15 yearsThe origin is recorded in
.hg/hgrc
when you do a clone, so you don't have to specify it when you pull/push. You can add more paths to the[paths]
section in.hg/hgrc
if you want. -
Nicolas Dumazet over 14 yearsTo build mercurial from source, one will need the Python headers. Install python-dev or python-devel for those using package-oriented distributions.
-
Debilski over 14 yearsI just used
hg convert
and it really takes a long time when you have an svn repository with many commits. Setting up a local repository withsvnsync
helps a lot speeding things up – especially when you need to do it several times, because you had some wrong options in your convert command. -
quark over 14 yearsI think all these methods are faster if you have a local copy of the SVN repository. But you can't always get at the full repository right? I thought
svnsync
needs to access the actual files of the repository, not just the URL. -
mbillard over 14 yearsI found this guide that could be helpful for branching in Mercurial: stevelosh.com/blog/2009/08/a-guide-to-branching-in-mercurial
-
hultqvist about 14 yearsFor lightweight branches in hg that are not permanent(as the hg branch) use the mercurial bookmarks extension
-
Casebash almost 14 yearsThe article linked by GoodEnough provides another lightweight method using bookmarks. Seems pretty good for changes that aren't significant to deserve their own branch
-
Wim Coenen over 13 yearsIsn't this what
hg backout
is for? -
slf over 13 yearsYes, however hg backout only supports a single changeset id to backout. Definitely should be mentioned, I will update accordingly
-
Jon L. over 13 yearsPlease note that hgsvn is now in Maintenance Mode, so it is no longer being actively developed. They recommend using hgsubversion instead.
-
DOOManiac almost 12 yearsNewer versions of Mercurial do not need the transplant extension. You can use the built-in
graft
command to do the same.hg help graft
for more info -
Jamin Grey over 11 yearsAs a new mercurial user, and as someone new to source revision control in general, I don't really understand this answer. Could you clarify and expand your post? I still don't quite understand how to use the merge command.