Home > Articles > Programming > C/C++

Why Do You Need a Version Control System?

  • Print
  • + Share This
In this article, programming expert Bartosz Milewski argues that a version control system is a must-have tool for every self-respecting programmer.
Bartosz Milewski is the author of C++ In Action: Industrial-Strength Programming Techniques (Addison-Wesley, 2001, ISBN 0-201-69948-6), a book that teaches professional programming in C++.

The Big Undo

What would you say if I told you that I've found a terrific programming editor on the Internet, free for the downloading, with all the great features you find in other editors, except one—there's no "undo." I'm afraid you'd dismiss such an editor without even trying it. And for a good reason! Programmers are fallible—we write something, try it, change our minds, rewrite it, go back to the previous version, and so on. Sometimes we implement a whole elaborate idea only to find out that it won't work. We have to go back to square one—and that's when we desperately need the undo button.

The simplest, most elementary application of a version control system (VCS) is to act as a big undo button. An editor might keep a stack of recent editing commands, but this stack has limited capacity and in most cases disappears at the end of your editing session. A version control system has a much bigger stack that's stored persistently between sessions. Obviously, storing the history of all your keystrokes would not only overwhelm your computer, but it would also be very unwieldy. So a version control system stores larger-granularity snapshots of your work. And it lets you decide when to take such a snapshot.

Here's how it works in practice. (Each VCS has its own protocol, so I'll describe the most generic interface.) Before you start working with a VCS, you have to create a project and add all existing files to it. This will constitute your baseline. During a typical editing session, you'll work with a subset of project files. You have to tell the systems to check out these files, using the checkout command—before you start changing them. Quite often, your editor or your IDE can collaborate with the VCS. In such a case, the program will ask whether you want to check out the file the first time you attempt to edit it. Once a file is checked out, all your editing is done on a scratch copy of this file.

The first level of VCS "undo" works by undoing your checkout. The "undo checkout" or uncheckout command returns the file to its pre-checkout state. Whenever you change your mind about modifying a certain file, you can always use the uncheckout command to clean the slate.

Now suppose you're satisfied with your changes. You tell the system that you're ready to take a snapshot. You check the file(s) back in, using the checkin command—this time with your changes. But the version control system doesn't discard the previous state of the files. In fact, it keeps all versions of your files in its database, so that you can retrieve them at your convenience. This gives you the option of "deep undo." You can retrieve a snapshot of your files from a day ago, a week ago, or a year ago with the same ease. In practice, no VCS actually stores full versions of each file—that would take too much storage space. It's enough to store the differences (called diffs) between consecutive versions. This way, if you modify a single line in a thousand-line file, only the modified line will be stored in a diff.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.