ATTENTION ATTENTION
gear.huuah.com has launched. Visit the shop at http://gear.huuah.com/. Lots of Photo Gear at the moment
ATTENTION ATTENTION
Itsplanned.com is just launched! - Task and project management made easy. Try it for free.

Create your own lists of things to do - arrange the order to do them - move them around - group them

No limitations - all free project management - try the free demo before signing up - demo: itsplanned.com
How to use Git version control.
16.05.2010

How do you keep track of your code changes? What do you do, if you want to go back to how your code was yesterday? What if you by mistake has deleted a file from your code. Well, I though it would be a great time to get some control over my project and the code behind them.

I asked around a bit and everyone said I should take a look at ‘git’, so that I did and I will here summarize what I found so far.

What is Git?

The git version control is a decentralized system that runs along side your code projects. This means that your are not depended on a shared/central server to manage your requests. This have many advantages compared to say cvs and subversion (svn), but I will not go into details with this.

Basic file handling

I have writing a small Hello World sample i php, which looks like this:

<?
  print "Hello world!";
?>

Nothing fancy – just a starting point.

Initializing a project

To start up a new project, you have to use the init-option. Standing in the root folder of your project run this:

$ git init
Initialized empty Git repository in /home/js/helloworld/.git/

This creates a .git-folder in the root of the project folder and your project is ready to go.

Adding files to the git repo

To add the newly created helloworld.php file to the git repository, you have to use the add option like this:

$ git add helloworld.php

The helloworld.php is now queued for adding to the project.

Committing changes

When files are marked to be added to the project, you have to commit the changes to the repo. This is done with the commit option

$ git commit

This opens up your default editor, where you can type in a comment for your update. When this is done and the content is saved, just quit your editor and the file should now be committed.

$ git commit
[master (root-commit) 22aff8e] Initial commit
 1 files changed, 3 insertions(+), 0 deletions(-)
 create mode 100644 helloworld.php

Showing a log/history of the project

To see what has happen to the project, you can always run the log option like this:

$ git log
commit 22aff8e5f227c6284307363d036fbe06806ad4a5
Author: js <js@w(none)>
Date:   Sun May 16 22:54:13 2010 +0200

    Initial commit

Here we can see, that I have submitted a change, which I have commented “Initial commit”.

Updating existing files

So lets make some changes to the helloworld.php file:

<?
  print "Hello world... Hello world!";
?>

I have here added some text to the print command.

When using the diff option we can see which changed has been entered:

$ git diff
diff --git a/helloworld.php b/helloworld.php
index 4450c89..50f9c46 100644
--- a/helloworld.php
+++ b/helloworld.php
@@ -1,3 +1,3 @@
 <?
-  print "Hello world";
+  print "Hello world... Hello world!";
 ?>

Here we can see, that there is a difference in the helloworld.php file.

To save these changes in the repo, just commit them like shown above:

$ git commit -a
[master d6bcc84] Added more text
 1 files changed, 1 insertions(+), 1 deletions(-)

Reverting back to previous versions

Doh!, this is all wrong. It should only print out “Hello world” and nothing else. Well, then we just have to fetch the previous version of the helloworld.php file.

Lets use the log function to see which change we have to get back to:

$ git log
commit d6bcc84950f5968e8c5d3fc0522e55cfb3cc8dab
Author: js <js@w(none)>
Date:   Sun May 16 23:04:17 2010 +0200

    Added more text

commit 22aff8e5f227c6284307363d036fbe06806ad4a5
Author: js <js@w(none)>
Date:   Sun May 16 22:54:13 2010 +0200

    Initial commit

We then want to go back to the initial commit by using the hash value stated on the commit line:

$ git checkout 22aff8e5f227c6284307363d036fbe06806ad4a5
Note: moving to '22aff8e5f227c6284307363d036fbe06806ad4a5' which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
  git checkout -b <new_branch_name>
HEAD is now at 22aff8e... Initial commit

Listing the content of the helloworld.php will now verify that we have gone back to the original version.

But as you can see, we now have to work with branches. I will not cover branches in this article, so just save a branch and move it to the master like this:

$ git checkout -b test
Switched to a new branch 'test'
$ git branch -M master

There you go – a previous update was found and we can continue our coding from here.

I will cover branches and much more later on.

Leave a Reply