You Are Here Home > Beginning Source Code Management With GIT Tutorial Part 1

Beginning Source Code Management With GIT Tutorial Part 1

If you are not using anything like GIT and you just use simple folders for development then I’m sure you have had situations where you needed to make some changes but broke the whole thing.

Or consider the situation where you had a huge idea and it required rewrite of a lot of things OR some dramatic changes to your source code files so you went half way through and (unless you had a copy of the whole code) you didn’t have a way to go back anymore.

Or when you wanted to backup your codebase, did you have to zip it and copy it to some external hard disk or server? That’s a pain.

Or when you wanted to develop something with your friend and you know what kind of a mess that is, right?

If these sound familiar then GIT is designed just to help you do these tasks faster, nicer and more reliable. It will also take care of a lot of things for you.

First let’s install git, on Windows (which is my platform for now) you will need to install this:
http://code.google.com/p/msysgit/
NOTE: you will have some options during installation, choose these options: 1 – GIT should use the regular Windows Command line 2 – GIT should add itself to Windows path 3 – GIT should NOT change the line endings. You will see these options during installation…

On MAC try:
http://help.github.com/mac-git-installation/

On Linux try: (note: you might already have it, got to Terminal and type “git” and hit “Enter”)
yum install git-core

After you installed GIT open your Terminal or Command Line for Windows and type these commands:

> git config --global user.name "YOUR NAME"
> git config --global user.email "YOUR EMAIL"

For Windows:

> git config –global core.editor “notepad.exe”

It’s obvioud what these do so now let’s try GIT, make a folder somewhere and paste one of your projects in there let’s assume that the folder is at C:\git_test\ then do:

> cd C:\git_test

On Linux or Mac this would look like this:

> cd /git_test

Now let’s initialize our GIT repository:

> git init

After this command, GIT will create a .git folder inside git_test folder, the cool thing is that GIT won’t modify anything, GIT won’t keep the files in some other location, it just tracks the files inside your folder as you edit them normally, in other words, GIT won’t get in your way at all!

Then try:

> git status

This will show you all the files in this folder but it will also tell you that these files are not tracked by GIT yet so to let GIT track them try:

> git add .

You could add single files like:

> git add index.html

But the “.” will add everything so that’s what I always do.

Now we need to commit everything for the first time, so try:

> git commit -a -m "Initial commit"

So now go ahead and edit one of the files and change it a bit then go back to your command line and type:

> git status

As you can see GIT knows what file you edited, it doesn’t end there try:

> git diff

It even knows what you did exactly, so now you can commit the new changes:

> git commit -a -m "Test edit"

Note that you are entering a message with each commit, it’s best if you explain in a short sentence what you did…

You can see a history of your commits with:

> git log

You may notice that with each record there is a sha1 hash string, that is the hash of the content and it is useful for a bunch of things you can do with GIT.

Assume that the last edit you just did, broke something and you want to go back to the previous stable state, to do this you must know the sha1 hash of the commit that you want to revert to, for this example let’s revert to “Initial commit” state (our first commit), to do this type:

> git log

Then copy the sha1 hash that is associated to the “Initial commit” commit and type:

> git revert PASTE_THE_SHA1_HASH_HERE

Now go back and look for your changes, they are not there anymore!

Also you must be careful, GIT never asks questions like “are you sure you want to revert?” etc. it just does it so pay extra attention when doing things like this…

Now let me show you one last thing in this tutorial and that is the concept of branches, type:

> git branch

This will show you something like:

*master

The branch master was created automatically when you created this repo and the * means that this branch is active.

Let’s say that you have a crazy idea and that requires changing the code quite a bit but you don’t want to mess around with the code that is already stable and working, this is were branching comes into play and GIT does it better than any other system, it does it in a very simple and fast way.

Let’s create a branch and call it experiment:

> git branch experiment

This single line just created an entirely identical copy of the whole code – folders and files – in GIT’s database for you, that was fast right?

Now let’s switch to this new branch:

> git checkout experiment

If you try:

> git branch

You will see something like:

*experiment
master

Confirming that you are now in the branch “experiment”.

So the cool part is this, try editing some of the files, maybe even remove some files and add some other files to the folder git_test to test how all of this works then try:

> git add .

This will add any new files you just pasted in your git_test folder for tracking and finally try:

> git commit -a -m "Committing in experiment to test branches"

Here is the magic, try:

> git checkout master

Go back to see your new edits and check for the files you removed or added; all the changes are gone! Your folder is now in it’s initial/stable state!

To remove a branch try:

> git branch -d "experiment"

Again remember, GIT won’t ask you “are you sure?” or questions like that so be careful when removing stuff…

I should also mention that GIT was written by Linus Torvalds who also made Linux Kernel…

That’s it for now, I will write more about this great tool soon and I hope this helps someone.

Beginning Source Code Management With GIT Tutorial Part 1
Filed under: GIT, General, Programming   Posted by: Codehead

Got a Question?

Get answers here.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment