GIT – a widely used version control system

by karthikselvakumar


git is a widely used version control system with its flexibility and rich command sets .

Here covers three most basic and important functionalities in GIT .

i) Creating a new repository and committing

ii) working with branches

iii) Cloning and distributed working

Creating a new repository and committing

This is a basic operation in any version control system. Assume a scenario  where you are creating a maths software using C .

now each project should have a name ( I will be speaking package , software, project and repo interchangeably )

let it be myCproject . now to create a new repo the steps are :

step 1)

create a new directory of that project name

>> mkdir  myCproject

step 2)

get into the project and intialis it for git database

>>cd myCproject

>>git init-db

now your project is initialized and ready to accept files .

step 3)

start coding some files and place it myCproject directory . eg ) add.c,mul.c,sub.c,div.c

now this files wont get added to git unless you add into it

>> git add .

>>git commit -a -m ” Your first commit “

step 4)

now your first version of the software should be commited and you can check it by

>>  git status

or

>>  git whatchanged

step 5)

whenever you perform more than one commit then use

>> git diff

to get the changes from first and second commits .

Sample window :

Working with branches :

now placing all files under single category may be tiresome . So we can group similiar files under a branch .

branch is something like a sub-repo

step 1)

first create a new branch under which you are going to work

let us say math software needs a gui

>> git branch gui

this will create a branch named gui .. to see the list of branches in project you can use

>> git branch

step 2)

by default git will be in branch named master .

to switch over to gui  branch use

>> git checkout gui

now your workings will be placed under gui branch after changed dont forgot to commit

>> git commit -a -m ” commit from gui branch “

step 3)

now to switch back to master branch use

>> git checkout master

step  4)

to merge all the changes done by the branch gui to master use

>> git merge gui

and after merging if the branch is no more needed then delete it

>> git branch -d  gui

to get a graphical view of branches and files use gitk

Cloning and distributed working :

step 1)

whenever more than one user works with a project then sharing should take place . say two user alice and bob .

bob wants the myCproject from alice . then bob should issue

>>git clone /home/alice/myCproject

now bob will get a copy of alice in his current directory

step 2)

similiarly commit and add are done as

>> git add sub1.c

>> git commit -a -m ” commit from BOB “

step 3)

to get the changes made by bob alice will pull bob and get the changes by

>> git pull /home/bob/myCprojects


Commands overview :


git init-db     = initialsises the repository

git add file1 file2 file3   = adds the files to git repository

git commit -a = commits with the next version

git status = current status of files and branch name

git whatchanged = history of all versions

git diff = difference between previous and next version

git branch branch-name = creates a new branch

git branch = lists the all available branches

git checkout branch-name = switches to particular branch

git merge branch-name = merges with other branch

git branch -d branch-name = deletes unwanted branch

git clone repo-url = clones a existing project from other url

git pull repo-url = gets the changes from other repo