About Git

Git is not inherently a traditional source code management system. Git can be rather said as a “Personal” source control system. By “personal” it means it doesn’t require any server to store the code, can maintain the revision records on the personal computer only.

Unlike in SVN, where all the changes are stored in a remote central server, in Git there is no concept of server. In git all the code or data is stored at all the user ends, each end user will have complete copy of the repository along with the history of changes.

The next question which arises with this is whether it leads to unnecessary occupation of memory, but it is not so. Git stores not the complete copies of all the files during each commit, but stores just a index of changes in the files. By index it means just the changed or added code in the files.
Suppose there are three files a,b and c. Then in first commit all the files will be stored. But in next commit if just file a is changed, then just the added or removed or changed lines of that particular file are stored for that commit.

There are three kinds of classification for files done by git.

1)Untracked files: These are not known to the git repository.
2)Tracked files: These files have one copy with the repository, but it is changed and those changes are not matching with repository.
3)Committed files: These files are same as that in repository.

Before looking into those, will cover few basic git commands
First will create a local repository, will begin with terminal

r@localhost:~$ mkdir gitrep
r@localhost:~$ cd gitrep/
r@localhost:~/gitrep$ git init
Initialized empty Git repository in /home/rohini/gitrep/.git/

‘git init’ command initialises local git repository, so can maintain revision records for all the files in folder ‘gitrep’.

r@localhost:~/gitrep$ gedit first.txt

Can add whatever content wished to add in this file and save.

r@localhost:~/gitrep$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#	first.txt
nothing added to commit but untracked files present (use "git add" to track)

‘git status’ command shows the current status of all the files in our repository. In this ‘first.txt’ file is shown as Untracked file since this files not even single copy is known to git. So here first.txt is untracked file.

r@localhost:~/gitrep$ git add first.txt
r@localhost:~/gitrep$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#	new file:   first.txt
#

Now first.txt is not shown in untracked files list. Which indicates it is a tracked file. But its current content is not present in the repository.

r@localhost:~/gitrep$ git commit -m "new file" first.txt
[master (root-commit) 7093d5e111] new file
 1 file changed, 1 insertion(+)
 create mode 100644 first.txt
r@localhost:~/gitrep$ git status
# On branch master
nothing to commit (working directory clean)

Here git shows first.txt as committed file.

There are three basic storage levels in git on local system:
1)Working directory
2)Index
3)Local repository(head)

1)Working directory: This is the directory in which we are writing or developing our code.
2)Index: this is the one comes into picture when we add a particular file to git. But is not committed yet.
3)Local repository, which also called as head, is in which the file gets saved after commit.

Lets repeat the above example to better understand.

First will create a local repository, will begin with terminal

r@localhost:~$ mkdir gitrep
r@localhost:~$ cd gitrep/
r@localhost:~/gitrep$ git init
Initialized empty Git repository in /home/rohini/gitrep/.git/
r@localhost:~/gitrep$ gedit first.txt

Can add whatever content wished to add in this file and save.

r@localhost:~/gitrep$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#	first.txt
nothing added to commit but untracked files present (use "git add" to track)

Have added a file in the working directory, which is not present in the repository yet or not even know to the index yet.

r@localhost:~/gitrep$ git add first.txt
r@localhost:~/gitrep$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#	new file:   first.txt
#

Now index file which is there in the working directory is known to the index as well, but still it is not present with the local repository or head.

r@localhost:~/gitrep$ git commit -m "new file" first.txt
[master (root-commit) 7093d5e111] new file
 1 file changed, 1 insertion(+)
 create mode 100644 first.txt
r@localhost:~/gitrep$ git status
# On branch master
nothing to commit (working directory clean)

Now the file ‘first.txt’ is present with working directory, index as well as local repository.

Now if changes are introduced in first.txt

r@localhost:~/gitrep$ gedit first.txt

And content of the file is set to ‘new changes other changes’
And add those changes to git using command,

r@localhost:~/gitrep$ git add first.txt

Now again open the same file and edit it

r@localhost:~/gitrep$ gedit first.txt

And content of the file is set to ‘new changes other changes next change’

rohini@localhost:~/gitrep$ git diff
diff --git a/first.txt b/first.txt
index 834325d..f31ec65 100644
--- a/first.txt
+++ b/first.txt
@@ -1 +1,2 @@
-new changes other change
+new changes other change next change
+

rohini@localhost:~/gitrep$ git diff --cached
diff --git a/first.txt b/first.txt
index 3bc7d1e..834325d 100644
--- a/first.txt
+++ b/first.txt
@@ -1 +1 @@
-new changes
+new changes other change

‘git diff’ and ‘git diff –cached’ are commands to see the difference for the three storage levels.

‘git diff’ shows the difference between index and working directory. Whereas ‘git diff –cached’ shows difference between local repository (head) and index.

‘gitk’ is also a command which clearly shows the various commits and changes in those commits, which could be used to identify each change.

About these ads

2 thoughts on “About Git

  1. Reblogged this on The Ruby Railroad and commented:
    Found a really nice post about Git by MyWayOnRails. Git is the predominant source controle solution (in my opinion) and I highly recommend it. I also recommend using Github, as it allows you to pull or commit your source code anytime, anywhere there is a wifi connection, which is excellent for me as I work predominantly out of coffe shops. I also feel software companies will continue to embrace telecommuting as a viable option for their employers, which makes source control systems like Git essential to learn.

Comments are closed.