History
So, it’s always nice to know a bit of history. What happened was that the Linux Kernel was being maintained using bitkeeper free of charge. However, later Bitkeeper free of charge license terminated. So, Linus Torvalds decided to create Git to maintain the Linux Kernel Development.
Differences with other VCSs
Git is a Version Control System. There are many other VCSs but what’s good with it is that it was initially designed to maintain the Linux Source code which is huge. By that, you can understand that it’s going to consider lots of aspects such as size, bandwidth optimization and many others into consideration.
There are several differences between Git and other VCSs is that Git stores whole snapshots of the project rather than only deltas. Also, files are checksumed using SHA-1 Hash before they are stored and referenced using the checksum.
Now, a file in Git can be three states:
- Modified
- Staged
- Committed
It’s very simple. Whenever, we create/modify a file, we need to explicitly tell Git (Staging Process) to include the file in the next version. Now, to create this “next version”, we simply commit.
Configuration Files
Git has 3 level of configuration:
- System Wide Configuration
- User Specific Configuration
- Project Specific Configurations
What’s more interesting is that the configuration are overridden. So, User Specific configuration overrides System Wide Configurations and Project Specific configurations overrides User Specific configurations.
To view configurations, you can simply use this command:
git config --[system|global|local] -l
1. System Wide Configurations
Global git configurations are stored in the /etc/gitconfig file. To add/modify to this file, simply pass —system as an argument
git config --system user.name "Noorani Bakerally"
2. User Specific Configuration
All user specific configurations are stored in a .gitconfig file. This file is found in the home folder of users. So, to display it, simply run cat ~/.gitconfig.To add/modify to this file, simply pass – -global as an argument
git config --global user.name "Noorani Bakerally"
3. Project Specific Configuration
So, project specific configurations overrides all configurations. So, add/modify these project specific configurations, simply ensure that in the git repository of the project and do git config user.name "noorani Bakerally"
Git Objects
There are 4 types of object:
- blog: Consider it as a file in a git repository
- tree: A directory which can contain other trees or blobs
- commit: A specific point in time containing meta information and a pointer to the previous commit
- tag:A milestone in the life of the project. e.g. A new version
Also, all object have 3 attributes, a type(1 of the above 4), a size and its content.
Git basics
To create a git repository
mkdir new_git_repository
cd new_git_repository
git init new_git_repository
To clone an existing git repository
git clone <URL_Existing_git_repository>
To view status of files from the last commit
git status
File tracking
To manage your files, git track your file so that it can identify and changes to it. This is not automatic as you can avoid tracking some files but not adding adding or simple creating pattern in a gitignore file(we’ll see that later)
To explicitly tell git to track a file, do
git add <filename>
Creating/managing commit
git commit -a -m "the commit message", -a:all, -m:message
A commit is like a milestone in the life of a project. It’s like saving the project state in the project life time. A commit is represented in git as a 40 character SHA-1. Anything that is committed in git can be obtained again. At any point in time, you can return back to a specific commit using the reset.
git reset --hard <commit SHA1 Code>
The above command is dangerous. Suppose currently you have some modifications pending (not yet committed), if you run it, you lose all your modifications. Remember, anything committed can be regained. The negation is also true.
Suppose that you have created a commit and have forgotten to include some files. Therefore, just after the commit, perform the necessary modifications and add the files. Then do a
git commit --amend
To undo a commit,
git reset --[soft|hard] HEAD^
soft leaves all unchanges while hard removes all changes and returns back to the previous commit(HEAD^ refers to the previous commit).
Revert and Undo is different. Revert returns to a previous commit and commit this return. To undo a commit using revert,
git revert HEAD
To revert to a previous commit if you know the commit code:
git revert <SHA-1 Code of commit>
You can also revert by going back in history without entering the hashcode:
# move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}
Just by changing the number in the {}, you can revert back
Sometimes you need to know where you are:
To view the location of the current head,
cat .git/HEAD
this outputs the reference of the current head, ref: refs/heads/master
now to view the current head, simply perform a cat on the file:
cat .git/refs/heads/master
To get the difference between two branches:
git diff –name-status –color branch1..branch2
e.g. git diff –name-status –color master..development
To change the name of the branch you are currently are:
git branch -m newname
File Operations within a git repository
Like in any other folder, in a git repository, you can perform file operations like creating,removing,editing or moving file. For all file operations, you need to explicitly tell git about it. For the “create” operation, consider the file tracking above.
References:
- http://www.codinginahurry.com/2011/02/05/three-levels-of-git-config/
- http://linuxmanpages.net/manpages/fedora13/man1/git-config.1.html
- http://git-scm.com/book
- http://www.gitguys.com/topics/head-where-are-we-where-were-we/








