Setting Up Your First Git Repository

Guest Author - Julie L Baumler

The first thing you need to do is install and configure git on your machine. If you are running windows, my earlier article Installing Git On Windows explains how to do this. You can find OS X installers for git here. For Linux and Unix, your local package management tool (pkgadd, yum, dpkg, rpm, etc) likely has access to a git package, if it isn't already installed. Finally, you have the option of installing from source. The git source is available here (see my article Installing Linux and Unix Software From Source if you need help with installing from source.)

You will need ssh keys if you want to upload code to repositories that use ssh authentication, such as Git Hub. If you do not already have an rsh public key, create one using ssh-keygen. (This command is available in the Git Bash shell that is part of msysGit and is installed by default on OS X and most Linux and Unix systems.) You can just pick all the default options. Remember where ssh-keygen puts your public key (id_rsa.pub) so that you can retrieve it when you need it. (Most likely the .ssh directory in your home directory.)

Next, you should configure your git client so that it knows who you are (your name and email address.)

(Note: If you use more than one email address or multiple nom de codes that you want to use, it is possible to set these to different values for specific projects.)

Next, create a local directory to hold your code. On Unix-like operating systems /usr/local/src or $HOME/src are conventional. For Windows, I recommend c:\code. But you can use anything you like.

Move to your code directory. Now you can create your first git repository named "test".

git init test

This creates a directory named "test" containing only the files necessary to manage git.

You can add files to this directory using your favorite text editor, development environment, or the file manager. For the purpose of this tutorial, create a file called "README" that explains what your project is about. Next you need to tell git about the file with:

git add README

(Note: you'll need to be in the test directory for this to work.)

Now you can look at your repository using git status and see the file:

At this point, git knows about the file, but it isn't officially part of the repository. To officially add the file to git, you need to use:

git commit -C 'Initial Version' README

The string after the -C is a comment that describes what the code is for or what changes have been made. You can review changes later by looking at these comments. Now if you look at git status it will show no changes to be committed. Now that your code (or in this case README file) is committed, you are free to take advantage of all the features of a version control system – edit files and revert them, share your archive, etc.

Buy Ben Lynn's book Git Magic from Amazon.com or download a copy using git clone git://repo.or.cz/gitmagic.git

Buy Travis Swicegood 's book Pragmatic Version Control Using Git (Pragmatic Starter Kit) from Amazon.comor check out a preview here