Programming Tools & Computer Science I

CSC 185 & 201, Spring 2013, Northern Virginia Community College

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:

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

  1. Create an account at Bitbucket, which provides free hosting of mercurial repositories. You'll be posting most of your homework to this account.

  2. 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.

  3. 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.

  1. 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
    
  2. Make a few edits on an existing file, and create a new file (put your name in the filename).

  3. Run hg status to see which files you've changed.
  4. 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.
  5. Run hg diff to see the lines you've changed.
  6. 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
    
  7. When you're satisfied with your changes, run hg commit to commit them, and give an informative message.

  8. Run hg outgoing to see which changes you have ready to post to the public server.
  9. Run hg incoming to see which changes are waiting on the public server for you to download.
  10. 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.
  11. After fetching the latest changes, post your changes by running hg push.
  12. Run hg log to view a log of all changes made so far.
  13. (Only if you're using TortoiseHg): run thg to launch a graphical browser.
  14. Run hg help for a summary of all these commands and a few more.

Readings