Git – A distributed SCM

Git is a distributed source code management system. A very powerful, fast and reliable way of managing source code. I started working with Git in 2010 and I found it better than any other non distributed SCM. I thought of sharing all basic Git operation which one can use and become productive very fast.

Please note : This article does not focus on Git vs SVN or CVS. This gives information on most useful commands with Git which can help a person to quick start with it.

Most useful commands with Git:

SSH operation (Key generation): Use this to allow your repository to securely communicate with Github, Beanstalk or some other hosting platform or even with your team mate.

$ ssh-keygen -C "email-id" -t rsa

Git configuration

git config --global email-id
git config --global "Dharmesh Borad"

Repository operation

1> How to create Git repository : $ git init

2> Add remote repository : $ git remote add github

Branching and Merging operation with Git

1> Creating branch under this repository

$ git branch branchname

2> Listing all branch under this repository

$ git branch

3> To switch between branches. Below command will switch to newly created branch i.e. “branchname”.

$ git checkout branchname

4> To merge with master branch (branchname -> master)

$ git checkout master

$ git merge branchname

5> To delete/remove branch

$ git branch -d branchname

6> To delete branch on remote machine(origin alias of remote machine/repository)

$ git push origin :branchname

Add operation

1> Adding files to repository index for further commit

$ git add filename

2> Adding folder to repository index for further commit. This will add files from that folder also.

$ git add foldername/

Remove/Delete operation

1> To remove file/folder form .git index but to not remove physically

$ git rm –cached filename

2> To remove files from index as well as from physical storage. Use –r for recursive remove.

$ git rm -r myfolder/

$ git rm myfile

Commit operation

1> Committing to local repository. –a is for all and to skip staging area. –m is for message.

$ git commit -a -m "commit message"

Remote operation

1> Pulling from repository

$ git pull remotename branchname

2> Pushing to repository

$ git push remotename branchname

Help operation

1> To get help on any command

$ git help commandname

To fetch branch from remote repository

git remote show origin

1> If the remote branch which you want to checkout is under “New remote branches” and not under “Tracked remote branches” then you need to fetch them first from remote

git fetch branchane

2> Now it will allow to create branch out of fetched one

git checkout -b local-name origin/remote-name

In case if you are looking for some very specific operation please write me back. I will try to give details in my free time.