What Version Control Is and Why It Matters
See why folders of final and final2 copies fail, and watch Git keep a complete history of one file without any clutter.
In this lesson you will
- Explain why saved copies do not scale
- Say what version control records
- Tell Git and GitHub apart
- Use the words repository, commit and history
About 20 minutes
Which file is the real one?
Nia looks after the planting plan for her community garden. Every time she changes it, she saves a copy first, “just in case”. Last week Dev asked, “Is this the latest plan?” and Nia honestly could not say. There was plan_final, plan_final_REAL, and a plan_v2 that might have been newer. She spent twenty minutes opening files before she found the one with the carrots in it. If you have ever had a folder like that, you already understand the problem this whole course solves.
The problem with saving copies
Here is Nia’s habit, played out for real in a terminal. She starts with one file, and after each change she copies it to a new name. After only three changes, her folder looks like this:
$ mkdir garden-copies
$ cd garden-copies
$ echo "Beans: row 1" > plan.txt
$ cp plan.txt plan_v2.txt
$ echo "Carrots: row 2" >> plan_v2.txt
$ cp plan_v2.txt plan_final.txt
$ echo "Beans moved to row 3" >> plan_final.txt
$ cp plan_final.txt plan_final_REAL.txt
$ ls
plan.txt
plan_final.txt
plan_final_REAL.txt
plan_v2.txt
$ ls | wc -l
4
That is 4 files for one plan, and the names tell you nothing. Which is newest? What changed between them? Who changed it? Why? The folder cannot answer, and it grows every week. Worse, when two people each save their own copy, one person’s work can quietly overwrite the other’s. Copies are a memory aid for one careful person. They break down for anyone else.
What version control does instead
Version control is a tool that records the history of your files. Each time you reach a good point, you save a labelled snapshot of the project, together with who made it, when, and a short message explaining why. Nothing is overwritten. The old versions stay safe inside the tool, and you can look at them, compare them, or bring one back whenever you like. Your working folder keeps just one copy of each file, the current one, so it stays tidy.
Git is the most widely used version control tool. It is free, it runs on your own computer, and it works without an internet connection. GitHub is a different thing: a website that stores copies of Git projects online, so you can back them up, share them and work with other people. There are other websites that do the same job, and Git works perfectly well without any of them. This course teaches Git first, because everything on GitHub is built on top of it.
The same project, tracked by Git
Now watch the same three changes with Git. Do not worry about understanding every command yet. Lessons 2 to 4 take them one at a time. Just notice the result. The commands that start with git commit each save one snapshot, and the -m part gives it a message.
$ cd ..
$ mkdir garden
$ cd garden
$ git init
Initialized empty Git repository in /home/nia/garden/.git/
$ echo "Beans: row 1" > plan.txt
$ git add plan.txt
$ git commit -m "Add planting plan"
[main (root-commit) d46d7e7] Add planting plan
1 file changed, 1 insertion(+)
create mode 100644 plan.txt
$ echo "Carrots: row 2" >> plan.txt
$ git commit -am "Add carrots in row 2"
[main 5f0a18f] Add carrots in row 2
1 file changed, 1 insertion(+)
$ echo "Beans moved to row 3" >> plan.txt
$ git commit -am "Note that beans moved"
[main d638397] Note that beans moved
1 file changed, 1 insertion(+)
$ ls
plan.txt
$ git log --oneline
d638397 Note that beans moved
5f0a18f Add carrots in row 2
d46d7e7 Add planting plan
$ git show HEAD~2:plan.txt
Beans: row 1
The folder holds exactly one file, plan.txt, yet the log lists 3 saved versions, newest first, each with a message. The last command reached back two versions and printed the very first plan, which was a single line: Beans: row 1. That is the whole idea. One tidy folder, a complete history, and nothing lost. The short codes at the start of each log line are commit IDs. Yours will be different from the ones above, because Git builds each ID from the content, the author and the time, and that is fine.
The little -am in two of the commits is a shortcut that saves changes to files Git already knows about, and the next lessons explain it properly. It is used here only to keep the demonstration short.
Three words to learn today
- Repository (or “repo”): a project folder that Git is watching, together with its saved history.
- Commit: one saved snapshot in the history, with an author, a time and a message.
- History: the chain of commits, from the first to the latest.
Git works best with text files such as notes, web pages and code, because it can show you exactly which lines changed. It can store pictures and other binary files too, but it cannot show a friendly comparison of them, and large files make a project heavy. For a first project, text files are the perfect choice.
What version control is not
A repository on your own laptop is a history, but it is not a backup. If the laptop is lost, the history goes with it. Putting a copy on another computer or a website such as GitHub is what protects you, and lesson 9 shows how. Version control is also not a way to avoid talking to your team. It records who changed what, but people still need to agree on what the plan should be.
Thinking you need to understand all of Git before you begin. Beginners who learn four commands well, and use them every day, do better than people who read about forty and use none.
Practice for this lesson
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What is the main problem with saving copies called final and final2?
- Copies take no disk space
- The names do not say which is newest, what changed or why
- Computers cannot open copies
- Copies delete the original
The folder had four files and no way to tell which was the real plan.
What is Git?
- A website that hosts projects
- A text editor
- A kind of backup disk
- A free tool on your computer that records the history of your files
GitHub is the website. Git is the tool that runs locally.
What is a commit?
- A saved snapshot with an author, a time and a message
- A copy of the whole internet
- A branch name
- A password
The log listed three commits, each with a message.
After three commits, how many plan files were in the working folder?
- Three
- Four
- One, because Git keeps the old versions in its history
- None
The folder showed only plan.txt while the log held all three versions.
Is a repository on your laptop a backup?
- Yes, it is a perfect backup
- Yes, but only on Windows
- Only if it has many files
- No, it is a history, and a copy elsewhere protects against loss
If the laptop is lost, a local repository is lost with it.
Summary
- Copies of a file hide which version is newest, what changed and why.
- Git records snapshots with an author, a time and a message.
- Git is the tool on your computer; GitHub is a website that hosts projects.
- A local repository is a history, not a backup.
Questions
Do I need GitHub to use Git?
No. Git works entirely on your own computer. GitHub is useful for sharing and backing up, and you will add it in lesson 9.
Can Git handle things other than code?
Yes. Any plain text works well, including notes, essays, recipes and web pages. Pictures and other binary files can be stored, but Git cannot show a friendly comparison of them.
Will my commit IDs match the ones in the lesson?
No. Git builds each ID from the content, the author and the time, so yours will differ. That is normal and does not mean anything went wrong.
