Git for Source Control

Posted on February 15, 2009

Source Control is an essential part of the development system that any and every programmer needs to utilize. Simply put it is a repository of source code that keeps track of revisions and is a system that allows multiple programmers to work on one copy of code without issues. There are numerous version control systems (VCS) out there including Subversion, Mercurial and Bazaar. Everyone has their preference, today we will be focusing on git, a VCS that has become more popular in the latest months.

Git

A typical programmer’s workflow would be to:

  1. pull (download) the latest copy of code from the git respository.
  2. Make neccessary changes, modify code etc.
  3. commit the changed files
  4. push (upload) the files back to the git repository.

If multiple programmers were working on the same project they would all use the same git repository. It’s good practice to always pull the latest copy before you push any of your changes to make sure your using the most update-to-date copy of the code (Someone may have pushed another change while you were editing your code). You can also branch (fork) a git repository. For example two programmers might be working on the same code but both are implementing different features. Their work flow would be something like:

  1. Each of them fork the master (main) copy of code
  2. Make appropriate changes to their branches/forks of code
  3. Merge both forks back into master (Git does this for you)

Installing Git

Git is very easy to install and use on *nix type operating systems (Linux, Mac, BSD). Using Macports or your package management system, install the package git-core. It’s a little more complicated on Windows. I use msysgit. Download and install that. On Windows git is not integrated into the windows command prompt, instead it has it’s own *nix like terminal (cygwin?). You use *nix commands rather than windows commands -  ls instead of dir. Fire up the shortcut to ‘git bash’ (The git command prompt), you should be able to cd around, viewing your directories, and when you type git it should bring you up a list of available commands.

Git Basics

To use Git you must have a repository. You can host one yourself or use one of the numerous Git Hosts out there. I use Github, it allows unlimited repositories for open source projects, and also offers paid plans for private repositories. For the rest of this tutorial I’ll assume your using github. The first thing you need to do is set your username and email. (In the git shell)

git config --global user.name "Username Here"
git config --global user.email "email@email.here"

Note that we use –global, this tells git that we want these details to be applied to all of our projects, you can can override these settings when in the local project directory by calling the command again without the –global flag.

Now we need to generate an SSH key. Git requires it as it’s an extra step of authentication. There are numerous ways to generate the key and they are different on all platforms so I advise you read this (github.com) tutorial on setting it up.

The final step is to create the git project itself. On Github, login to an account (that you have created) then head over to the create a repository page. Enter the information and hit Ok.

Now we can make that project on our local system. Change into the project’s directory then

git init

This generates the .git directory and git will now start ‘watching’ deletes, alterations and the like. Now we need to add the files that we want to commit. You can name them individually or to select all we simply use the decimal point.

git add .

That will add the entire directory’s contents and all sub directories. We can then commit our changes/additions. We also provide a commit message, explaining to ourselves/others/repository readers what changes you made in that commit – eg new features in the code.

git commit -m 'This is our first commit'

Now we need to attach our project to the repository, then push the changes we made. Again, this is fairly simple.

git remote add origin git@github.com:GITHUBUSERNAME/REPOSITORYNAME.git
git push origin master

You can view your project’s page on github now and you will see that the files have been added. From now on it’s just a matter of git add-ing altered files, git commit-ing then finally git push-ing to upload the changes.

Leave a Reply

You must be logged in to post a comment.

Sourcebits

iPhone Development and Flex Development

Categories