Sub topic 1
Why Version Control Exists
Prerequisites
This chapter assumes you can do the following:
- Work with files and folders: create, copy, rename, and move them.
- Open a terminal or command prompt on your computer.
- Run simple shell commands such as
lsordir, and read their output.
You do not need to know Git yet. You also do not need to have written software before. Anyone who keeps important files that change over time can use the ideas here.
The problem: files change, and mistakes happen
Think about any document you have worked on for more than a few days. A report, a spreadsheet, a configuration file, a program. You edit it, save it, and the old version is gone. Most of the time that is fine. Sometimes it is not:
- You delete a paragraph on Monday and realize on Thursday that you needed it.
- A change that looked harmless breaks something, and you cannot remember what you changed.
- Two people edit the same file at the same time, and one person's work overwrites the other's.
- A teammate asks which version of a file is the current one, and nobody is sure.
Version control is a system that records changes to a set of files over time, so you can recall specific versions later. It answers questions that file systems alone cannot: what changed, when, who changed it, and why.
A file system stores only the current state of each file. A version control system stores the history that led to that state.
The manual workaround and why it fails
Almost everyone starts with manual versioning. You copy the file and rename the copy:
report.docx
report_v2.docx
report_final.docx
report_final_REALLY_final.docx
report_final_edited_by_sam.docxThis works for a day or two. Then the problems accumulate.
| Problem | What happens |
|---|---|
| Naming is ambiguous | Nobody agrees whether `final` or `final_v2` is newer. |
| No record of why | A copy tells you that something differs, not what or why. |
| Storage grows | Every copy holds the whole file, even if one line changed. |
| Many files drift apart | A project with 200 files needs 200 copies per version, and they must match each other. |
| Collaboration breaks | Emailing copies back and forth creates parallel versions that someone must merge by hand. |
| No safe experiments | Trying a risky idea means copying the entire project first. |
A version control system automates this bookkeeping and adds what copies cannot provide: a described, ordered history that you can search, compare, and restore.
What a version control system gives you
A version control system (VCS) provides several capabilities. The rest of this tutorial series builds on them.
History
Each time you record a state of the project, the VCS stores it with a timestamp, the author, and a message that you write. The message explains the intent of the change. Months later, a message such as "Fix rounding error in invoice totals" is far more useful than a file named invoice_v7.py.
Restoring and comparing
You can return a file, or the entire project, to any earlier recorded state. You can also compare two states and see exactly which lines differ. The comparison of two versions is called a diff. Because a diff shows only what changed, you can review a large project by reading a small amount of text.
Attribution
The history records who made each change. This is not about blame. It is about finding the person who knows why a decision was made, and about tracing a bug to the change that introduced it.
Parallel work
Several people can work on the same project at once without sending files around. Each person's changes are recorded separately and then combined. When two people change the same lines in incompatible ways, the system flags a conflict, and a person decides how to resolve it. The system does not silently choose a winner.
Safe experiments
You can try an idea on a separate line of development, called a branch, while the stable version stays untouched. If the idea works, you combine it with the main line. If it fails, you discard the branch. Branches are cheap in modern systems, which changes how people work.
A trustworthy backup
When history is stored in more than one place, losing a laptop does not mean losing the project. Version control is not a replacement for backups, but a copy of the history on another machine provides much of the same protection.
A small worked example
Consider a script that converts temperatures. Here is the first version:
def to_fahrenheit(celsius):
return celsius * 9 / 5 + 32
print(to_fahrenheit(100))Running it prints 212.0. Suppose you record this state with the message "Add Celsius to Fahrenheit conversion". Later you change the function and accidentally break it:
def to_fahrenheit(celsius):
return celsius * 9 / 5 - 32
print(to_fahrenheit(100))The script now prints 148.0, which is wrong. Without version control, you must find the mistake by reading the code. With version control, you ask the system for the difference between the last good state and the current one. The diff looks like this:
- return celsius * 9 / 5 + 32
+ return celsius * 9 / 5 - 32The line that changed is obvious. You can fix it by hand, or ask the system to restore the earlier version of the file. In a real project, a change like this might sit among hundreds of other edits, and the ability to isolate it is what saves hours of work.
What gets recorded
Version control systems work best with text files such as source code, configuration files, documentation, and data in formats like CSV or JSON. For text, the system can show line-by-line differences and combine changes from several people automatically in most cases.
Binary files such as images, videos, compiled programs, and office documents are different. The system can store them and keep their history, but it usually cannot show a meaningful diff or merge two edits. Large binary files also make a history heavy. Teams often handle these with extra tools or keep them outside the history.
Version control also has a limit on what it should hold. Do not record these:
- Passwords, API keys, and other secrets. Once recorded, they live in the history, and removing them completely is hard.
- Files generated by a build, such as compiled output, which can be recreated from the source.
- Machine-specific files such as editor settings or temporary files.
Who uses version control, and for what
Software teams are the best-known users, but the idea applies more widely:
- Developers track source code, which is the original use.
- Operations engineers track infrastructure and server configuration, so a change to a system can be reviewed and reversed.
- Writers and researchers track manuscripts, papers, and analysis scripts, so a result can be reproduced from a known state.
- Designers and data teams track documents and datasets, often with additional tooling for large files.
Individuals benefit as well. A solo developer with no collaborators still gains undo, history, and safe experiments.
How the history looks
A history is an ordered series of recorded states. Each state builds on the one before it. The following diagram shows a simple project history with a branch for an experiment.
Each box is a recorded state. The arrows show which state came before. The experiment started from State 2, ran separately from the main line, and was combined into State 4. A history of this shape is not possible with a folder of renamed copies, because copies do not record how they relate to each other.
A short history of the idea
Early tools tracked changes to individual files and did not coordinate between people. Later tools added a shared server so teams could work from a single central store. More recent tools give every user a complete copy of the history. The next chapter compares these three approaches, which are called local, centralized, and distributed version control, and explains why the differences affect how you work.
For now, remember the progression. Each generation solved a weakness of the previous one:
- Manual copies had no description and no coordination.
- Local tools added history but only for one person on one machine.
- Centralized tools added collaboration but depended on one server.
- Distributed tools removed the dependency on one server and made branching cheap.
Git belongs to the fourth group.
Common misconceptions
Several wrong ideas slow beginners down. Correcting them early saves effort.
- "Version control is only for programmers." Any set of files that changes over time can benefit, especially text files.
- "It is the same as a backup." A backup protects against loss. Version control records meaningful, described changes. The two overlap but are not the same, and you should keep both.
- "It is the same as a shared folder." A shared folder shows one current state and lets people overwrite each other. A VCS keeps every recorded state and flags conflicting edits.
- "I only need it for large projects." The habit is easier to build on small projects, and the first time you recover a deleted file it pays for itself.
- "Every save is a version." In most systems you decide when to record a state. A good recorded state is a coherent unit of work, such as "fix the login bug", not a snapshot taken every few seconds.
Habits that make history useful
A version control system only helps if the history is readable. Three habits matter from the start:
- Record small, focused changes. A change that does one thing is easy to review, compare, and undo.
- Write messages that explain why. The diff already shows what changed. The message should say the reason.
- Record working states. If you can, make sure the project runs before you record it, so any earlier state is a safe place to return to.
These habits cost little and make every later chapter easier. You will apply them as soon as you make your first commit.
Summary of the chapter
- A file system keeps only the current state. Version control keeps the history that produced it.
- Copying and renaming files does not scale, and it cannot record what changed or why.
- A VCS gives you history, restore and compare, attribution, parallel work, safe experiments, and an extra copy of your work.
- It works best with text files. Keep secrets and generated files out of the history.
- The approaches have evolved from manual copies to local, centralized, and distributed systems. Git is a distributed system.
Exercise
A teammate says, "We already have a shared network folder with daily backups, so we don't need version control." Give two things the shared folder with backups cannot do that a version control system can.
Show answer
Any two of these are correct. (1) It cannot show what changed between two versions or why, because a VCS stores described changes and can produce a diff, while backups only store whole copies at fixed times. (2) It cannot prevent people from silently overwriting each other's edits, because a VCS records each person's changes separately and flags conflicts. (3) It cannot support safe parallel experiments, because a VCS allows branches that are combined later. (4) It cannot restore to a specific meaningful point such as "before the login change", because backups are taken on a schedule rather than at the moments that matter.
Exercise
In the temperature example, the sign of one operator was changed by mistake. Describe how a version control system would help you find and fix the problem, compared with reading the whole file.
Show answer
You ask the system to compare the last good recorded state with the current files. The diff shows only the changed line, with + 32 replaced by - 32, so you see the mistake immediately. You can then edit the line back or restore the earlier version of the file from history. The saved message on the earlier state also tells you what that version was meant to do.
Exercise
Which of these should you keep out of a version control history, and why? (a) A Python script, (b) a file containing a database password, (c) the compiled output of a build, (d) a README file.
Show answer
Keep out (b) and (c). The password is a secret, and once it is recorded it can remain reachable in earlier states even if you delete it later. The compiled output can be recreated from the source, so storing it only adds size and noise. The script (a) and the README (d) are ordinary text and belong in the history.