How to use source control – treat it like a video game
It struck me the other day that the way I use source control is very similar to how I play video games – specifically, when and how I save my game. I like to use source control as a safety net – it’s a way to keep track of a place I may need to return to. It allows me to continue on with my work and not be worried if I go down a wrong path. I can always revert and be right back where I started.
Taking this one step further, we might say that the quick save in a video game is like a commit in source control. Saving your game with a specific name is like creating a branch.
Let’s say I just finish taking out a whole horde of zombies (Left 4 Dead anticipation is infiltrating my blog). I’ll probably do a quick-save to make sure I don’t have to re-play the same encounter again. This is just like committing some code you just finished. You’re saving your current progress to make sure you don’t have to re-write the same code twice.
Now, back to my game, what if I come to a fork in the road. I can go left through the ominous looking sewer, or I can go right through the abandoned city. Well at this point I’ll go and save my game with some sort of name like “Taking the sewer”. That way I can continue playing, quick-saving as I go, but I can always get back to that fork if it turns out I made a mistake.
This is very similar to creating a branch in source control. You want to start working on something new, or something experimental, and you need to be able to save your progress as you go, but you always may need to go back to that original point.
I find that many developers wait too long to commit their changes. The more frequently you commit, and the more organized you are about it, the less you have to worry about keeping track of every change you make. This means you have left cruft floating around in your mind to remember and you can concentrate on writing good code. I think if developers think about source control the same way they think about saving their game progress, it would help them get into the habit of committing frequently and branching appropriately.