Week 2-3: version control with Mercurial
2013-01-22
Writing complex software (or even simple programs) often requires many
revisions. It's useful to be able to track those revisions for many reasons,
including:
- looking back to find the revision that introduced a bug
- managing competing changes from several collaborators
- managing different versions of the source code
- just as a general history of changes/features added
Numerous version control systems have been created to help track revision
information. We'll be covering version control concepts using Mercurial,
a distributed version control system.
Agenda
-
Create an account at Bitbucket, which provides free hosting of
mercurial repositories. You'll be posting most of your homework to this
account.
-
Install Mercurial (should already be on the lab computers). On Windows, the
easiest way is to use TortoiseHg, which comes with a merge program and
other utilities. On Linux, it's probably even easier, but depends on which
distribution you're using. On a Mac, we can figure something out (I haven't
use it there before).
The Mercurial downloads page has an installer that can be
used when you don't have admin access, if necessary.
-
Set up Mercurial. Create a file called mercurial.ini
in your home directory
(or called .hgrc
on non-Windows systems). You can use this to define
basic preferences. Here's a template:
[ui]
username = Your Name <yourname@youremail.com>
merge = kdiff3
[extensions]
fetch =
About your home directory: on Windows 7, this is C:\Users\yourname
,
which is also listed in the %USERPROFILE%
environment variable. When
you start a command prompt, you'll usually start in your home directory.
-
Clone this example repository
for us to play with, by executing the hg clone
command in a shell:
hg clone https://bitbucket.org/jlepak/185-example
-
Make a few edits on an existing file, and create a new file (put your name
in the filename).
- Run
hg status
to see which files you've changed.
- Notice the
?
lines, which indicate new files that haven't been set
to be tracked yet. Use hg add
to add all newly created files.
- Run
hg diff
to see the lines you've changed.
-
If there are files in the directory that shouldn't be tracked, add them to
a file named .hgignore
in the root of the repository. For example, since
you probably don't want to track the .class
files created when you
compile Java source, you could add the lines:
syntax: glob
*.class
-
When you're satisfied with your changes, run hg commit
to commit them,
and give an informative message.
- Run
hg outgoing
to see which changes you have ready to post to the public
server.
- Run
hg incoming
to see which changes are waiting on the public server for
you to download.
- If there are incoming changes, run
hg fetch
to get the changes, merge
the changes with your own if necessary, and update your working directory
to the latest version.
- After fetching the latest changes, post your changes by running
hg push
.
- Run
hg log
to view a log of all changes made so far.
- (Only if you're using TortoiseHg): run
thg
to launch a graphical browser.
- Run
hg help
for a summary of all these commands and a few more.
Readings
-
Hg Init. The "Ground up Mercurial" chapter provides a good detailed
introduction, with a focus on usage in Windows. The other chapters (optional)
give even more detaiil.
-
Understanding Mercurial. This discusses details of how repositories
are structured.
-
If you'd like to browse to find some material that might be more to your liking,
see this list of beginner's guides.