A Path Less Taken

Breaking with convention in a very conventional fashion. Powered by WordPress

"What would you attempt to do if you knew you could not fail?"
Dr. Robert Schuller

Saturday, January 30, 2010

Category: Software Tags: Author: JJ 4 Comments

If you are an open source developer and you have not been living under a rock for years (yes, I know, many have) then you have probably heard of git. Billing itself as “the fast version control system“, git is the distributed version control management system that has become the darling of the hour. It allows developers to work on-line and off-line, to host repositories locally or on the now popular github, to perform merges as the rule rather than the exception. In short, git is to version control what WordPress is to blogging. The eight hundred pound gorilla in a small room.

The problem is, I’m a windows user. Microsoft sells my drug of choice at the conner electronics store. Command line tools just are not my style. Be that as it may, I decided it was high time I caught up with the hip open source crowd and learned how to drive this thing commando (Linux) style. That plus there is the code block on github that I want to fork, but let’s not get bogged down in details!

What follows is a series of simple commands wrapped in a monkey stupid (yes, I said monkey stupid) how-to guide for git using the Git Bash on Windows Vista. I’m not going to tell you how to install it on Windows. You can Google that for yourself. I’m going to tell you the (mostly) absolute minimum needed to get up and running with git version control. If you are nice and follow along to the end, I may even share some web resources where I obtained this top secret information. No promises mind you. We will just have to see how well you behave along the way. Remember, there will be a test at the end. (There actually won’t be, but when the monkeys hear test it gets their attention!)

Navigation

Let’s begin by launching the Git Bash program. You know, Start, All Programs, Git, Git Bash. Good monkey! Now what you see is the MINGW32:~ header on what looks like a slightly off beat DOS command prompt. Sigh. The directory that Git Bash opens in is the c:\Users\your_user_name directory. Since most monkeys don’t put source code there we need to navigate to the directory of the source that we want to version. You can use the cd command (change directory for you old school DOS nerds) to navigate around your computer to find the directory where you source lives. Let’s try some commands and see what we get.

  • It’s not navigation really, but to clear the Git Bash DOS prompt window we type:
    1
    
    clear
  • Next, we can navigate using the cd command to change directory. Here we move up to the parent directory of the current directory.
    2
    
    cd ..

    Afterwards the prompt should show the new directory, in my case that is:

    3
    
    /c/Users

    In Windows speak this is the equivalent of c:\User\.

  • For our example let’s assume that my source is on my e: drive and I want to navigate directly there.
    4
    
    cd /e/lighttpd/htdocs/test

    This brings me to e:\Lighttpd\htdocs\test in my Windows directory structure.

  • To find out what is in my current directory I simply type the ls command.
    5
    
    ls

    At the moment my folder is empty so nothing is returned. Let’s throw some files in and start to version our source.

Basic git

To start I create an index.php file with the this content.

1
2
3
4
<?php
/*My Awesome Program! */
phpinfo ();
?>

Yes, it’s amazing. I also create a test.css file, but you can put anything you want in that. Now when we run our ls command again we see the two files we created. Since git is in the directory that we want to control we can start managing our source with a few simple commands.

  • To start tracking at the current project level we type
    1
    
    git init
  • To add both our PHP and CSS files and commit them initially we type
    git add *.php
    git add *.css
    git commit -m "the monkey flips the switch"

Once you have done this you can run the ls command again and you will see that Git Bash appends the “(master)” description to the end of the directory path. This is a fancy way of git saying that it just created a .git directory right in the middle of your project directory and stored your commit there. But we are monkey stupid, right? We did not want to do that. It’s cool. We can just blow it away. For the real monkeys out there you may want to use Windows Explorer to do this. For stupid monkeys (like me) you can use the following. WARNING: MAKE SURE YOU ARE IN THE RIGHT DIRECTORY, MAKE SURE YOU BACKED UP YOUR PROJECT, ETC!

rm -rf .git

Afterwards you can just use git init again to start over and rebuild your git repository. If you did try the delete (bad monkey!) then go back and re-init, populate and commit your source. Afterwards you can run the following command to see what git thinks of your repository.

1
git status

If you added all file types in your project folder using the git add command and if you ran your commit then you should see something like this.

1
2
#On branch master
nothing to commit (working directory clean)

You have saved the day (and some source code too)! Now let’s get to programming. Go change your index.php file to contain the following.

1
2
3
4
<?php
/*My Awesome Program! */
print "My even more awesome program!" ;
?>

Now when we run git status again we see something like this.

On branch master
#Changed but not updated:
#  (use "git add <file>..." to update what will be committed)
#  (use "git checkout -- <file>..." to discard changes in working directory)
#
#        modified:      index.php
#
on changes added to commit (use "git add" and/or "git commit -a")

What happened!?! Work happened my monkey friend, work happened. Now that you are off creating the next great application git is waiting patiently in the background to save your source code or recover it for you. Let’s commit our changes since they are clearly better than what we had before! You already know the commands that you need.

1
2
git add index.php
git commit -m "now with really awesome monkey output"

Solid. You are now a certified (not really) monkey stupid git user. Sweet! git will actually do a lot of other cool and powerful things like cloning, merges, etc, but that is beyond the scope of a monkey stupid guide. When you are ready to move on to more sophisticated fair you should absolutely check out Pro Git and buy a paper copy of the book to support the site. Good luck and have fun using git my monkeys!

4 Comments to “Getting Git”

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">