Tutorial - Initializing a Repository

(This page is part of the Tutorial series. Previous part is TutorialInstall, next part is TutorialClone)

You have followed TutorialInstall to install Mercurial already, right? Good!

In Mercurial, we do all of our work inside a repository. A repository is a directory that contains all of the source files that we want to keep history of, along with complete histories of those source files (inside the .hg directory — see UnderstandingMercurial).

Unlike some version control systems, you can create a repository in any directory you have write permissions to. All you have to do is initialize the repository, which will create a .hg subdirectory, and add some files to the repository.

To do this, we use the init command.1

Let's init a small "hello, world" repository in our file system.

(telephoto: /tmp) mkdir repo
(telephoto: /tmp) cd repo
/tmp/repo
(telephoto: /tmp/repo) ls
(telephoto: /tmp/repo) hg init

We can now see that our new repo has the .hg directory:

(telephoto: /tmp/repo) ls -a
./  ../  .hg/

We can now add some files to mercurial. In this example we're creating the files after we've initialized the repository, but that's not important in mercurial. One of the great things about mercurial is that it's easy to add version control to an existing directory structure. In either case, we just use "hg add" to place the files under version control.

(telephoto: /tmp/repo) touch hello.txt
(telephoto: /tmp/repo) ls
hello.txt
(telephoto: /tmp/repo) ls -a
./  ../  hello.txt  .hg/
(telephoto: /tmp/repo) hg add hello.txt

After adding the files they're not really "in" mercurial yet. For that we have to commit them:

(telephoto: /tmp/repo) hg commit -m "adding initial version of hello.txt"
(telephoto: /tmp/repo) ls -a
./  ../  hello.txt  .hg/
(telephoto: /tmp/repo) 

Nothing obvious changes about the files now that they've been committed, but this version of our files is now preserved in the repository.

A common way to use mercurial is to clone an existing repository that someone else has created. The next lesson shows you how to do that, in TutorialClone.


CategoryTutorial CategoryTutorial

TutorialInit (last edited 2011-05-30 11:18:39 by 212)