First words on git…

I have been meaning to do a post on git for a while, hoping to write some gigantic treatise on how it’s such a good thing. Rather, figure I would take little chunks and blog about them. Today, I did something simple. I set up a master git repository on my web server. The benefit here is that it’s a common place with a well-known name (primordia.com) that’s available everywhere.

Previously, I had synchronized code using git between my desktop and laptop, but neither machine is an ideal master repository because:

  • My laptop often has the firewall enabled
  • My desktop goes to sleep often
  • Both machines use DHCP and every now and then the IP changes and I don’t want to bother with static IP’s
  • etc.

So, my web server has shell access. I created a new user, created a new master repository, and synchronized my laptop and desktop to this repository. I imagine some of you have had experience with git and some haven’t. For those of you with no experience, git is a version control system just like CVS, Subversion, Perforce, et al.

What’s unique about git is that it’s fast, easy to use, and works on a distributed model so powerful it’s silly. The git website has this to say:

Git is a free & open source, distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Every Git clone is a full-fledged repository with complete history and full revision tracking capabilities, not dependent on network access or a central server.Branching and merging are fast and easy to do.

Git is used for version control of files, much like tools such as MercurialBazaarSubversionCVSPerforce, andVisual SourceSafe.

So, what that says is that when you create a repository from scratch, or you clone it from some remote repository (another one of your machines or a central server), the repository you create is a full repository and can serve as a master for other-sub-repositories.

Also, branching with git is ridiculously fast and easy. You can clone your entire tree instantly and start working on little experiments. Files are identified in a hidden database and all files are hashed. Hashes are compared for various operations and this makes git very fast. As far as I know, you don’t check in files, you checkin file contents.

I have my bash prompt setup so that when I’m in a branch, it shows up in my prompt. Say I have a git repot in ~/src.projects. When I’m working in the “master” branch, my prompt looks  like this:

disven: ~/src.projects (master) $

If I create a new branch, say for some experiments, I execute these commands:

disven: ~/src.projects (master) $ git stash
Saved working directory and index state WIP on master: 729d6b3 Added header.
HEAD is now at 729d6b3 Added header.
disven: ~/src.projects (master) $ git branch experimental
disven: ~/src.projects (master) $ git checkout experimental
Switched to branch ‘experimental’
disven: ~/src.projects (experimental) $

git stash assumes I want to save away changes I made to master without checking them in and I want experimental to be created from a clean master branch. Notice creating a branch and switching to it are trivial. If I want to forget about my changes, I just do this:

disven: ~/src.projects (experimental) $ git checkout master
Switched to branch ‘master’
disven: ~/src.projects (master) $ git branch -D experimental
Deleted branch experimental (was 729d6b3).

If I wante do keep my changes in experimental, I would commit them and merge them:
git commit -a
git checkout master
git merge experimental
There is tons of good tutorials on git. I’m still essentially a newbie even after  using it for two months. I’ll leave y ou with this one last git feature. The repote repositories do not need to me git.. t hey can be subversion, cvs, perforce, etc. What t his means is you can use git locally do experiment and manage the many features you’re working on, and commit your changes (say, to perforce) only when you’re ready… or on a feature-by-feature (branch-by-branch) basis!
Enjoy!

One Reply to “First words on git…”

Comments are closed.