Tuesday 17 July 2012

Git Foundations

I've been using git for the last few years... but I've noticed recently that I've gotten markedly better at it. I think that's because I had to. Anyway, I find the clarity and possibilities opened up by understanding it more deeply very cool. So, in furthering this understanding... I will attempt to explain what I see as the core components and differences between it and other version-control systems.

VCS Rethought - Snapshots not file-system changes


Git sort of rethought version-control instead of just using the same technique as it's predecessors. 
Other version-control systems worked based off of file-system changes.... and tracking these changes over time. Git works based on snapshots. Every time you commit, Git takes a picture of what all your files look like at that moment and stores a reference to that snapshot. It only does this for files that have changed, for unchanged files, it just stores a link to the previous identical file it has already stored. I think in understanding this, it's best to visualize Git as a mini filesystem. 

Local Storage - Intelligent, compressed database


Git does almost everything locally. You can diff a file against the same version of it 2 weeks back without a network connection. The reason for this is that it stores everything, all it's snapshots etc... in it's own intelligent, compressed database. You may say... wow, how big does that get? It actually scales  much better in terms of disk space use than other VCSs. The larger/longer the project the bigger the difference. For the Mozilla project, Git is 30x smaller than SVN.

Three Directories, Three File States


Important: Git has 3 main states that your file changes can be in at any time: Committed, Modified, Staged. And, at any given moment 1 file may exist in all three of these states for you to access and adjust. How does it do this? By basically tracking these changes using 3 different directories: the working directory, the staging area, and the git directory

(Borrowed from Pro Git by Scott Chacon)


The git directory - This is essentially the git database that stores all the info and snapshots of your project. This is what you copied onto your local filesystem when you first cloned the remote repository (from Github or wherever) for your project.
The working directory - This is one version of the project that's been checked out from the git directory for you to work on. This is where your initial file changes, additions go. 
The staging area - When you're happy with some work, but not quite ready to commit it, you can stage it. You do this by adding it to Git. [> git add /dir/filename.rb] or [> git add .]

No comments:

Post a Comment