Monday, January 3, 2011

Getting started with GIT

I recently started working on a new project and decided to try out the source repository GIT. As a long time user of CVS and Subversion (SVN), these distributed repositories take a little time getting used to.
As long as you use it yourself in a one man project, a distributed repository is almost the same as CVS and SVN. You check code out, make modifications and commit. The major difference is that you then "push" your commits to another repository, usually a remote server.

Create a local repository

To get started, after installing GIT, you can create a repository with the "init" command.
cd /path/to/repository
git init

Make modifications

Just like CVS and SVN, you can add/modify/delete files and commit them.
git add readme.txt
git commit -a -m "added readme text"
Note that this commit is stored locally, it is not committed to a central server.

Copy a repository

You can create a copy of a local repository with the "clone" command. This way you can locally make a branch.
cd /path/to/workspace
git clone /path/to/repository

Shareable repository

To create a sort of "central" repository like CVS or SVN, you can create a shareable repository with the "init" command. Do this on your server.
cd /path/to/repository
git init --bare --shared

Checkout with SSH

I use SSH to "checkout" from the remote repository, do this on your client.
cd /path/to/workspace
git clone ssh://myserver/path/to/repository
This makes a copy of the remote repository to my local workspace. Here I can make modifications and commit them. GIT will store your commit locally until you "push" this to the central repository.
git add readme.txt
git commit -a -m "added readme text"
git push origin master
This last command will update the remote repository with your commits.

See also