The Git index is the killer feature of git. Now obviously Git has many other things going for it, but I was searching for the real differences between Git and Mercurial and this appears to be the trump card of Git.
You may know that I am a fan of Mercurial, and have fought against the tides of Git. I had an excuse for every feature that people brought up as a "killer feature". I thought that git stash was a crappy version of a full on Quilt, like Mercurial Queues (hg q*). I thought that git rebase -i was a less featured hg transplant. Then Xentac brought up that git add -i was his favorite command. And so ensued a discussion on what a git index actually is, and how it affects your development. I shall now summarize what a git's index is, and how it makes you a happier developer.
Git has 3 places you can store changes: the working copy, the index, and the object database. The working copy is the files being tracked by your repository as they are in your filesystem. The object database is the place where tree objects (diffs/deltas/what have you) are stored when you type in git commit. The index is the middle ground. It is the "cache" or a temporary staging environment for the tree object of which you are about to commit.
What does this give you? It gives you completely non-linear development. A good example is that you are working on some new feature, and half way through you realize that you should refactor some completely unrelated function into two functions, for whatever reason. You could edit them now, but you're afraid both the feature and the refactor changes will appear in the patch you are going to send off. They are highly unrelated and you'd like them to be in two seperate commits. With git, this non-linear development is supported ( can we call it polynomial development? I'd like that. ). You can edit your index so that way the refactor is commited, and then edit again so the feature you were initially working on is in a seperate commit.
Nifty! But how do you actually do that index edit? git add -i to the rescue. Make your edits, then run git add -i. You will be prompted interactively to edit and change the index. When you are done, run git commit and be done! You have just prepared, and commited a tree object manually.
I will be migrating stuff over to git. Out of both popularity and features! The only thing I'm going to miss is plugins, but I'll live.. it's not like I wrote mercurial plugins all day.
20 Comments