Sub topic 2
Local, centralized, and distributed version control
This chapter assumes you already know why version control exists: it records the history of a set of files so you can compare, restore, and combine changes. It also assumes you can open a terminal and run simple commands. You don't need Git installed to read most of this chapter. The hands-on section near the end uses Git, so you can come back to it after the installation chapters.
Every version control system answers two questions. The first is where the history lives. The second is who has to be involved for a change to be recorded. The answers to those questions define three generations of tools: local, centralized, and distributed. Knowing which generation a tool belongs to explains most of its behavior, including why some commands need a network connection and others don't.
The two questions behind every model
Here are the two questions in more detail.
- Where does the history live? It may be a single directory on one computer, a single server, or a full copy on every contributor's computer.
- What does it take to record a change? A commit may be a purely local act. It may instead be a request that a server must accept.
A third question follows from these: how does the system deal with two people editing the same file? Early tools prevented the conflict with locks. Later tools allowed both edits and combined them afterward. This chapter returns to that point after the three models.
Local version control
A local version control system keeps all history on one machine. The tool records each change to a file in a database on disk, usually next to the files or in a hidden directory. SCCS (1972) and RCS (1982) are the classic examples.
RCS, for instance, stores the history of each file as deltas, which are the differences between consecutive versions. It keeps them in a companion file with a ,v suffix. You check a file out to edit it and check it in to record a new revision. The tool works on individual files, not on whole projects.
Local systems have these properties:
- They are fast, because everything is on local disk.
- They work offline, because there is nothing to connect to.
- They are simple to understand and need no server.
- They don't support collaboration. Two people on different machines each have their own database, and nothing connects them.
- They have a single point of failure. If the disk dies and you have no backup, the history is gone with the files.
- They track files individually, so there is no concept of one change that touches ten files at once.
Local systems are still sometimes used for single-file histories or configuration files on one host. You can also build a crude local system with dated copies, such as report_v2_final_FINAL.docx. That is the habit all version control tools try to replace.
Centralized version control
Centralized version control systems (CVCS) introduced a server so that many people could work on the same project. CVS (1990), Perforce (1995), and Subversion (2000) are the best-known examples.
The server holds the single authoritative repository, with the full history. Each developer has only a working copy, which is a checkout of one version of the files, plus a little metadata. Nearly every operation that touches history talks to the server.
- Checkout copies one version of the project from the server to your machine.
- Update brings newer changes from the server into your working copy.
- Commit sends your changes to the server, which assigns them a new revision number.
- Log, diff against old revisions, and blame usually ask the server for the data.
Subversion improved on CVS in an important way. A Subversion commit is atomic, meaning the whole commit succeeds or none of it does. A CVS commit was effectively a series of per-file operations, so a failure partway through could leave the repository with a half-applied change. Subversion also gives the entire repository a single, increasing revision number. "Revision 4120" names the state of every file at one point in time.
Strengths of the centralized model
- There is one place to look for the truth. Everyone agrees what "the latest version" is.
- Administrators have fine-grained control over who can read or write which paths.
- It has a gentle learning curve for simple cases, because a working copy holds one version and has few moving parts.
- It handles very large repositories and large binary files well. Perforce is still used in game development for this reason.
- Locking is available for files that can't be merged, such as art assets.
Weaknesses of the centralized model
- The server is a single point of failure. If it is down, nobody can commit, and if it loses its data without a backup, the history is gone.
- Work depends on the network. On a plane or a bad connection you can edit files but cannot commit, view old history, or create a branch.
- Commits are public the moment they happen. There is no private space to save work in progress without affecting others.
- Branching and merging were historically expensive and error-prone in some of these tools, so teams avoided them.
- Operations that require a network round trip are slower than local ones.
Distributed version control
Distributed version control systems (DVCS) give every contributor a complete copy of the repository, including its entire history. Git and Mercurial (both 2005) are the most widely used. Darcs and Bazaar are other examples.
When you clone a repository, you do not just check out the latest files. You copy the whole database of history. After that, you can do almost everything locally:
- Commit, creating a permanent record in your own copy.
- View history, compare any two versions, and search the past.
- Create, switch, and merge branches.
A separate step moves commits between repositories. In Git, you push your commits to another repository and fetch or pull commits from it. Recording a change and sharing a change are two different actions.
"Distributed" does not mean "no central server"
This is the most common misunderstanding. A distributed system allows many topologies. Most teams still pick one repository as the agreed meeting point, such as a repository on GitHub, GitLab, or a company server. By convention Git calls that remote origin. The difference from the centralized model is that the shared repository is a convention, not a technical requirement. If it disappears, every clone is a complete backup, and any of them can become the new shared repository.
Strengths of the distributed model
- Most operations are fast because they run on local disk.
- You can work offline and push later.
- Every clone is a full backup of the history.
- You can commit freely in private, then tidy your work before sharing it.
- Cheap branching and merging supports workflows where each feature or fix lives on its own branch.
- Different team structures are possible, such as a single shared repository, or contributors who publish to their own copies and ask a maintainer to pull their work.
Costs of the distributed model
- A clone downloads all history, so large or long-lived repositories can be slow to clone and use a lot of disk space. Large binary files make this worse because every version of them is stored.
- There are more concepts to learn: local versus remote branches, commit versus push, and the difference between fetch and pull.
- Without a shared convention, it is not obvious which copy is "official". Teams must agree on it.
- Per-path access control is harder. A clone contains everything, so anyone who can clone can read the entire history.
- File locking, which some binary-heavy teams need, does not fit the model naturally.
Two ways of handling simultaneous edits
When two people change the same file, a system can respond in one of two ways.
- Lock, modify, unlock. A person reserves the file, and others can't edit it until the lock is released. This prevents conflicts but blocks work. It also causes familiar problems, such as someone locking a file and then going on vacation.
- Copy, modify, merge. Everyone edits their own copy. When changes are combined, the tool merges the non-overlapping edits automatically and asks a human to resolve any overlapping ones.
Early local and centralized tools such as RCS, and some versions of Perforce and SCCS workflows, leaned on locking. CVS and Subversion popularized merging as the default, and Git and Mercurial are built around it. Merging works well for line-based text such as source code. It works poorly for binary formats, such as images or compiled assets, where there is no meaningful way to combine two edits. That is why locking survives in some tools and teams.
Comparing the three models
| Property | Local | Centralized | Distributed |
|---|---|---|---|
| Where history lives | One directory on one machine | The server | Every clone |
| Typical examples | SCCS, RCS | CVS, Subversion, Perforce | Git, Mercurial |
| Collaboration | Not supported | Through the server | Through push and fetch between repositories |
| Commit requires network | No | Yes | No |
| Working offline | Full | Edit only | Nearly full |
| Backup if the main copy is lost | None unless you make one | Only the server's backups | Any full clone |
| Private work in progress | Yes | Hard | Yes |
| Access control | Operating system permissions | Per-path rules on the server | Per-repository, not per-path inside one clone |
| Large binary files | Varies | Handled well in some tools | Costly, since clones store all versions |
Hands-on: seeing the distributed model
This example needs Git, which later chapters install. If you don't have it yet, read it now and run it afterward. It builds a shared repository and two clones. It then takes the shared repository offline to show that the second clone can still read history and commit. It uses a bare repository, which is a repository with no working files, the usual form of a shared repository. It requires Git 2.28 or newer for --initial-branch.
set -e
mkdir dvcs-demo && cd dvcs-demo
# The shared repository: history only, no working files
git init --bare --initial-branch=main shared.git
# Alice clones it, commits, and publishes
git clone shared.git alice
cd alice
git config user.name "Alice"
git config user.email "[email protected]"
echo "first line" > notes.txt
git add notes.txt
git commit -m "Add notes.txt"
git branch -M main
git push -u origin main
cd ..
# Bob clones the shared repository
git clone shared.git bob
cd bob
git config user.name "Bob"
git config user.email "[email protected]"
# Simulate the server going away
mv ../shared.git ../shared.git.offline
# History and commits still work with no server
git log --oneline
echo "second line" >> notes.txt
git commit -am "Extend notes.txt"
git log --oneline
# Server returns; Bob shares his commit
mv ../shared.git.offline ../shared.git
git push origin mainThe first git log --oneline shows Alice's commit, which Bob received at clone time. After his commit, the second git log --oneline shows two commits, even though the shared repository was unreachable. The final git push is the step that shares Bob's work.
The git config lines here apply only to each clone. Configuration scopes are covered in a later chapter.
In a centralized system, Bob's commit step would have failed while the server was unavailable.
Choosing a model
For most software projects today, a distributed system is the default. Git has the largest ecosystem of hosting, tooling, and documentation, and that alone is a strong reason to choose it. Still, the other models have real uses, and these criteria help you decide.
- Repository size and content. Very large repositories, or ones dominated by large binary files, may fit a centralized system such as Perforce better. Git has extensions for large files and partial clones that reduce this gap.
- Need for locking. If your files can't be merged, such as design assets, check whether the tool supports locking.
- Access control. If different people may see only parts of a project, per-path permissions on a central server are simpler than splitting the project into several Git repositories.
- Connectivity. If people work offline or on slow links, a distributed system gives a better daily experience.
- Team familiarity and existing tooling. Migrating a team and its automation has a cost, and sometimes the right choice is to stay where you are.
Bridges also exist. git svn lets a person work with a local Git repository against a Subversion server, which is a common way to adopt Git gradually.
Common errors in thinking about these models
- Treating commit and push as the same thing. In Git, committing is local. Your teammates see nothing until you push. People coming from Subversion often expect
committo publish. - Believing a clone is just the latest files. It is the full history, which is why clones can be large and why a deleted secret remains in them.
- Assuming a server-less workflow is required. Using a shared repository is normal and recommended for teams. Distributed means you are not dependent on it.
- Assuming revision numbers work everywhere. Subversion gives one global number per commit. In a distributed system there is no single counter, so Git identifies commits by a hash, a long identifier computed from their content.
- Assuming a backup exists because of cloning. Clones help, but they only hold what was pushed or fetched. Unpushed work exists in one place only.
Exercise
A developer on a flight with no network wants to commit three small changes, review the history of a file from last year, and create a branch. Which of these can she do with a centralized system, and which with a distributed one?
Show answer
With a centralized system she can only edit files. Committing, viewing old history, and creating a branch all need the server. With a distributed system she can do all three, because her clone holds the full history and commits and branches are local. She shares her commits later by pushing.
Exercise
A team uses Git with a shared repository on a hosting service. The service suffers a total data loss. Is the project history gone? Explain what the team should do.
Show answer
No, as long as someone has a clone. Each clone contains the full history up to the last time it was fetched. The team should find the clone with the most recent work, create a new shared repository, and push to it. Anything that existed only on the lost service, such as unpushed work on other machines, can be pushed from those machines afterward.
Exercise
Why is "Git is decentralized, so you don't need a central repository" misleading?
Show answer
Git does not require a central repository, but most teams still choose one shared repository as the agreed place to publish work. The point of the distributed model is that this repository is a convention, not a technical necessity. Every clone is complete, so the shared repository can be replaced without losing history.