Posted by: sureshamrita | August 5, 2010

git – a tutorial

It was a big relief to see git. At last, I need not maintain a central repository and the git commands seemed so simple….

What is a version control system? It helps one to maintain different versions of one’s document (whether it be code, tex file, pdf or whatever) in such a way that, any version can be pulled out easily for later reference or modification. All version control systems allow us to add comments so that by reading the comments we can decide which version to pull out in case we want some old version of the document.

What are the different version control softwares? I have used SCCS (very old), RCS, CVS, and SVN. Git is a new version control system. Another popular one is Mercurial

What are the advantages of git? Easy to set up, easy to use, very comfortable branching and merging. It is distributed. It may be noted that git was developed by Junio Hamano and Linus Torvalds.

How to install git? sudo aptitude install git-core

How to start
Go to your working directory and issue the command:
git init
This will create a .git directory in your current directory. Only after this you will be able to use git in this directory. It may be noted that git is a distributed vcs, meaning what you do in one directory will not affect your work in another directory.

Adding your files to version control Git will not take charge of your files in your working directory just because you ran the command git init. Sometimes you may not be interested in tracking all files in your working directory. For example you will not want to track executable files or obj files which are the result of your compilation. You may be interested in tracking only the source code files. As another example, if you are writing a report using latex, you may want to track only your tex or bib files but not the aux or log or pdf files. The files you want git to take charge are to be “added” to it. You add a file to git by using the command,
git add filename
or
git add *

What is committing? Suppose that you have made some small changes to your code and you want to record changes to git (commit) them. Files are committed to git by the command
git commit filename
or
git commit -a
when you want to commit all modified files. This will open up an editor, so that you can write your commit comments. These comments may be made as elaborate as possible so that later you can recover the appropriate files by reading the comments. The editor will also show files that you have changed but not yet added to git. Seeing that you may later decide to add them to git if you need to.

It is strongly encouraged that you commit as often as you want.

Viewing your commits: You can view your commit log by the command git log

A sample from my working directory is shown below.

commit 414748677e07cc1392958b55d91b661ca5a42741
Author: suresh
Date: Fri Aug 20 17:09:15 2010 -0700

 

removed redundant lines in the closest method.

commit fd9e6503d0c52381fc0f6ac405b7b535dd30cb52
Author: suresh
Date: Fri Aug 20 17:07:32 2010 -0700

 

incoroporated SGs comments

The above shows 2 different commits. Each commit is identified with its hash. You can see the commit comment, time of commit and the author. You can see that in this example I have committed twice within 2 minutes. You are encouraged to make as many commits as you want to.

Branching: This is one of the very powerful features of git. I will explain its use by giving an example. I have written code for finding the distance between two objects using say method A. The code is tested and is working fine. Now I want to add method B. Instead of just adding method B code, I will create a branch with name “method B” and work in that branch. The idea is, once the method B branch is tested and working fine, I can merge that branch to the master branch. The advantage of branching is that, if your method B code didnot work, you can go back to your original working code and restart again without any difficulty, by checking out the previous branch.

The branch command is:

git branch branch_name commit hash

Example, git branch method_B fd9e6503d

It may be noted that when you give commit hash, the entire hash string need not be given. Only the initial few bits sufficient to identify the commit uniquely need only be given.

Once you have given a branch name for a specific commit, the branch can be checked out (for working on that) by giving the command,

git checkout branch_name

Merging: Once you have branched and wrote some working code, you may want to merge it with its parent branch or some other branch. As an example, assume that while you were in “branch_a”, you created a new branch named “branch_b” for some new development. Your branch_b development is over and you want to merge your code with branch_a. First checkout branch_a and then merge branch_b with it using the following sequence of commands.

git checkout branch_a
git merge branch_b

Changing the commit editor: By default the git commit editor is nano. You can use the vi editor  for the current working directory by using the command


git config core.editor "vi"

To change the git commit editor globally to say, vi editor,

git  config --global core.editor "vi"

Git and Eclipse: Egit is available as a plugin for eclipse. It works in the Ubuntu maintained version of eclipse. The egit plugin in helios (64bit) does not seem to work for me in Ubuntu 10.04.

GUI for git: So if you need a gui, a very good one is, smartgit. Go for it, I use it every day!

Installing Smartgit: Unfortunately smartgit is not available as an ubuntu package. The binary is available from here. Just unpacking the downloaded file will give the smargit binary. I unpacked the downloaded files in /usr/local/smartgit. Its installation is straight forward too. However, one needs to have sun java installed for smartgit to work. Sun java can be installed in ubuntu 10.04 using the following commands.

  1. To add the relevant repository: sudo add-apt-repository “deb http://archive.canonical.com/ lucid partner”
  2. To update the repository: sudo aptitude update
  3. To install sun java packages: sudo aptitude install sun-java6-jre sun-java6-plugin sun-java6-fonts
  4. If you have already installed sun java but later due to some reasons the free version of java was installed, then the java links can be configured to point to sun java by issuing the following command: update-alternatives --config java and select the sun java option
  5. Enjoy smartgit

Adding smartgit to Ubuntu Menu: Right click on the Ubutnu Applications menu. Select Edit Menu-> Programming->New Item. Add the relevant entries for smartgit. Here afterwards you need not type smartgit.sh on the command line to get smartgit running!!!

For more information about git, visit the documentation page of the git web site.

Thank you!


Responses

  1. where do i get smartgit?????

    • Unfortunately smargit is not available as an ubuntu package yet. You have to download it from
      here. Soon I will write some notes on
      smartgit installation.

  2. instructions on smartgit installation have been added.

  3. nice background…..

  4. Sureshamrita Swamigal,
    Nice to see that you are verymuch alive and kicking!
    Warm regards
    Krishna

  5. Ahh,,, I don’t know whether the comment fits here, there is an online app called dropbox with desktop installable versions for all OS’s. It does almost everything you said provided with connectivity. Add to it, a facility to store all your files online on the go with up to 30 previous versions. Automatic synchronization to any device (including smart phones). Facility to share and control the extent of it. The only disadvantage I found was the limitation of storage. Its restricted to a max of 8 gb, but still enough for m-files and latex I guess.

    • Dear Dinesh Dileep,
      Thanks for your comment.
      I was using dropbox for some time and then I dropped it almost an year back. I know some of my lab mates using it very actively to backup between lab machine and their home machine without paying. One limitation was the quota. Another limitation was, I will forget which will to be added to dropbox – I could not just move my entire current working directory because of small quota. I wanted to write a small program, to find the latest modified files to be sym linked to the drop box directory but finally due to lack of time etc etc I abandoned that idea. Instead I have a paid online backup service (crashplan.com) which does automatic backup of my stuff which I can access from anywhere but only thing is that it is slow. This aspect is remedied by using ‘rsnapshot’ backup, which maintains a 30 min backup of my work. So this is how I do my backup jobs.

      However, git is a version control system and not a backup tool. It allows us to branch and then to merge. I think this is one of the very powerful features of it – not shared by older version control systems. So when I want to test a new idea by writing code, I branch, test it, and if successful, I merge the code into my current main branch. Of course, like other version control systems, it allows us to pull out a specific version by reading our comments logs.


Leave a comment

Categories