Welcome to the Part 1 of “Learn GIT series”.

Purpose: If you are a power Linux user then chances are that you must have heard about GIT – the distributed source code management software written by Linus Torvalds himself to ease Linux kernel development. However, GIT has become so popular these days that many big projects like GNOME, Ruby on Rails, etc. have started using GIT. One of friend in Expedia told me that they have started using GIT too in their new projects. I myself have decided to start learning/using GIT. So why not join with me in learning GIT? I will regularly write post about my GIT learnings until I get a sufficient hold on it.

Assumptions: Although these steps should be very similar for any Linux distribution, I will just assume Debian Lenny 5.0 (x86) as my Linux distro. So let’s get started…

Step 1: Install GIT

First let’s install the core package of GIT and then we will see how to more and more other GIT packages:

apt-get update
apt-get install git-core

Note: There is also another package called just “git”. We DON’T need this package. So do not do apt-get install git.

Step 2: Configure your name and email

For every commit that we make while using git, a name and email address needs to be specified so that the person who committed the code change can be identified. So our first job after installing git is to configure the name and email address by giving the following command:

kushalk@debian-tablet:~$ git config --global user.name "Kushal Koolwal"
kushalk@debian-tablet:~$ git config --global user.email kushalk@koolwal.net

You can verify whether the name and email address if registered or not by giving the following command:
kushalk@debian-tablet:~$ git config -l
user.name=Kushal Koolwal
user.email=kushalk@koolwal.net

Step 3: Prepare a project directory

Finally the time has come to create your first GIT repository. Now this can either me an existing project directory or a newly created directory. For simplicity I will create a new project directory as follow:

kushalk@debian-tablet:~$ mkdir -p projects/hello

So the directory “hello” is going to be our actually GIT repository in which our project’s code file is stored.

kushalk@debian-tablet:~$ cd projects/hello

Now let’s write a simple “Hello Word” C program.

kushalk@debian-tablet:~$ nano hello_word.c

Code:
#include <stdio.h>
int main (void)
{
printf ("Hello World!\n");
}

Now so far we haven’t made our directory “hello” as a GIT repository. So till now we it just like any other normal directory which has a simple C program in it.

Step 4: Create your first GIT repository


Now simply give the following command to initialize the “hello” directory as a GIT repository:

kushalk@debian-tablet:~/projects/hello$ git init

Output:

Initialized empty Git repository in /home/kushalk/projects/hello/.git/

The above message means that your GIT repository is initialize successfully and you are ready to take full advantage of all the features that GIT has to offer. Notice that the above command created “.git” directory inside the hello directory in which it stores all the META information about your project.

kushalk@debian-tablet:~/projects/hello$ ls -al .git/

Output:
total 40
drwxr-xr-x 7 kushalk kushalk 4096 2009-08-07 01:46 .
drwxr-xr-x 3 kushalk kushalk 4096 2009-08-07 01:48 ..o
drwxr-xr-x 2 kushalk kushalk 4096 2009-08-07 01:46 branches
-rw-r--r-- 1 kushalk kushalk   92 2009-08-07 01:46 config
-rw-r--r-- 1 kushalk kushalk   73 2009-08-07 01:46 description
-rw-r--r-- 1 kushalk kushalk   23 2009-08-07 01:46 HEAD
drwxr-xr-x 2 kushalk kushalk 4096 2009-08-07 01:46 hooks
drwxr-xr-x 2 kushalk kushalk 4096 2009-08-07 01:46 info
drwxr-xr-x 4 kushalk kushalk 4096 2009-08-07 01:46 objects
drwxr-xr-x 4 kushalk kushalk 4096 2009-08-07 01:46 refs
kushalk@debian-tablet:~/projects/hello$

For now don’t worry about every file that is present in there. As we learn more and learn in this series we probably will understand what is the specific purpose of each of these files.

For now just give a meaningful description to your project:

kushalk@debian-tablet:~/projects/hello$ nano .git/description

and add the following line in it:

My first GIT project - Hello World

after deleting the default line:

Unnamed repository; edit this file 'description' to name the repository.

Quit and Save the file.

So far you have successfully created a basic GIT repository for your “hello” project.

Step 5: Add file(s) to your project

Now by default GIT does not recognize the file “hello_world.c” (created in Step 3) to be part of GIT yet. We need to explicitly add the file to tell GIT to start tracking it:

kushalk@debian-tablet:~/projects/hello$ git add hello_world.c

Now if you give the command:

git status

Output:

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#    new file:   hello_world.c
#

it basically tells you that a new file “hello_world.c” has been added but not yet committed.

Step 6: Make your first commit

So let’s go ahead and make our first commit:

kushalk@debian-tablet:~/projects/hello$ git commit -a -m "Initial Commit"

Output:
[master (root-commit) 7e9459a] Initial Commit
1 files changed, 8 insertions(+), 0 deletions(-)
create mode 100644 hello_world.c

The above output confirms that your commit has been successfully. The option “-m” stands for a brief message that you would like to attach to your commit. Note that you have to give some sort of message for every commit you make. Do NOT try to skip the message part. Moreover, having meaningful one-line message for commit is always useful as other and you yourself can refer to later on to see what changes you made and why.

Congratulations on doing your first commit!

Now if you give the command:

kushalk@debian-tablet:~/projects/hello$ git status

Output:
# On branch master
nothing to commit (working directory clean)

it will tell you that there is nothing to commit more which probably means that you are up-to-date.

Step 7: Check commit logs

Also you can see your commit logs (history) by giving the following command:

kushalk@debian-tablet:~/projects/hello$ git log

Output:
commit 7e9459a2cb40469fe23116ef025354b45b3a45ce
Author: Kushal Koolwal <kushalk@koolwal.net>
Date:   Fri Aug 7 01:57:10 2009 -0700
Initial Commit

Step 8: Add one more file and commit

Let’s see how you can add one more file to your project and commit it.

kushalk@debian-tablet:~/projects/hello$ nano library.h

Code:
#ifndef DEFINITIONS_H
#define DEFINITIONS_H 1
/* Implement a number using a linked list.  */
struct LinkedListNumber
{
struct LinkedListNumber*
one_less_;
};
#endif /* DEFINITIONS_H */

Save and quit the file.

Now we edit our hello_world.c file to include the library.h file too:

Code:
#include <stdio.h>
#include "library.h"
int main (void)
{
printf ("Hello World!\n");
}

Save and quit the file.

Now if you give the command:

kushalk@debian-tablet:~/projects/hello$ git status

Output:
# 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:   hello_world.c
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    library.h
no changes added to commit (use "git add" and/or "git commit -a")

It basically tells you that which files have been changed since they were added (hello_world.c) and which new files are being created which are not added yet (library.h)

So let’s go ahead and add the “library.h” file:

kushalk@debian-tablet:~/projects/hello$ git add library.h

And now we make our commit:

kushalk@debian-tablet:~/projects/hello$ git commit -a -m "library.h file added"

Output:
[master d0e0654] library.h file added
2 files changed, 14 insertions(+), 3 deletions(-)
create mode 100644 library.h

Now you can check the status of your repo and log of your commits:

kushalk@debian-tablet:~/projects/hello$ git status

Output:
# On branch master
nothing to commit (working directory clean)

and
kushalk@debian-tablet:~/projects/hello$ git log

Output:
commit d0e0654468e2e9f343369cda40e7760fc94ded9e
Author: Kushal Koolwal <kushalk@koolwal.net>
Date:   Fri Aug 7 02:17:37 2009 -0700
library.h file added
commit 7e9459a2cb40469fe23116ef025354b45b3a45ce
Author: Kushal Koolwal <kushalk@koolwal.net>
Date:   Fri Aug 7 01:57:10 2009 -0700
Initial Commit

Congratulations once again if you have made it so far. You have just created your first GIT repository and have made two commits just like the PROS.

Note that if you feel intimidating right now please don’t worry. It is pretty normal. As we progress through our series you will start feeling more comfortable with GIT and the commands. Trust me it took me a while just to understand what we learned in this post.

Happy GIT’ing

Part 2: Install GITWEB to host repository on Apache web-server

Be Sociable, Share!