Version control: how Git remembers every edit and brings a team’s work together
A program is almost never written in one sitting or by one person: code changes over years, dozens of people work on it, and any edit can break something. A version control system stores the project’s entire history (who changed each line, when and why), lets you run several lines of work at once, and brings them back together. The most widespread such system is Git. Let’s look at what it stores on disk, what a commit and a branch are, and how different people’s edits meet on the server.
Why it’s needed: “final_version_2_really.docx”
Without a version control system, a project’s history is a bunch of folder copies with dates in their names. A month later it’s impossible to tell how one copy differs from another, which edit broke the program and who made it. And if two people edited the same file, one will overwrite the other’s work without even noticing.
A version control system does three things. It stores snapshots of the project: not just the latest, but all of them, with a description of each change. It lets you run parallel lines of work (branches): one is the stable version, another a new feature, a third an urgent fix. And it brings those lines together, automatically merging edits that don’t touch the same lines.
Early systems (CVS, Subversion) were centralized: the history lived only on the server, and without a network connection you couldn’t even look at a past version. Git, which Linus Torvalds wrote in 2005 for the Linux kernel, is distributed: every participant has a full copy of the history. The server (GitHub, GitLab or your own) is just one more copy that the team has agreed to treat as the shared one.
What’s in the .git folder: objects and hashes
The project’s entire history is stored in a hidden .git folder next to the files. Its foundation is an object database, where each object is named by the hash of its contents (SHA-1, 40 hexadecimal digits; new repositories can use SHA-256). There are only three main kinds of objects. A blob is the contents of a single file, without a name. A tree is a list of “name → blob or nested tree”: a snapshot of one folder. A commit is a reference to the root tree, references to parent commits, the author, the time and a message.
Because an object’s name is the hash of its contents, identical files are stored only once, and an old commit can’t be swapped out unnoticed: its hash would change, and along with it the hashes of all later commits that refer to it. History in Git is a chain in which each link is “fastened” by the hash of the previous one.
Git stores snapshots, not differences: each commit points to a complete tree of the project. This isn’t wasteful, because unchanged files and folders reuse objects that already exist. And to save disk space, Git periodically packs objects into pack files, where similar versions of the same file are stored as the difference between them (compressed with zlib). That’s why a repository with ten years of history often takes up less space on an SSD than the working folder itself.
See also: Storage: SSDs and hard drives
Three places for a file: working folder, index, commit
Every file in Git lives in three states. The working folder holds the ordinary files you open in your editor. The index (staging area) is a draft of the next commit: the git add command copies the current contents of a file there. The repository holds the saved commits: git commit turns the index into a tree and writes a new commit.
Why the intermediate step? It lets you assemble a commit deliberately: out of ten changed files, save together only the three related to one task and leave the rest for later. A good history is a sequence of small commits with clear messages (“Fix price rounding”), not a single “everything from this week” commit.
git status shows how these three places differ: which files are modified but not added, and which are staged for the commit. git diff shows the changes themselves line by line. The lab in this section shows all three levels at once: an edit highlights the file in the working folder, add moves it to the index, and commit creates a new commit cube in the history.
Branches and HEAD: just pointers
A branch in Git is a tiny file containing the hash of one commit: .git/refs/heads/main holds 40 characters. When you make a commit, Git writes a new commit whose parent is the current one and moves the current branch’s pointer to it. That’s why creating a branch is instant and almost free: git branch feature simply writes one more pointer to the same commit.
HEAD is the “where am I now” pointer: usually it points to a branch (ref: refs/heads/main), and the branch points to a commit. git switch feature switches HEAD to another branch and brings the working folder to the snapshot of that branch’s commit. If you switch directly to a commit rather than a branch, HEAD becomes “detached” (detached HEAD): new commits won’t end up on any branch, and once you switch back they’re easy to lose; Git will warn you and suggest creating a branch.
Branches make parallel work cheap. The typical routine: main is what users are running; each task gets its own short-lived branch; when the task is done and reviewed, the branch is merged into main. That way unfinished work doesn’t get in anyone’s way, and main can always be released.
Merging and conflicts
When two branches have diverged, git merge brings them together. Git finds the common ancestor, the last commit present in the history of both branches, and compares both sides against it. If one side changed a file and the other didn’t, the changed version is taken. If both changed different places in the same file, the edits are combined line by line. The result is a merge commit with two parents.
If, however, the branch being merged into hasn’t changed since the two diverged, the merge reduces to a “fast-forward”: the pointer simply moves ahead to the latest commit of the other branch, and no new commit is needed.
A conflict arises when both sides changed the same lines in different ways: the heading is “Shop 2.0” in one branch and “Best Shop” in the other. Git doesn’t guess which is right; instead it writes both versions into the file between the markers <<<<<<<, ======= and >>>>>>> and stops the merge. A person chooses or combines the versions, deletes the markers, adds the file to the index and makes a commit, and with that the conflict counts as resolved.
The alternative to merging is rebase: the branch’s commits are “moved” on top of the latest main, as if the work had started later. The history comes out linear, but the commits get new hashes, so you must not rewrite history that has already been published and pulled by colleagues.
The server: clone, push, fetch and pull
git clone copies the entire repository from the server to your computer, all objects and branches, and remembers the server’s address under the name origin. From then on you work locally, without a network: commits, branches, browsing history all happen on your own disk.
git push sends the server commits it doesn’t have and moves the branch on the server. The server accepts a push only if it is a “fast-forward”: if a colleague managed to push their commits first, your history has branched off, and the push will be rejected; you first need to fetch the other person’s work and combine it with yours.
git fetch pulls new objects from the server and updates “remote-tracking” branches such as origin/main without changing anything in your work. git pull is fetch plus a merge (or rebase) of origin/main into your branch. Under the hood the exchange runs over HTTPS or SSH: the client and server compare which commits each one has and send only the missing objects as a single packed file.
A team workflow is built on top of this: on GitHub or GitLab, a branch isn’t merged into main directly; instead a merge request (pull request) is opened. Colleagues review the changes, automated checks (CI) build the project and run the tests, and only then is the branch merged. That way every line in main has been through both a human and the tests.
See also: Network and server: how data finds its way and comes back · Data center: where websites live
Where Git meets the hardware
Hashing is Git’s main computational workload: on every add and commit, the processor computes SHA-1 over the contents of the files. By default Git uses a variant of SHA-1 that detects known collision attacks; it is slower than “plain” SHA-1 but still processes hundreds of megabytes per second on a single core, so for an ordinary commit it takes a fraction of a second.
So that git status doesn’t reread the whole project, the index stores each file’s size and modification time from the file system: if they match, the file is considered unchanged and isn’t hashed again. That’s why status in a repository with a hundred thousand files takes milliseconds; it is mostly stat system calls to the OS and reading metadata that the OS keeps in RAM.
Everything else is work for the storage drive: objects and pack files live on the SSD, and when you clone a large project (the Linux kernel is several gigabytes of history), the speed is limited by the network and by disk writes. That is exactly why Git is so careful with space: compression, deltas and reuse of unchanged objects.
See also: The processor core: an educational datapath and a modern core