Friday, August 3, 2012

Git : A DVCS



Git is a Distributed Version Control System (DVCS) that can be used to maintain different versions of files modified over time. It differs from almost all other VCS in that it stores the file(s) on the basis of generations. It does so by employing three states of a file -Commited, Modified and Staged .(click here to know more)
Before we can start using Git we need to have it installed and configured. Installing Git

How to use - Git repository
Repository is basically a store of files or directories that Git uses track the states and modifications to perform version control. There is a set of commands that can be used for initializing and maintaining a Git repository. Also we can get a Git project either by by using an existing project or by cloning from an existing repository.
Here are some commands

$ git init
$ git add <project file or directory>
$ git commit -m "message"
If we want to push the project to a remote repository , then

$ git remote add origin https://github.com/user/repo_name.git
$ git push -u origin master


We can create a python script for this if we do not want enter these commands each time we want to add files using git module as follows

import git
import os
import sys

project = sys.argv[1]
remote_origin = sys.argv[2]
g = git.cmd.Git(project)
g.init()
if os.path.isdir(project):
  files = os.listdir(project)
  for file in files:
    g.add(file)

else:
  g.add(project)

message = "First version"
result = os.system("git commit -m %s" %(message,))
if not result:
  print "Successful!"
else:
  print "Unsuccessful, Some error occurred, error code value: %d" %result
  sys.exit(1)
g.push(remote_origin)  


Run this script as python script.py /home/user/project  https://github.com/user/repo_name.git

The files in the working directory can be in two states : tracked and untracked. Tracked files are those which were present in the last snapshot or generation. Untracked files are everything else.
The command
$ git status

can be used to determine which files are in which state.
So the command
$ git add <filename>

in the above script is used start tracking a new file.
$ git log 

The above command can be used to list the commits or changes from the most recent changes to the least.

There are many more things that can can be done using Git and it also provides efficient way of modifying or undoing the changes made to the project file compared to the other Version Control Systems. (see here)

No comments:

Post a Comment