Wednesday, April 6, 2011

Configuring and Working with GIT in Ubuntu

GIT - A new name in the Arena of SCM's

Though we already have many SCM's available (Perforce,CVS, SVN, VSS etc), than why GIT?

Actually GIT offers much more than just a simple repository.

Apart from all the other standard SCM features, there are few which I liked the most: -

1. Every developer (by default) has its own private repository, which can be synced with the mainstream at periodical intervals.
2. Already Hosted over the internet (GIT-HUB) and anybody can use it, which itself makes the source code sharing such an easy task. Seems like a Social Networking of code :)
3. Local Branching is easy and Cheaper now, which was a pretty heavy and almost difficult in other SCM's
4. Faster than any other SCM's.
5. Over the Network - Works very well and far better than any other SCM, doesn't eat my network resources for each commit and checkouts are also very quick.

Visit the following liks to know more benefits of using GIT over other SCM

Installing GIT: -

login as root or use sudo to run the below command

apt-get install git-core

Setting up Private repository: -

create and browse the directory which will be used as your local repository

run the following commands

git init

The above command initialize the empty GIT repository

Then clone the empty git repository to create a MATSER REPOSITORY (thats the default Branch for GIT empty repo)

git clone --bare /<Path>/<code-dir>/.git /<PATH>/myrepo.git

myrepo.git - is a Directory which will contain your master repository. All other development folks will be referring to this directory when checking out the code

That's it!!!!You have just completed the client setup and your repository can be used by dev folks

Setting up client or local repository

create and browse the directory which will be used as your local repository

run the following commands

git init

The above command initialize the empty GIT repository

git config <email id>

The above will configure the repository to use the specified the email id for each commit

git remote add origin ssh://<serverIP/ domain-Name>/<Path of your GIT repo>

e.g. in our case it would be something like: -

git remote add origin ssh://<serverIP/ domain-Name>/<PATH>/myrepo.git

The above adds a master server to your repository..This would be used while you are trying to sync your local repository with main repository.

HURRAY----We are done with client easy isn't it

Lets try a simple commit: -

Browse your local directory where you have set up your GIT working repository and create a file by by name "first.txt" and write something in it

Now execute the following commands from the console

git add *

Above will add all the changed or untracked files to your local repository

git commit -m "my First Commit"

Above will commit the code to your local repository

git push -u origin master

Now this will sync your local repo with the master...i.e. you just ready to upload all your changes to the master repository.
As soon as you execute the above command, it will ask for the password and here is what exactly it is doing: -

GIT mainly works on 2 protocols GIT and SSH and while doing an Sync it needs to login to the remote box and connect to the repository.

In case of ssh it asks for the password of the current user (by which you are logged into your local box), assuming that same user is already setup on remote box and have SSH privileges and also have access to git repo.

So make sure that the username (same as your local login) is created on the remote server.

Though it is not necessary that the SSH login of remote box matches your local login (you can use a different username while Syncing your changes) but just to avoid any unnecessary complexities I would have always suggested to use the same Logins.
Also while working on the real networks you will always use your network login, which anyways will be same on the remote box and local box.

Frequently used commands: -

Below is the list of GIT commands which are commonly used in our day to day life


Simply shows the available commands and their usage

git add "list of files to be added"

Adds the List of Files to your local repo

git commit –m "comments"

Commits the files to your local repo

git push -u origin "Branch Name"

Pushes the code to upstream branch and now other developers can checkout

git fetch ssh://<username>@&server-IP or domain-Name>/<Path of the repo on remote box>.git <branch-name>

Used to fetch the branches from the upstream branches to your local repo

git pull ssh://<username>@&server-IP or domain-Name>/<Path of the repo on remote box>.git <branch-name>

Same as Fetch but the only difference is that it tries to merge into the current branch

git checkout <branch-name>

By default every user is on "master" branch. The above command will move him to the specified Branch

git log

Shows the History of the branch on which the person is working.

git status -s

Shows the status of your local repo. Status of all committed or un-commited files

git rm -r >dir>

Used to remove the specified directory from your local repo

git reset --hard <branch-name>

Loose all your local changes and make your code in Sync with the upstream Branch.

Get more insight of GIT: -

No comments: