Initializing a new repository using Mercurial

Date: Fri Apr 25 2014 Mercurial

I just red through the semi-official Mercurial book (http://hgbook.red-bean.com/) and didn't see in there a section describing how to initialize a new repository. It talks at length about how to use existing repositories but not quite explicitly how to start a new one. However the quick start guide does discuss it, under "Setting up a new project".

$ cd project/
$ hg init # creates .hg

The

hg init
</span> command creates a blank repository that contains no files.  By default it does so in the current directory, an alternate method is:</p><p></p><div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;">$ hg init project</pre></div><p></p><p>This version causes the blank repository to be created in the given directory, and that directory is created if required.</p><p>As a blank repository it knows about no files.  A repository is rather useless without files.. so let's look at how to add them.  Assuming you have some existing files copy them into the repository directory.</p><p></p><div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;">$ hg add            # add those 'unknown' files<br>$ hg commit         # commit all changes, edit changelog entry</pre></div><p></p><p>The first does a recursive walk of the directory tree to record the existence of all the files.  The second puts them into the repository control, and along the way you'll be thrown into your $EDITOR to explain what you're doing.</p><p>It's possible to have mercurial ignore certain files in your directory tree, such as if there are build results or other flotsam lying around which shouldn't be checked in.  The <i><a href="http://www.selenic.com/mercurial/wiki/index.cgi/.hgignore">.hgignore</a></i> files lets you instruct mercurial what files to ignore.</p><p>Now let's test this new repository.  One is the <span class="geshifilter">
hg status
</span> command, which can be executed immediately after the <span class="geshifilter">
hg add
</span> command to see that the status is a bunch of new files, then again after the <span class="geshifilter">
hg commit
</span> to see that there are no files left to commit as they'd already been committed.</p><p>Another test is to make a copy of the repository.</p><p></p><div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;">$ cd ../<br>$ hg clone project project-copy<br>updating working directory<br>1459 files updated, 0 files merged, 0 files removed, 0 files unresolved<br>$ ls<br>project project-copy</pre></div><p></p><p>This makes a duplicate of the repository, the second is named "project-copy".  At this point both are identical and further clones can be made of either one.</p><p>Another test is to use <i>ssh</i> to access a repository.  In my case the repository is being set up within my dreamhost account and I want to make a duplicate of the repository on my local system.  The local system is a desktop running Ubuntu, but I also have other local systems running Mac OS X and I run a CentOS server to house my websites.  All these systems have ssh available.</p><p></p><div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;">$ hg clone ssh:<span class="spamspan"><span class="u">//user</span> [at] <span class="d">domain [dot] com</span></span>/path/to/project</pre></div><p></p><p>You may be asked for a password but that's all which is required to retrieve the repository over <i>ssh</i>.</p><p></p><div class="field field-type-emvideo field-field-extvideo">      <div class="field-label">extvideo:&nbsp;</div>    <div class="field-items">            <div class="field-item odd">                    <div class="emvideo emvideo-video emvideo-"></div>        </div>              <div class="field-item even">                    <div class="emvideo emvideo-video emvideo-"></div>        </div>              <div class="field-item odd">                    <div class="emvideo emvideo-video emvideo-"></div>        </div>              <div class="field-item even">                    <div class="emvideo emvideo-video emvideo-"></div>        </div>        </div></div><!-- google_ad_section_end --><div class="sexybookmarks-default-8125"></div></body></html>