Getting Started with Git

At OpenSourcery we use Subversion for all of our version control but lately I've been using Git on some personal projects. A lot of people consider Git to be the next evolution of version control and I definitely agree. Some of the unique advantages that Git has over Subversion include:

Much easier branching and merging

Since Git makes branching and merging so simple I will often create a handful of "feature" branches on a given project and work on them whenever the mood strikes. When they are stable and tested they get merged back into the master branch and pushed out to the other feature branches. This method of branching and merging fits the schizophrenic approach that I naturally want to take toward development.

Easier Collaboration

Git allows you to track all your colleagues branches and pick out just the pieces of their code that you want. It also makes handing a volume of patches coming from mailing lists much easier.

Local development

Git allows you to save locally. This allows me to create lots of savepoints in my code at points where the test suite might not be passing and I wouldn't want to be committing into the trunk repository. When my code is tested and more stable I can push it out for the other developers to look at.

Getting Started

Let's say you have a development project in the directory devproject. Let's start using Git to manage this project.

First off install Git. In Debian and Ubuntu we just need to do "aptitude install git"

cd devproject
git init
git add .
git commit -m "My first commit"

At this point you have all the benefits of a local version control system but no one can see your work. To make it available to other people we'll need to install a remote repository on your server. At home, I only allow people to access my code through SSH so that's the method I am going to talk about here.

ssh alex
mkdir -p /var/git/devproject.git
cd /var/git/devproject.git
git --bare init
exit

Your remote Git server is now configured so let's set up our local repository to talk to the remote repository

cd devproject
git remote add origin ssh://alex/var/git/devproject.git

We can now push our changes to that repository:

git push origin master

Tagged as: git, sourcecontrol, svn