Mercurial Version Control

Creating a Mercurial Repository

First, create a project directory and change into that.

c:\>mkdir hgtest

c:\>cd hgtest

Initiate the Mercurial repo, notice that the command for mercurial is hg (got to love the sense of humor, right?)

c:\hgtest>hg init

c:\hgtest>hg status

Add content to the repository

Let's create a file with some silly content

c:\hgtest>echo "just some text" >> file.txt

Notice that the status now return a '? file.txt' which means that it detects an unversioned file called file.txt

c:\hgtest>hg status
? file.txt

Add that file to the repository

c:\hgtest>hg add .
adding file.txt

Verify that it is now Added

c:\hgtest>hg status
A file.txt

Commit these changes to the repository

c:\hgtest>hg commit -m "initial commit of file"

Modify content and keep the history

Realize that the contents of the file included "" so issue a new command to the file and echo the text without quotes

c:\hgtest>echo just some text > file.txt

As you would expect, this is a change so the file is marked as Modified

c:\hgtest>hg status
M file.txt

Commit the modification to the repository

c:\hgtest>hg commit -m "change contents of file.txt"

Create a new branch and use it

Create a new branch, we will implement a new feature (a new line of text, but you get the idea?)

c:\hgtest>hg branch some_more_text
marked working directory as branch some_more_text

Modify the file further, adding a new line

c:\hgtest>echo some more text >> file.txt

Verify that the change is detected by mercurial (this step is not needed)

c:\hgtest>hg status
M file.txt

Commit these changes to the some_more_text branch

c:\hgtest>hg commit -m "add some more text in some_more_text branch"

Type the contents of that file to the screen

c:\hgtest>type file.txt
just some text
some more text

Change back to the default branch

c:\hgtest>hg update -C default
0 files updated, 0 files merged, 0 files removed, 0 files unresolved

Notice that here the file.txt contents are different

c:\hgtest>type file.txt
just some text

Merge in the changes we made in the some_more_text branch

c:\hgtest>hg merge some_more_text
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)

Verify that the new content has been added, we now see some more text

c:\hgtest>type file.txt
just some text
some more text

This is important to remember; the change was commited in the some_more_text branch, but ut is considered a change in the default branch. As such, we will need to commit it before it's stored in the repository for the default branch.

c:\hgtest>hg status
M file.txt

c:\hgtest>hg commit -m "merge with some_more_text"

Take a while and review the following log:

c:\hgtest>hg log
changeset:   3:3fb4fccd0bf6
tag:         tip
parent:      1:2fe7608e6738
parent:      2:d7bd27dc951e
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:28:46 2012 +0100
summary:     merge with some_more_text

changeset:   2:d7bd27dc951e
branch:      some_more_text
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:15:54 2012 +0100
summary:     add some more text in some_more_text branch

changeset:   1:2fe7608e6738
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:14:14 2012 +0100
summary:     change contents of file.txt

changeset:   0:6d1abbe729c2
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:11:53 2012 +0100
summary:     initial commit of file

You can see that all your commit issues has been kept

Branches can modify the same files

c:\hgtest>hg branch change_some_text
marked working directory as branch change_some_text

Reset the file to contain

just some text
some other text

Here are the commands to do that from the command line, you could of course edit these files from a text editor

c:\hgtest>echo just some text > file.txt

c:\hgtest>echo some other text >> file.txt

c:\hgtest>type file.txt
just some text
some other text

c:\hgtest>hg status
M file.txt

c:\hgtest>hg commit -m "some other text"

c:\hgtest>hg status

Change back to the default branch

c:\hgtest>hg update -C default
1 files updated, 0 files merged, 0 files removed, 0 files unresolved

c:\hgtest>type file.txt
just some text
some more text

Make some changes here as well

c:\hgtest>echo even more text >> file.txt

c:\hgtest>type file.txt
just some text
some more text
even more text

c:\hgtest>hg status
M file.txt

c:\hgtest>hg commit -m "even more text"

Now it's time to merge in the 'some other text' sentence from the change_some_text branch, but we want to keep our 'even more text' sentence.

c:\hgtest>hg merge change_some_text
merging file.txt
0 files updated, 1 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)

c:\hgtest>type file.txt
just some text
some other text
even more text

c:\hgtest>hg status
M file.txt

c:\hgtest>hg commit -m "merge with change_some_text"

The resulting complete log

c:\hgtest>hg log
changeset:   6:6df21f71a5f7
tag:         tip
parent:      5:de632a6d32f1
parent:      4:e95ac861d415
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:49:07 2012 +0100
summary:     merge with change_some_text

changeset:   5:de632a6d32f1
parent:      3:3fb4fccd0bf6
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:48:44 2012 +0100
summary:     even more text

changeset:   4:e95ac861d415
branch:      change_some_text
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:47:41 2012 +0100
summary:     some other text

changeset:   3:3fb4fccd0bf6
parent:      1:2fe7608e6738
parent:      2:d7bd27dc951e
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:28:46 2012 +0100
summary:     merge with some_more_text

changeset:   2:d7bd27dc951e
branch:      some_more_text
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:15:54 2012 +0100
summary:     add some more text in some_more_text branch

changeset:   1:2fe7608e6738
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:14:14 2012 +0100
summary:     change contents of file.txt

changeset:   0:6d1abbe729c2
date:        Alexander Brevig <alexanderbrevig@gmail.com>
date:        Tue Feb 14 12:11:53 2012 +0100
summary:     initial commit of file
blog comments powered by Disqus

Published

28 February 2012

Category

Tags