Wednesday, April 21, 2010

Mercurial





Sometimes you miss up your code with some changes that cause it not to work and you are unable to remember what you did or even worse not able to recover what you deleted, that's way their is something called Source Control, this helps you keep different versions of your projects, so you can just move between them without losing any thing, there are many stuff that do Source Control like cvs or svn, any way I came across something called Mercurial which seemed a nice thing to write about.
The guide is really good and simple, but I am gonna explain things little bit more and with some examples.

Installation

You can install Mercurial easily by typing sudo apt-get install mercurial in your terminal if you don't already have it, you also need to create a file name .hgrc in your home directory, Mercurial uses this file as your preferences, so you type your user name, and your preferred editor (ex. nano, vim or emacs).
my .hgrc file looks like this

[ui]
editor = nano
username = Khaled Hady

Creating a Repository

The first thing we need to do is to create a repository which is like telling Mercurial this folder is where I work and I want you to keep tracking any changes that I do.
If you already have a folder where your projects are then you will write two commands, let's say that my project is in a folder called Test in my home directory, and I have only one file "main.c" in it, that I want to keep track of.



I used cd to go to Test folder, then I used hg init to initiate a repository. I typed hg add because I want ALL the files to be tracked, if you want only some files to be tracked use
$ hg add first.c third.c
where first.c and third.c are the files you want to keep track of.
I also used the hg status command to know which files I am following and which I am not.





I used the nano command to edit the main file by adding few lines, then I added a new source file named newFile.c





After that I use hg diff to show the differences between the current version and the data in the files I am tracking, it tells me that there have been changes, so I type hg commit to make a new version that maintains the changes I made, and I am asked to type a comment which is important so I can keep track of my changes.





Now accidently, I changed the "newFile.c" and wrote some stupid code, then I commited the version so now I have two versions (0 and 1), I want my old version back (version 0)



first I will type hg log to see what each commit did in the project (so you must write reasonable words when you write the comment of the commit), then I will type hg update 0, I typed 0 cause I want to go to the first version which is zero, also you can use hg update tip to go to the latest version whatever it is.

Now the files are the same in version 0.




there is also a command called hg identify -n which tells you on which version you are now, if it has the form [num]+ (ex. 3+) this means you made changes to the version number 3 but hasn't commited these changes.


That's for now, there are other commands that we can talk about later.

1 comment:

  1. nice article...
    http://importantshock.wordpress.com/2008/08/07/git-vs-mercurial/

    ReplyDelete