Last Updated: 08/12/2010 07:46:00 PM

The GIT Repository: Attack of the code clones.

The GIT Repository: Attack of the code clones.

You are probably familar with the term "repository."  Webster's Dictonary defines it like this: "a place, room, or container where something is deposited or stored."  Mr Webster sums it up nicely. That is one of the things a code repository does, it stores your files and code.  But GIT Repositories are more than a dumb container. 

Git thinks of the information it has been entrusted with like a series of snapshots of a mini filesystem.  Every time you commit, or save the state of your project Git takes a picture of what ALL your files look like at that point in time.  Git then makes a label for that snapshot (stores a reference)  To be efficient if something has not changed, it doesn't store it again, it just stores a pointer to that identical content.

This is a big difference from how CVS and Subversion think about their data.  They think of their data more like a list of file based changes.  Kind of like a to do list, start with a file that looks like this, then do that, then change this, then this, and on and on.

This means that GIT knows the entire state of your project at any point in time. It is like a time machine for your code!  You sir, can be a Time Lord!


Ok enough gabbing! Lets work with a repository.  We have two main approaches to getting a Git repository.

  1. Clone an existing GIT repository hosted somewhere.
  2. Create an empty GIT repository and put and existing project into it.


Let's do the first one, because Cloning sounds really cool!

 Cloning Sean Corfield's FW/1

As of the time of writing this Sean Corfield's Framework 1 (FW/1) seems to be the most popular ColdFusion project currently hosted on  GitHub if you don't know is an online service that hosts your git repository for you so you can make it available for collaboration.  You can certainly host it yourself (that is another article), but GitHub supplies everything you need very quickly and easily.  We are going to clone Sean Corfield.  Well his FW/1 code actually.  Cloning Sean himself would be cool, but that is way outside the scope of this article.

Get to a GIT Bash.  In Windows I go to Start, Git, Git Bash and it opens in a command line looking window. (Click here if you need steps on installing GIT)

Now we need the http address of FW1, which can be found be searching github.  For me this page returned:

Copy the http url from this page, yes the url with the big orange arrow.  Now return your attention to the GIT bash window.  When we actually run the clone command, it is going to put the repository in the directory in which git bash currently thinks it is.  Depending on a whole host of variables the folder you start in when Git Bash first starts up could be the users folder, the root drive, or system32 like mine.  We need to traverse to the directory we really want the files in.  Use the cd command to do this (cd = change directory) . 

cd .. (note there is a space between cd and the ..) will take you up to the parent directory.

cd d: will take you to a the root of the d drive

cd foldername will take you to named folder

cd c:/project will take you directly to the project folder. (Note I used the SLASH as opposed to \. Both actually work, but if you use the \ Ray Camden may come and whip you with a light saber.)

Now that I am in my project folder, I am ready to clone me some framework 1!

Type: git clone

If you still have the URL in your clipboard, you could paste it by right clicking the top edge of the GIT Bash window, choose edit, choose paste, or type it, whichever works for you.

Now hit Enter you should see something like this:

So what just happened?

Git created a folder with the same name as the .git file you pointed to over the internet and created an empty repository in the fw1 folder.  Then on the remote server it gathered all the items in its repository, files, history everything.  GIT then compressed it and sent it to you.    Your computer then unpacked it and recreated and exact clone of the fw1 project in your fw1 folder.

You can cd fw1 into that folder and then ls (list) the files in the directory (or do it from your OS native file browser: the files really are there!)

Voila! That was pretty easy. Now lets make a repository and put an existing project into it.

Creating a repository from scratch

Lets take the code from a domain name I have no clue what to do with the code resides in c:\project\maythe4thbewithyou so in GIT  bash traverse your way to that directory and type:

git init

Pretty uneventful? All GIT has done has created a hidden .git folder with all the magical stuff it needs in it.   We need to tell GIT about our files. Type:

git add *.* (this adds files to GIT's stage area)

git commit -m "Initial Project Version" (this is committing the files to the repository and marking it with a note about being the Initial Version)

So now we have a git repository.  Work we do in the Maythe4thbeyou folder can now be tracked.  Note: editing and saving files in this folder is not going to automatically going to do anything to repository.  Saving files is a function of the OS.  We have tell GIT WHEN to take a snapshot and commit the files, more on that in the next article.

At this point, you know how to create a repository from scratch and put an existing project into it.  You also know how to clone an existing repository that is on the internet. Later we will need to learn how to share your repository with your colleagues on the same network and also across the network.