In software development, version control system (VCS) hold an important role. Especially when the project is collaborated by many programmers. Besides to keep tracks of changes, version control could helps handle task distribution and later project integration from the programmers. Basically, there are two flavors of version control system: centralized and distributed. There are many comparison between these two flavors on the net, one of them explained it well with some illustrations. The key point between these two systems is there are local working copies of the project in distributed VCS, while in centralized VCS, every changes must be updated to the central repository.
Distributed VCS (DVCS)
There are some DVCS available to use. Two of them which gained popularity are Mercurial and Git. In this article, I will use Mercurial. Not because I don’t like Git or anything, it just I’m already familiar with Mercurial. You can also read a great Mercurial tutorial created by Joel Spolsky to familiarized yourself. Before we started to playing, I’ll give you some terms that will be used thorough this article. Get familiar with it, okay?
- central repository: place where anybody can access (read/write) and store the main ongoing project changes. It’s located on a server, whether placed in the Internet or Intranet.
- local repository: place where a person can access and store their own project changes. It’s located on the local directory of that person.
- clone: the action of cloning project from central repository to our own local repository.
- init: the action of registering project to the DVCS.
- commit: the action of submitting changes on local repository.
- push: the action of submitting changes from local repository to central repository.
- pull: the action of taking changes from central repository to local repository.
- merge: the action of merging changes between central and local repository, usually involving conflict resolutions.
Now that we already on the same page, let’s start the game, shall we?
Setting Up Central Repository
The first step is to set up the central repository. You can set it up on your local network, but in this article I try to use free Mercurial repository in the Internet. These Internet central repositories are also known as project hosting. I made a quick survey on some project hosting site, and here some information that I got:
|Project Hosting||DVCS||Free Plan|
|Google Code||Mercurial, Git||Open source project|
|Kiln||Mercurial||Startup & student|
|GitHub||Git||Open source project|
|BitBucket||Mercurial, Git||Up to 5 persons|
If you know another project hosting with great free plans, let me know. Based on my quick survey above, I decided to use Mercurial hosting from BitBucket for this article. Well, you can choose any hosting you want, there’ll be just need a small differences in the configurations. It’s not a big deal. Okay, here we go.
- Register for a free account in BitBucket (if you didn’t have it already). Don’t forget to confirm your email address after you successfully registered.
- Hover your mouse to Repository tab, and pick Create Repository.
- Enter the repository’s details. For this article, I created a Testing repo shown below. In this new repo, I create a Mercurial repository using Java.
- Our new repository is created. On the overview page, we can see information about our new repository. It’s said that our repository address is
usernameis your BitBucket user account. We will this information for configuration in our Netbeans project.
Setting Up Netbeans Project
In this tutorial, I used Netbeans 7.1 Beta. There might be some minor differences with the previous versions of Netbeans, but it still could work.
- First, we must make sure that the Mercurial plugin is active in Netbeans. Go to Tools > Plugins and navigate to Installedtab. Make sure that Mercurial has been activated.
- Now let’s heading to Netbeans. Open up your Netbeans and create a new Java project there. In this project, make a simple class, such as main class printing “Hello World”.
- Next, we initialized this project to Mercurial so that every changes we made are noted. In the initialization process, we added all the project’s files and directories to local repository. To do this go to Team > Mercurial > Initialize Project or right click on the project and pick Versioning > Initialize Mercurial Project. Notice a small tube icon appear on our project name in the left sidebar.Note: In Netbeans 6.9/6.9.1, you can find Mercurial on the Versioning toolbar above.
- Let’s add something. If you’re not already created main class, let’s create a main class with a simple main method to print the most popular phrase in the programming world: “Hello World”. Notice that our new class is marked green in the project window. It means our class is locally new and haven’t added to the local repository yet.
- Okay, let’s add our changes to the local repository or as we called it before as commit. To do this, go to Team > Commit… or right click on the project and pick Mercurial > Commit….
- A window will appear. In this window, we can set commit message and see all the changes that have been made by us. Since this is the first commit, we can see all the files are locally new and need to be added to the local repository. Let’s add some commit message to indicate our first commit.
- After we committing our changes in local repository, we can now committing our changes to the central repository or push. Let’s define our central repository based on the repository we created in BitBucket before. To do this go to Team > Share > Push to default…. Oops.. A window appear showing that we haven’t set the default push path yet. Let’s set it out. Go to Team > Properties. Set the default push and pull path with our BitBucket repository path.
- After we set up the default push path. Let’s retry pushing our changes by pick Team > Share > Push to default.... Enter your BitBucket username and password to push the changes. If our push is success, we can see our latest changes on the project’s Overview in BitBucket.
At this point, we already configured our Netbeans project to communicate with local and central repository of Mercurial. We can commit changes locally as frequently as possible. But never forget to push the changes to the central repository after we think our code is ready.
Pulling Changes from Central Repository
When the project involved many programmers, there’ll be a chance that the code in our local repository is not the latest version. Maybe someone in our team already altered the program with his portion of work. If we push our changes, while there are already some new changes in the central repository, then our push are marked as fail. Then we must pull the latest version from the central repository to our local and merge them with our local version. The process of merging changes from central with our local could involve some conflict resolution if there are portion of our modified code are altered by others in the central repository. I’ll not discussed the merging process here.
- To pull changes, we just need to go to Team > Share > Pull from default…. Since we already defined the default pull path, we can automatically contact the central repository and get the latest version of the code.
- If the pulling process successful, our project will be the latest version.
To pull an existing project in the project hosting to our new project, we can clone the project to our local. Using clone, we pull all the project files and directories from central repository to our local directory. We then can open the project from pulled directories. This is how to do it.
- Let’s assume we want to clone the Testing project above to our new (not created yet) project. Go to Team > Clone or Team > Clone from other… if you still opened the Testing project from before.
- On the repository URL, roll down the drop down menu and pick
https://. Then, we enter the project url and user information as necessary.
- On the next step, we set the default pull and push path of the project. Netbeans will set it up for us, so leave it alone.
- Next, we define the destination directory where the project’s clone will be placed. All the project files and directories will be downloaded and placed in this directory. Don’t forget to name the directory. Netbeans then will give option to scan for Netbeans project from the project we are going to clone. Let it be.
- After a moment, there’ll be a notification whether you will open the new cloned project or not. Pick Open Project and you’ll see the project you cloned from central repository.
Well, you know the basic now. With this, you can start tracking changes your own project, set up project for your group, or join an open source project. The choice is yours. You can also playing with another great DVCS like Git. Well, have fun..