Know About GIT !

What is git?
Git is a free and open source distributed version control system.

Installation of git in Ubuntu:

$sudo apt-get install git 
You can check whether the installation is successful by:
$which git
Git records your name and email address with all your commits so first you have to do:
$git config --global user.name 'Your name'
$git config --global user.email 'Your email id'
and you can see the settings with this command:
$cat ~/.gitconfig

First to work with git you need a Git repository. Which can be made either by cloning from an existing public Git repository or by initialising a new reposiory in a directory using the command:

$mkdir Project

$cd Project

$git init 
This will create an empty repository in /path/Project/.git .
If you want to work on an existing project and use its source code then you can clone it by:
$git clone "https://github.com/username/repositoryname.git

git

The local repository consists of three “trees”:
1) Working directory – where the actual files are located.
2) Index – the staging area, ie where the changes are stored before the commit.
3) Head – It points to the last commit that has been made.

Commands in git :

$git add – It adds file to the staging area which is place to store the changes before commit.
$git status – It shows the status of the files in the working directory and staging area.
$git commit – Records the snapshot of the staging area, ie. all the changes are recorded. For adding the commit message while doing commit in the command line the following command is used:
$git commit -m 'My First Commit'
$git diff – Show the changes that have been made in the project after the last commit, which is not staged.
$git rm – It removes the file from the staging area which was added by using git add.
git2

The above things are done in a local repository. If it is to be shared among other people it should be added to a remote repository. To connect to a remote repository we use the following command:

$git remote add origin [url] 

To push the contents to the remote repository do:

$ git push -u origin master

$git fetch – Fetches new data from the remote server.

$git pull – It also do the same function as that of fetch. It differs by merging the data into the current branch.

git3

Git supports another great feature called branching. Branching helps in adding changes to the existing code without disturbing it. It has many use.  I will write about branching in git in another post.

Leave a comment