git add before commit
I’ve been experimenting with git for version control of my document folder, before I use it professionally. It has several things I like, particularly the blazing speed and the fact that there’s a single .git directory instead of a myriad of .svn all over the place (which made it a bit of a pain to keep those directories in sync if one of of the sub-.svn’s got erased by, say, an over-eager script).
Coming from subversion and cvs, something struck me as odd: if you do git commit, it will only commit the changes that you have just added or removed – anything else needs to be explicitly added to the working index, even if you have added it before. Bit of a pain if you simply want to commit everything.
Digging around the git docs, there’s an option you can pass so that it will just commit all files you have previously told it about: git commit -a. There’s also an option invoked by git commit –interactive that causes it to ask you for each file if you want it to go on the current commit or not.
As to why that is:
What git add does is to move the current version of the named file to a special staging area, holding files that are ready to be committed. And what git commit (without other arguments) will do is to take the index and make a new revision out of what the index contains. git commit -a is just a convenience which adds all modified files to the index, and then commits the result.
How does this affect you? The first thing to remember is this one: only run git add on new files just before committing. Otherwise, you’ll commit the wrong contents of the file.
It takes a bit getting used to, but it’s a lovely tool so far. If you’re thinking about using it yourself, start by reading Git Magic, and then after the first three chapters move to Using git without feeling stupid and its part 2.