Since moving off Windows as my development platform, git has probably been the greatest treat. A great, lightweight, self-contained and very well thought-out piece of software, it’s one of those rare tools that once you’ve used it for a while, you can’t image how you worked without it.
A lot of great ruby tools are using or moving to github, a platform which I’m only just starting to get my head around, but looks really exciting for the development of open-source software.
I suspect windows users probably feel a little excluded from git so in order to spread the love, I thought I’d have a go at installing it on a Windows machine while you just sit back and watch. Wanna play?
I run the .EXE and see the installer start up. It asks me the usual stuff about installation location, start menu shortcuts… Then I see this:
I don’t use any fancy command-line tools on Windows really, so I don’t mind about this stuff. I could also quite happily install it on cygwin (see http://lifehacker.com/tag/cygwin/ for a great usage guide) but not today: I’m going to keep it nice and simple.
Next we see this odd poser:
Second option sounds a bit complicated. First one sounds warm and fuzzy.
And we’re off. Git goodness is pouring out onto my Windows hard-drive.
How about a nice cup of tea before we get into the fun stuff?
Basic Version Control with Git
Start by opening a command prompt. You’d better get used to this – there is a GUI but you’re definitely best off learning the tool from the prompt. Trust me, after using the command line for a few weeks, you won’t want it any other way.
git and hit return. You should see the standard help text with the major commands listed. That means git is installed and we can move on.
Create a directory for playing around (I just made on in c:\temp) and cd into it:
C:\Documents and Settings\Matt>cd C:\ C:\>mkdir temp C:\>cd temp C:\temp>
Now tell git to create a new source control repository in the current folder:
C:\temp>git init Initialized empty Git repository in C:/temp/.git/ C:\temp>
So what git does is puts all the files it needs to run a complete version control system in that .git folder. Yep, the whole thing. No databases to set up, no web servers to run (unless you want to). By the way – the dot-prefixed name signifies, in unix-land, that this is a hidden folder. You’ll just have to learn to ignore it.
Let’s get an idea of what git can do by creating a text file and editing changes to it.
Type something in and save the file.
Now we’ll add the file to git. First of all let’s get git to wake up and take a look at what’s been going on:
C:\temp>git status # On branch master # # Initial commit # # Untracked files: #Â Â (use "git add <file>..." to include in what will be committed) # #Â Â Â Â Â Â hello.txt nothing added to commit but untracked files present (use "git add" to track)
So git knows that the file has been added to the folder, but also that it’s not currently under version control. We can add it (but not yet check in the change) like this:
C:\temp>git add .
That will add any untracked file in the current directory and below to source control.
Checking git status again, we’ll see that the file is now ready to be committed.
You have to enter a commit message, but instead of using a geeky unix command-line editor to type in your message, just use the -m flag to enter the message at the command line:
C:\temp>git commit -m "wrote someting really clever in the file"
Change the file once more using notepaf, and commit it again, saving a different commit message. Play around. Go nuts.
Now we’re going to see some of the really cool stuff git can do. Because it’s so incredibly cheap to set up a repository, and because git is built to be able to syncronise these repositories, you can set up another one, and push changes between them.
C:\temp>cd .. C:\>mkdir temp2 C:>cd temp2 C:\temp2>git clone C:\temp\ temp Initialize temp/.git Initialized empty Git repository in C:/temp2/temp/.git/ C:\temp2>dir Volume in drive C has no label. Volume Serial Number is EC69-87C6 Directory of C:\temp2 13/09/2008Â 01:11Â Â Â <DIR>Â Â Â Â Â Â Â Â Â . 13/09/2008Â 01:11Â Â Â <DIR>Â Â Â Â Â Â Â Â Â .. 13/09/2008Â 01:11Â Â Â <DIR>Â Â Â Â Â Â Â Â Â temp 0 File(s)Â Â Â Â Â Â Â Â Â Â Â Â Â 0 bytes 3 Dir(s)Â Â 4,084,494,336 bytes free
And there we have it – a freshly cloned copy of the whole repository (history and all) which we created earlier. Go on in and take a look around:
C:\temp2>cd temp C:\temp2\temp>dir Volume in drive C has no label. Volume Serial Number is EC69-87C6 Directory of C:\temp2\temp 13/09/2008Â 01:11Â Â Â <DIR>Â Â Â Â Â Â Â Â Â . 13/09/2008Â 01:11Â Â Â <DIR>Â Â Â Â Â Â Â Â Â .. 13/09/2008Â 01:11Â Â Â <DIR>Â Â Â Â Â Â Â Â Â .git 13/09/2008Â 01:11Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 11 hello.txt 1 File(s)Â Â Â Â Â Â Â Â Â Â Â Â 11 bytes 3 Dir(s)Â Â 4,084,494,336 bytes free
There’s a familiar face! It’s our old friend the hello.txt.
Try changing the file again, and checking in your changes. You’ll find you are quite safe to make changes and commit them to this new repository, stored at C:\temp2\temp
Now call git push. You should see this:
C:\temp2\temp>git push Counting objects: 5, done. Writing objects: 100% (3/3), 254 bytes, done. Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. To C:\temp\/.git 2e7c420..8727e8cÂ master -> master
Now cd back to your original C:\temp directory, and see what’s happened to the hello.txt file. Did it change?
Please let me know what you think of this article. I’m having a lot of fun with git, but I appreciate that this stuff can feel intimidating to those on the outside, so I hope I’ve given you enough of a taste to want to learn more.