Sunday, 24 March 2013

Getting started with Git and Qt

I'll admit, they could have probably come up with a better name

So now I've started producing code that I would prefer not to lose, I've turned my mind to Version Control solutions and, ever one to go with a crowd, it seems that git is the way forward. I've used a few VCSs in my time including CVS (urgh), SVN (meh) and even Microsoft's Source Safe (Aaaarrrgghhh!), but now I've had a chance to play with git, it does seem to be significantly easier and more friendly. That being said though, I have to admit that one of the major selling points is the free github account with repo space. This allows me to not only store my code remotely, but also develop on whatever machine I happen to find myself in front of.

There are many good tutorials for git, so I'm just going to note here what I found useful to know and how to get it working with Qt. Fundamentally, git is a distributed VCS that allows copies of a code repository to be taken and edited in isolation from the main repo. Once the user is happy, they can push their changes to the main repo. Each remote copy can be committed to, rolled back, etc. by itself before this push takes place. The key commands are to achieve all of this are:

git init     # initialise a git repository
git clone    # clone an existing repo (e.g. from github)
git add      # add files to a repo
git commit   # commit changes to the local copy of the repo
git push     # push any committed changes to to the master repo
git tag      # Tag the current version of the repo

To get this working with Qt is very easy as Qt comes bunded with git support built in. For my projects, as I'd already started them, I first needed to create a git repo on github for them, clone the (empty) repo, copy the necessary files in to the appropriate directory and finally add, commit and push the changes. Note that you don't want to use git to manage the .pro.user Qt file as that is created on a per machine basis.

This gives you a working repo that you can now clone and start using. I found this easiest to do by deleting the previous repo from the local machine, firing up Qt and then selecting 'New Project' and then 'Project from Version Control' and 'Git Repository Clone'. Put in the Github details of the repo and Qt will do the rest. At this point you can do all editing, etc. within Qt and when you're happy, perform commits and pushes through this as well (Tools -> Git). The only thing I've currently found you can't do is tag through Qt - this must still be done through the command line.

Note that I'm sure there is a much easier way of setting up the project but this is how I got it to work and as it was trivial to do, couldn't really be bothered to look around for a more elegant solution. If you wish to look at my developing code base and laugh at the poorly written code, you can find it here:

https://github.com/doc-sparks?tab=repositories

Update: During my time with git, I found out that removing a tag can be a bit of a pain. I found the following to work quite well (creating a dummy one first as an example):

# start with a tag
git tag test

# push this to github
git push origin --tags

# delete this tag from local repo
git tag -d test

# finally, push this tag change to github (note that --tags won't work as the removed tag isn't included)
git push origin :refs/tags/test

No comments:

Post a Comment