Learning git: Part 1

I was learning git for couple of months now but I am still couldn’t master the art of using git, Although I was using sub version for years. There are lots of difference between git and other subversions namely tortoise svn. I am a big fan of version controlling and obviously my first choice and I am still using tortoise svn but git attracted me by its powerful system of version controlling. I will be documenting what I learned for past few months and the basic art of using git. I hope you will have fun using git. I won’t be documenting how to install git either in windows or in *nix but I will be attaching some link where they stated beautifully how to install git in your operating system. Basically git is a command line system of version control so before you jump into this you need to know at least the basic of command line and terminal. If you are using windows like me and wanted to give git a chance then head over here to get the information how to install msysgit what is basically the tools you need for using git in windows. This project is hosted in google and basically running and improving by some awesome volunteer so if you want to help msysgit just be a volunteer.

Lets move to the main part. Assuming you have installed msysgit on your windows we will learn in this part of learning git, how to configure our git repositories, how to commit, add and see the log and status. If we can finish learning quickly then we will be learning branching in our second part of learning git. You have to configure git after you install msysgit on windows so that it will know who you are.
Configuring Git:Open up the terminal what came with msysgit not our native terminal of our windows. I think you shouldn’t be closing this terminal while you will be working on git because as I said git is basically the command line system. After you open your terminal execute those lines:

$ git config --global user.name “Maz Anwar”
$ git config --global user.email “maz@protishobdo.com”

Obviously you have to change name and email address in the double quoted area where now I have putted mine. Well, congrats! you have configured your git, now we will learn using git.
Using Git: First step of using git is setting up your git repository. Create a repository where you want to keep your files, move in to that folder in your terminal. Assuming you know how to change directory in your terminal, in case you don’t know how to do that use `CD`, which means change directory, and the directory that you just created for your project. Lets assume you have created a folder called test in your (D) drive and you are in root in your terminal so you probably want to use a code similar to this:

cd D:/test

Once we are in the repository of our project then its incredibly simple to initialize git for that repository. execute this code by staying in the repository you have created for your project:

git init

Once you execute this command your response in the terminal would be like this except the file path and folder name:

$ git init
Initialized empty Git repository in D:/test/.git/

Now we will be learning about git status, how it works and why you need it. This is very important and useful command both for beginners and proficient. Let’s say we have three files in our project folder.
1.default.css
2.index.html
3.readme.txt
Now if we run `git status` command in the terminal we will be getting the status of our folder and files. Result in the terminal will be looking like this(kind of):

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use “git add <file>...” to include in what will be
committed)
#
# readme.txt
# default.css
# index.html
nothing added to commit but untracked files present (use
“git add” to track)

The first line tells you that you are on the master branch.The next line—initial commit—is a reminder that we have not committed anything to our new git repository. Next we have a list of untracked files. Note that all three of the files we have in our directory are correctly listed as untracked.

Git Add: we will be using this command to add the file in version control system, to add any file which will be ‘trackable’ by git execute the `git add` command and the file name, like this:

$ git add index.html default.css

After adding the file you should check back the status of your repository by executing `git status` to see whether the file that are added on the git is tracked or not. If we see something like this then our adding is wokring and we will be moving on to next step:

$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use “git rm --cached <file>...” to unstage)
#
# new file: default.css
# new file: index.html
#
# Untracked files:
# (use “git add <file>...” to include in what will be

Git Commit: The primary purpose of source code management systems is to keep a snapshot record of your project as it grows up. the simplest way to commit is executing this line in the terminal.

git commit

This will take you to your command line’s default editor and will be asking you to put a short message which is very important to keep track of the committed reason and files and what was committed. It will be working as a change-log as well. After entering your message save the file and exit from the editor. To exit from the editor first press `Esc` key on your keyboard and then `:exit`, should be taking you to the commit confirmation code in your terminal and will close the editor inside the terminal. Once you close the editor you should see these kind of confirmation lines in your terminal:

[master (root-commit) 3435356] initial commit
3 files changed, 10 insertions(+), 0 deletions(-)
create mode 100644 README
create mode 100644 default.css
create mode 100644 index.html

Git Log:This command is very handy to see the log of a committing repository. This is very handy to keep track of users and the project as well. To see the log execute this command:

git log

After executing the command result in my terminal was:

Thanks for reading the post and hope you found interest in git, all the best in your gitting(I invented the word? :P )

About Maz

A PHP and javascript nerd believe in web applications and open source goodness. Ex MS Windows user converted to linux fanboy and love gadgets. Coding in python and maintaining servers became his passion. Website performance and optimization geek.
This entry was posted in Programming, git101 and tagged , , . Bookmark the permalink.
  • http://topsy.com/protishobdo.com/blog/learning-git-part-1/?utm_source=pingback&utm_campaign=L2 Tweets that mention Learning git: Part 1 | Dare to Dream — Topsy.com

    [...] This post was mentioned on Twitter by Mazharul Anwar, Mazharul Anwar. Mazharul Anwar said: Learning git: Part 1 http://goo.gl/fb/M5LkS #programming [...]

  • http://codebix.com/posts/post/159392/Learning-git-Part-1 Learning git: Part 1

    [...] lots of difference between git and other subversions namely tortoise … Continue reading… [full post] Maz Dare to Dream programminggit101gitsubversion 0 0 0 0 [...]