Quantcast
Channel: Jamie Hurst - Web Developer » python
Viewing all articles
Browse latest Browse all 2

Version Control with Mercurial

$
0
0

Version Control with MercurialThree years ago, if someone had asked me what version control was, I would’ve genuinely had no idea, and I’m quite ashamed of that. To be quite honest, I would never wish that feeling of simple stupidity on anyone, and so this post will be a quick introduction to the idea behind version control, and how to get to grips with one of the latest SCM (software configuration management) tools, Mercurial, which is my preferred choice at the moment. There are some more traditional SCM tools, such as CVS and Subversion, which still work well in the majority of circumstances, but Mercurial is designed to be a distributed tool, where the source code in its repositories can be worked on from multiple locations at any time, and the merging to the central server handled at a later date.

Mercurial Basics

The first thing you’ll need to do is download the Mercurial binary. As far as I’m aware, it isn’t included on any major system distributions, certainly not on Windows or Mac in any case, although it may be packaged up with some Linux offerings. So, your first stop is Mercurial’s website, which will guide you through the simple installation process. You may also need to install Python if you don’t already have the language set-up on your system.

Create a Repository

Once Mercurial is installed, you’ll have access to the hg command-line tool. This is Mercurial’s core, and where you’ll handle all of your version control needs. To create a new repository for a new project, first you need to create a new directory (normally mkdir on most systems), then cd into that directory and run the following command:

hg init

This tells Mercurial to set up the directory as a repository, creating all the hidden files it needs to store the history of the repo and keep track of all the changes. Now create some files that you might want to store in the repo such as some source code, and add them to the repository using this command:

hg add (files)

Finally, you need to commit the changes you’ve made to the repository, so run this command

hg commit -m 'My first commit'

Congratulations! Your repository is now at revision 1!

Managing the Repository

This is exactly how version control works. You make some changes to the files in the repository, add some new files and possibly remove some old ones, and then commit the changes with a helpful message to remember what you did at a later date. There are just a couple more commands that will help to get you started with your own repository. To remove a file from the repository, use this command:

hg remove (files)

And, to quickly commit all changes, adding and removing any files automatically without using the hg add and hg remove commands, use the following syntax:

hg commit -Am 'Message'

Cloning

Of course, one of Mercurial’s more powerful abilities is to clone other repositories, allowing the user to make changes locally before pushing the changes back to the central server. To clone an existing repository, use the following command:

hg clone (central-repository) (destination)

Then you can work on the repository as normal. When you’ve committed some changes and are ready to synchronise with the central server, use the following command:

hg push

This pushes all outstanding changes to the server, ensuring it is kept up to date. However, the server could be more up-to-date than your local repository, which would mean you’d need to pull the changes and then update your local repository to the current revision, just like this:

hg pull
hg update

And that’s the basics of using Mercurial’s cloned repository feature. As ever, I’m only showing you the very basics here. There are a lot of different circumstances you might want to foresee, such as merging changes between repositories, updating or rolling back to a previous revision, tagging and branching, and more. To read up on these features and how they are jused in the Mercurial tool, the best place is always Mercurial’s website.

Another problem that might arise is how to serve your repositories across the internet. Cloning from a local source works fine, but defeats the purpose of working in a decentralised environment, to clone effectively, you need to be able to do it across the internet. Normally this is done in one of two ways: using SSH as a wrapper for the Mercurial command, or using HTTP to serve the repositories. There are a lot of details to go into with both of these methods, and the best way to do this is search on Google, as I don’t want to baffle everyone with the details in this simple post. If you’re not so inclined as to search on Google, here is an example of how to setup Mercurial to serve using Apache.

Helpful Clients

Personally, I hate using the command-line unless I absolutely have to, so if you’re like me, you’ll probably want to know if there are any decent clients for Windows, Mac and Linux out there, and luckily, there are!

Windows – TortoiseHg
Based on the brilliant TortoiseSVN, which integrates nicely with Windows Explorer meaning no nasty gigantic client windows cluttering everything up, TortoiseHg can handle everything from committing and updating, to merging, diff’ing and more! When I was back using Windows, this was my tool of choice, and did the job very nicely!

Mac – MacHg
This has only been a recent discovery myself, but MacHg is simply fantastic for managing tons of repos in a GUI environment! The history graphs, simple directory window and helpful icons mean it is really easy to use and live with. It does have a couple of things I would change, such as TextMate integration, but I might have a go at writing a couple of scripts myself to sort out things like that!

Linux
I’ll be honest, I don’t use Linux with a desktop very often at all, so I’m not completely aware of any simple GUI-native interface for Mercurial on this platform. Have a look at Mercurial’s own list of tools and see whether there is something there that jumps out!

Bitbucket

Just one final piece of information. Bitbucket is a fantastic repository centre that will manage projects using Mercurial for you. It only gives you one free private repository currently, but an unlimited number of public ones, so it can be extremely handy for tracking a project and making it available online for others to use. I highly recommend giving this website a look for starting up your Mercurial lifestyle through, as it really does make things a lot easier with wikis, issue tracking, user management and more!


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images