Sub topic 2
Local, centralized, and distributed version control
This chapter assumes you know why version control exists: it records the history of a project so you can review changes, undo mistakes, and work with others. It also assumes you can run commands in a terminal. Here you will compare the three architectures that version control systems (VCS) have used, and see why Git belongs to the last one.
Local version control
A local VCS keeps all history on one machine. RCS, an early tool from the 1980s, is the classic example. It stores a series of patches (the differences between versions) for each file in a small database on your disk.
This is simple and fast. It also has two limits:
- Collaboration is awkward, because everyone would need access to the same disk.
- The history is a single point of failure. If the disk dies and you have no backup, the history is gone.
Copying files into folders named report_final_v2 is a manual version of this model, with fewer guarantees.
Centralized version control
A centralized VCS (CVCS) puts the history on one server. Developers keep only a working copy of the files, usually one version at a time. CVS, Subversion (SVN), and Perforce work this way.
Centralization solved the collaboration problem. Everyone can see what others are doing, and administrators can control who may change what. It introduced new problems:
- Single point of failure. If the server is down, nobody can commit or view history. If its disk is corrupted and there are no backups, the history is lost.
- Network dependency. Most operations, such as viewing the log, comparing versions, and committing, contact the server.
- Shared-state workflow. Many systems rely on locking files or on updating before you commit, which slows teams down.
Distributed version control
A distributed VCS (DVCS) gives every user a complete copy of the repository, including the full history. Git, Mercurial, and Bazaar work this way. When you clone a repository, you copy every version of every file, not just the latest one.
This changes how you work:
- Commits, history browsing, diffs, and branching run against your local copy, so they are fast and work offline.
- Every clone is a full backup. If a server dies, any clone can restore it.
- You can share changes with several remotes (other copies of the repository) in whatever pattern suits your team.
Seeing it in practice
This script creates a shared repository and two clones on your own machine. It then deletes the shared repository to show that a clone keeps the full history. It needs Git 2.28 or later for --initial-branch, and you install Git in a later chapter.
git init --bare --initial-branch=main shared.git
git clone shared.git alice
cd alice
git config user.name "Alice"
git config user.email "[email protected]"
echo "v1" > notes.txt
git add notes.txt
git commit -m "Add notes"
git push origin HEAD:main
cd ..
git clone shared.git bob
rm -rf shared.git
cd bob
git log --onelineThe last command lists the commit "Add notes" even though the shared repository no longer exists. Bob's clone holds the whole history.
Comparing the models
| Aspect | Local | Centralized | Distributed |
|---|---|---|---|
| Where history lives | One machine | One server | Every clone |
| Works offline | Yes | Limited | Yes, for most operations |
| Collaboration | Poor | Good | Good |
| Backup risk | Disk failure loses everything | Server failure loses everything | Any clone can restore it |
| Examples | RCS | CVS, Subversion, Perforce | Git, Mercurial |
Common misunderstandings
- "Distributed means no central server." Distributed describes where the data is stored, not how teams organize. Most teams still choose one shared remote as the agreed source of truth. Git does not require it, but workflows are easier when everyone agrees on one.
- "Every clone is identical at all times." Clones diverge until their owners exchange changes. Keeping them in sync is a task you perform with commands you will learn later.
- "Full history makes clones huge." Git compresses its data, and a full history is often smaller than you would expect. Very large repositories are the exception, and Git has options for partial clones when you need them.
- "A DVCS cannot enforce access control." Permissions are usually enforced at the shared remote, not inside each clone.
Choosing between them
Today most new projects use a DVCS, and Git dominates. A centralized system can still make sense in some cases. Perforce, for example, handles very large binary assets and fine-grained locking well, which suits some game and hardware teams. For general software work, the offline speed and resilience of Git usually outweigh these benefits.
Exercise
A team's Subversion server loses its disk and has no backup. A team using Git with a shared remote suffers the same loss. Why is the Git team likely to recover, and what do they do?
Show answer
In Git, every developer's clone contains the full history, so any up-to-date clone is a complete backup. The team creates a new shared repository and pushes a clone's history to it. In Subversion, developers hold only working copies of the latest version, so the history is lost.
Exercise
Which of these work without a network connection in a distributed system: viewing history, making a commit, pushing to a remote?
Show answer
Viewing history and making a commit work offline, because they use the local copy of the repository. Pushing to a remote needs a network connection to reach the other repository.