Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Engineering Concepts (http://www.go4expert.com/articles/engineering-concepts-tutorials/)
-   -   Introduction to Git VCS (http://www.go4expert.com/articles/introduction-git-vcs-t27216/)

pradeep 25Nov2011 18:06

Introduction to Git VCS
 
All software developers must have used some VCS (Version Control System) at a point of their career, most would say SVN, which is currently the widely used VCS in the software industry. Enter Git, created by Linus Torvalds in response to missing features & performance issues in the other VCSs, while designing Git he kept in mind the drawbacks of the existing VCSs.

Git is a distributed VCS which is the fastest amongst existing systems and has become very popular in the Open Source community and also is being used by corporate development teams. Git's by design allows use to create any type of workflow you require.

Many of you must be wondering what is meant by the term 'distributed', well, distributed means that Git does not require a central sever and not even an active network connection to be used, because stores a copy of the full repository on the local machine, whereas you may work, commit, branch, merge & push the changes to a central repository any time you wish, but it is completely optional unlike SVN.

Why Do We Need Version Control?



As students & novice programmers we all must have at some point wanted to undo some parts of our code, because we realized our mistake too late or for some other reason, so most of us must have been into the habit of renaming the source files with extensions like .bak, .bak1 or .bal_2011_11_25 etc. This is the lay man's way of 'version control', but it's very cumbersome and hard to track, that's where VCS like SVN & Git come to help, they maintain & track changes of source files, binary or text, which we may choose to revert to later on or merge changes of an older version of a file with the latest copy, etc.

Version control is not just for programmers, it is useful for anyone who would like to keep a history of changes to their project files, like designers, artists, writers, music composers etc.

Installing Git



Git being open source, you may compile it from source or install an available binary.

Go to the Download page or read the Install instructions

You may also install Git on Linux using the native package management app.

Code:

# yum install git-core

# apt-get install git-core

# pacman -Sy git

# emerge 'ask --verbose dev-util/git

On Windows, installing Git is pretty easy. Simply download and install the msysGit package.

Git Basics



Instead of diving into the sea of Git commands, let's have a look a the basic usage.

Change to the directory whose files you would like to be versioned, and initialize a git repository by issuing,

Code:

$ git init
to register the files with Git & take the first snapshot, issue the following command,

Code:

$ git add .
$ git commit -m 'First snapshot'

after making changes to the file(s), you may save the current state again by issuing,

Code:

$ git commit -a -m 'Save again'
the option -m is used to put the message for the commit.

To add new files or remove existing files,

Code:

$ git add new_program.pl
$ git rm old.pl

save after changes,

Code:

$ git commit -a -m 'added new file & removed old file'
see the log of commits i.e. the snapshots

Code:

$ git log
Output:
Code:

commit d311fbb70231fa65283b61d648666c8e182a7b33
Author: S Pradeep <pradeep@g4e.com>
Date:  Fri Nov 25 17:23:41 2011 +0530

    added new file & removed old file

commit bb57ed20b94187bbb52096ca1632bda09a2d3d64
Author: S Pradeep <pradeep@g4e.com>
Date:  Fri Nov 25 17:01:26 2011 +0530

    Save again

commit 1cff9294aa249870aaa27d3b29007305a0ad902a
Author: S Pradeep <pradeep@g4e.com>
Date:  Fri Nov 25 17:01:06 2011 +0530

    First snapshot

Undo & Redo



To return to an older state and erase history after that point, issue the following command,

Code:

$ git reset --hard bb57
in the above example 'bb57' is the first few characters of the commit you want to return to.

To return to an older state while preserving the history of commits,

Code:

$ git checkout bb57e
This feature is called branching, which we'll come to in the next part in the series.

To revert a recent commit,

Code:

$ git revert bb57e

View Changes



To view changes made since the last commit,

Code:

$ git diff
Or since yesterday,

Code:

$ git diff "@{yesterday}"
Difference between two commits,

Code:

$ git diff d311 1cff9
I hope you have enjoyed this article, we'll cover more advanced features in the next part.

Resources



book.git-scm.com
thinkvitamin.com
progit.org


All times are GMT +5.5. The time now is 03:44.