Real Developers never start a Project without Source Control, Period! Mercurial on BitBucket

Real developers never start a project without source control, period! So, today I am going to walk you through the steps to use www.bitbucket.org’s Mercurial (Hg) repository.

Disclaimer: BitBucket has not sponsored this article, you can use any available online Mercurial repository

BitBucket offers both Mercurial and Git repositories. I started off with Hg because at the time they had better tooling for Windows. But Git tooling has fast come up to speed and is now on par. I also chose BitBucket over the more popular GitHub because it doesn’t allow free private repositories. You have to pay for private repositories. BitBucket allows private repositories, even for commercial purposes. 

Please read the EULA carefully. For me I needed a place to keep my blogging source code that I make available for free, so I don’t have a commercial interest.

- To start off, go to www.bitbucket.org and setup a free account.

- Next go to http://tortoisehg.bitbucket.org/ and download Tortoise for Hg. If you have used tortoise for SVN earlier, this will be very familiar to you. If not, no worries, we’ll walk through the steps to learn the basics.

- Install Tortoise, since it’s a shell extension you may need to restart the machine. Go ahead and restart.

- Hg is a distributed source control, meaning you have a complete repository locally and any changes you make and ‘check in’ or ‘commit’ goes into the local repository. This is advantageous in multiple ways
  • You can keep checking in your code without affecting others or the build (that should be from a build server’s repository).
  • In your local repository, you can revert and diff code for your changes or changes pulled down from server.
  • You can start a local repository first and then push it to the server once you are ready.
  • You can do a repository pull from the server to create your ‘clone’ and continue committing locally till you are ready to push changes to server.
Method 1: Creating a Local Repository and pushing it to server

Let’s say you are travelling in an airplane without access to Internet and you come up with a brilliant idea that you have to prototype, then and there. You can get started with source control right away.

Create your solution in VS, mine is called HgVersioning. Open Windows explorer and navigate to the folder. Right click on it and select TortoiseHg -> Create Repository Here

create-local-repository

The following dialog, will popup. Select ‘Create’. Click Ok for the confirmation dialog

local-repository-path

repository-created-successfully

Notice the folder will now have a (?) attached to it indicating it’s being watched for changes and that it has uncommitted changes.

new-hg-folder

Committing code locally

Right click on the folder and select ‘Hg Commit’. This will bring up a dialog similar to the one below

commit-initial

Right click on one of the file in obj folder and select Ignore. The following dialog will pop-up.

ignore-filter-obj-files

Change the selection highlighted from Blob (default) to Regexp. Basically we are telling Tortoise Hg to ignore obj folders because these keep changing with every build. Repeat the same for the bin folder

Now select the .suo file and add it to ignore list. Only this time keep the ignore type to Blob. Repeat for .hgignore file.

With all the non-required files ignored, your check in list should like the following

first-local-commit

Don’t forget to put comments because even though it’s a local commit when you push it to server all your comments will be pushed to server. Hit Commit. The code is now in your local repository.

You can continue to make changes and commit to local repository

Pushing code to server

Now let’s assume your first cut of prototype is done and checked in on your laptop. Your plane lands, you get to a place with Internet. So for safety you want the code off your machine into some place more ‘reliable’. Let’s see how to push the code to BitBucket.

Log in to BitBucket. To create a new Repository click on the big green + sign.

bitbucket-create-new-repository

Fill in the Name, Description, Repository Type and Public/Private status. Keep the name same as local.

bitbucket-repo-creation-1

Click ‘Create Repository’ to finish the repository creation process. You should see a success screen as follows.

bitbucket-repo-created-1

You can push code in two ways.

1. From the command line.

Navigate to the folder where your project is and type in
Hg push https://[youraccountname]@bitbucket.org/sumitmaitra/hgversioning
<Once you hit return, it will ask for your password>
Password: [Provide your BitBucket Password]

2. From the UI.

Right click on your Project folder and select ‘Hg Workbench’

invoke-hg-workbench

The workbench looks as follows, select the ‘Refresh’ button as selected below

add-remote-repository-url

Initially the “Remote Repository:” will be empty. Change the Url Type from ‘local’ to ‘https’.

Put in the User ID and url of remote repository, Finally provide the path to the repository.  The final URL should look something like below

provide-url-params-for-remote-repository

Now click on the ‘Push outgoing changes to selected URL’ button (highlighted above).

push-to-remote-confirmation

Click Yes to Confirm

password-confirmation

Provide your BitBucket account password and wait for the push to complete. A banner on top will indicate the push is complete.

push-to-remote-successful

There your code is now secure in a cloud hosted source control server and you don’t need to worry if your laptop crashes what will you do. Next we’ll see the second method of creating the project on BitBucket first and pulling it down

Method 2: Creating the project on Server first and committing the code back

Create a new project in BitBucket as shown earlier. Copy only the url part from the instruction on how to clone the repository

Open Windows Explorer, go to the folder where you want to download the project, right click, TortoiseHg->Clone

provide-remote-repository-url

Paste the URL in the Source, and hit Clone. Provide the password as prompted and done. New source controlled folder is ready

remote-repository-cloned-locally

Create your project and ‘Commit’ as earlier. Only difference is when you Push to server the URL will be ready for you so you don’t have to add it explicitly.

That concludes this article on how to get started on BitBucket using Hg.

Don’t let your code be without source control anymore!





3 comments:

Unknown said...

Awesome :)
There were some glitches at first, like ignoring multiple files & finally when pushing to URL.
It was throwing an error "Connection was forcibly closed."
Just cleared all entries from .ini file tried again & there I go.

Feels relaxed now :)

Thanks a ton.
Supreet

Sandeep said...

What is the difference between SVN and BitBucket and which one is more efficient?

Bimal said...

Very Interesting and Help ful article
Thanks Buddy. :)