Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The Programming Shortcut: Version control

Part of The Programming Shortcut, a series that teaches one programming idea per article to people who already program. This one is about version control: saving, comparing and undoing your work.

You already do version control, badly

Somewhere on your computer there is a file called report_final.docx. Next to it is report_final_v2.docx, and next to that, if things went the way they usually go, report_final_v2_REALLY.docx.

You’ve done the programming version too. You copied main.py to main_old.py before trying something risky. You commented out a block of code instead of deleting it, “just in case.” You pressed undo forty times in a row, hoping to land on the version that worked an hour ago, and then the editor crashed and took the undo history with it.

Every one of those is version control. You were saving versions of your work so you could compare them and go back. You just did it by hand, with filenames and hope.

A version control system is a program that does this for you, properly. It keeps every version of your project you ask it to keep, it labels each one with who made it, when and why, and it can tell you what changed between any two of them, down to the line. It can also take you back, and it does that without throwing the present away.

That’s the concept this piece teaches, in three verbs: saving, comparing and undoing. Every version control system you’ll ever meet does those three things, and once you can see them in one, you’ll recognize them in the rest.

The one we’ll use is git. It was born in 2005, when the people building the Linux kernel, led by Linus Torvalds, needed a tool that could keep thousands of programmers from trampling each other’s work. That sounds like a tool for someone else. But the same tool works for one person, in one folder, on one laptop, with no server, no account and no internet connection. Everything in this piece happens on your own machine.

One folder and one hidden drawer

First, check that git is there. Open a terminal and type:

$ git --version
git version 2.47.3

Any recent version is fine. If the command isn’t found, install git from git-scm.com/install; on Debian or Ubuntu, that’s sudo apt install git. On Windows, the installer gives you Git Bash, a terminal where every command in this piece works as written.

Git signs your name on everything you save, so it needs to know your name once. These two lines go in a settings file in your home folder, and you never type them again:

$ git config --global user.name "Your Name"
$ git config --global user.email "you@example.com"

Now make a folder to play in, and turn it into a repository. A repository (or repo) is a folder whose history git keeps:

$ mkdir ideas
$ cd ideas
$ git init
Initialized empty Git repository in /home/you/ideas/.git/

Git may also print a few lines of hints about the name master. You can ignore them for this piece.

That .git folder is the hidden drawer. It’s where every saved version of your project will live. Your own files stay where they are, and look like normal files, because they are. Delete the .git folder and you’re back to an ordinary folder with no history at all. Your history lives nowhere else.

Make a file called ideas.txt in any editor, with three lines:

Things I could build:
- a dice roller
- a to-do list

Then ask git what it sees:

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	ideas.txt

Untracked means git can see the file but hasn’t been asked to look after it. Two commands change that. git add puts the file into the next save, and git commit makes the save:

$ git add ideas.txt
$ git commit -m "Start a list of ideas"
[master (root-commit) 0f5e4dc] Start a list of ideas
 1 file changed, 3 insertions(+)
 create mode 100644 ideas.txt

That’s your first commit: one saved version of the project, with a message saying what it was for. The -m gives the message right there on the command line. Leave it off, and git opens an editor for you to write one.

Now change something. Edit the file: change the to-do list line and add a fourth idea.

Things I could build:
- a dice roller
- a to-do list that nags
- a word counter

Before you save it into history, look at what you did:

$ git diff
diff --git a/ideas.txt b/ideas.txt
index 5e4b096..54d89b6 100644
--- a/ideas.txt
+++ b/ideas.txt
@@ -1,3 +1,4 @@
 Things I could build:
 - a dice roller
-- a to-do list
+- a to-do list that nags
+- a word counter

A line starting with - was there before and isn’t now. A line starting with + is new. A line starting with a space didn’t change; git shows a few of those as context, so you can see where you are. Changing a line shows up as one line out and one line in. The @@ -1,3 +1,4 @@ line says where you are in the file: three lines starting at line 1 before, four lines starting at line 1 after.

Happy with it? Save it:

$ git add ideas.txt
$ git commit -m "Add a word counter"
[master b676d88] Add a word counter
 1 file changed, 2 insertions(+), 1 deletion(-)

Everything that follows comes back to those steps: edit, look with git diff, then git add and git commit. The editor’s save button saves your file. A commit saves a version of your project. You’ll press the first one constantly, and the second one whenever you reach a point you’d like to be able to come back to.

A commit is a photograph of everything

Ask git for its history:

$ git log
commit b676d881b8fec963a723263a376ebd11eb015a80
Author: Your Name <you@example.com>
Date:   Sun Nov 22 15:34:02 2026 -0600

    Add a word counter

commit 0f5e4dc48c7c4c4795fd4c5dc354b6e11575d09a
Author: Your Name <you@example.com>
Date:   Sun Nov 22 15:34:02 2026 -0600

    Start a list of ideas

Newest first. Each commit carries four things, and between them they are the concept.

A snapshot. A commit isn’t a list of changes. It’s a complete picture of every file git is looking after, as it was at that moment. If your project had fifty files, each commit holds all fifty. (Git is clever about not storing the same file twice, so this costs far less disk than it sounds.) The diffs you just saw are something git works out by comparing two snapshots, when you ask.

A message, an author and a time. Who saved this, when, and why. The “why” is the one only you can supply, and it’s the one future you will want most.

A parent. Every commit except the first remembers the commit that came before it. That’s how a pile of snapshots becomes a history: each one points back to its parent, and the chain runs all the way to the first commit, which has no parent at all. Git calls that one the root commit, which is why the first git commit printed root-commit.

A name. That long string of letters and numbers is the commit’s hash: a fingerprint git computes from everything in the commit, including the snapshot, the message, the author, the time and the parent. Change one character of any of them and you get a different fingerprint. You’ll rarely type all forty characters. The first seven or so are enough, and git log --oneline shows only those:

$ git log --oneline
b676d88 Add a word counter
0f5e4dc Start a list of ideas

Your hashes won’t match mine, because yours include your name and the moment you committed. Wherever this piece uses a hash, use one from your own log.

Because the parent is part of what gets fingerprinted, a hash names the commit and everything before it. Change anything in an old commit and every commit after it gets a new name too. That’s how git can promise that the history you’re looking at is the history that happened.

Two more names come up constantly. HEAD means “the commit you’re on right now,” which is usually the newest one. HEAD~1 means one step back from there, HEAD~2 two steps back, and so on.

Three places your work can be

There’s one wrinkle in those steps, and it’s git add. Why not commit straight away?

Because git keeps your work in three places, and it helps to picture them:

  your folder          the staging area          the history
  (what you edit)  --> (the next snapshot)  -->  (every commit)
                 git add                   git commit

The staging area (git also calls it the index) is the next snapshot, still being packed. git add copies a file’s current state into it. git commit takes whatever’s packed and makes it permanent. That gap lets you choose. If you fixed a bug and also tidied up some spacing, you can add and commit the fix on its own, then the tidying, and each commit says one thing.

git diff with nothing after it compares your folder with the staging area. So once you git add a file, plain git diff goes quiet. To see what’s packed and about to be committed, ask for the staged diff:

$ git diff --staged

Not every version control system has a staging area. Some commit everything that changed, every time. But nearly all of them have snapshots, messages and parents, and when you meet a new one, those are the three things to look for. Even the version history in a shared online document is the same idea in disguise: snapshots, each with an author and a time, in a line that points back to the start.

Comparing: what changed, and when?

A history you can’t compare is just a pile of old copies. The report_final_v2 approach fails here: two files, and no way to see what’s different between them except reading both.

You’ve already used the most common comparison, git diff, for “what have I done since my last save?” The same command compares any two commits. Name them, older first:

$ git diff HEAD~1 HEAD

That shows what the most recent commit changed. You can use hashes in place of HEAD~1 and HEAD, from any distance apart, and git will show everything that changed between those two snapshots, however many commits lie in between.

To see a single commit, with its message and its changes together, use git show:

$ git show b676d88

And to read the history as a story, every commit with its changes underneath, add -p (for patch, an old name for a diff) to git log:

$ git log -p

That output is usually longer than one screen, so git shows it a page at a time. Press the space bar for the next page and q to quit.

Sometimes you don’t want a comparison at all, just a look at how a file used to be. git show can do that too, with a colon between the commit and the file:

$ git show HEAD~1:ideas.txt
Things I could build:
- a dice roller
- a to-do list

Nothing in your folder changed. You just looked into the drawer.

Here’s the habit that makes all of this pay off: commit small, and say why. A commit that does one thing has a diff you can read in ten seconds. A commit called “stuff” that touched thirty files has a diff nobody will ever read, including you.

Undoing without forgetting

Undo in an editor walks backwards and forgets. Press it ten times, type one character, and those ten steps are gone for good. Version control undoes things a different way, and the difference is the most useful idea in this piece.

There are three kinds of mistake, and they get undone at three different places.

An edit you haven’t committed yet. You changed a file, it went badly, and you want the last saved version back. git restore copies it from the staging area over your file:

$ echo "- delete everything" >> ideas.txt
$ git restore ideas.txt

Your bad edit is gone, and the file matches your last commit again. If you’d already run git add on the bad edit, take it back out of the staging area first with git restore --staged ideas.txt, then restore the file.

A file you want from further back. Maybe the version you want is one commit back. Tell git restore where to get it from:

$ git restore --source=HEAD~1 ideas.txt

Now your folder holds the older version of that one file, and git diff will show you the difference from where you were. If you like it, add and commit it like any other change. If you don’t, git restore ideas.txt puts you back. Do that now, because the next command wants a folder with no uncommitted changes.

A commit you wish you hadn’t made. This is where version control is cleverest. You might expect it to delete the bad commit. It doesn’t. git revert works out the opposite of that commit’s changes, applies them, and saves the result as a new commit:

$ git revert --no-edit HEAD
[master 1972de8] Revert "Add a word counter"
 Date: Sun Nov 22 15:34:14 2026 -0600
 1 file changed, 1 insertion(+), 2 deletions(-)
$ git log --oneline
1972de8 Revert "Add a word counter"
b676d88 Add a word counter
0f5e4dc Start a list of ideas

(Without --no-edit, git opens an editor so you can write your own message.)

Look at that log. The mistake is still there. So is the undo, with a name and a date. Your file is back the way it was, and the history has grown by one.

Banks have kept books this way for centuries. When an accountant finds a wrong entry in a ledger, they don’t scratch it out. They add a new entry that cancels it. The books always add up, and you can always see what happened and when it was put right. Version control works on the same rule: history only grows. Undoing is something you do, so it becomes part of the record.

That’s the answer to report_final_v2_REALLY. You never needed a copy of every version with a cleverer name. You needed one file, a record of every version it has been, and a way to say “put this back” that doesn’t destroy the evidence.

Saved isn’t committed

Most of the software you use makes a simple promise: when you press save, your work is safe. Git makes a narrower one. It protects what you have committed, and nothing else.

Edits you haven’t committed are just files in a folder. git restore overwrites them without asking, and nothing in the drawer remembers them, because they never went in. That’s why the habit matters: commit early, commit often, and the worst a mistake can cost you is the work since your last commit.

And the ledger has an eraser, if you go looking for it. Git has commands that rewrite history: that move you back to an earlier commit as if later ones never happened, rewrite the last commit, or replace a shared history with yours. They exist for good reasons, mostly to tidy up commits before anyone else sees them. But they give up the promise that history only grows.

Beware of git reset --hard. It turns up in a lot of answers online, because it’s a quick way to make a messy folder match the last commit. It does that by throwing away every uncommitted change, in every file, at once, with no confirmation and no undo. My advice: leave it alone until you can say, without looking it up, what it will destroy. To throw away an edit, git restore the one file you meant. To undo a commit, git revert it. Both take longer to type, and both only do what you asked.

Who put twenty eggs in the pancakes?

The project is a kitchen with a history, and a mystery in it.

It’s one script, story.sh, in projects/version-control/. It makes a throwaway repository in a fresh temporary folder (your own folders aren’t touched) and fills it with five commits of a pancake recipe, written by someone called Sam. Here it is in full, apart from the “Try this next…” hints at the end of the file:

set -e

repo=$(mktemp -d)
cd "$repo"
git init --quiet --initial-branch=main
git config user.name "Sam Baker"
git config user.email "sam@example.com"

save() {
    git add pancakes.txt
    GIT_AUTHOR_DATE="$1" GIT_COMMITTER_DATE="$1" git commit --quiet -m "$2"
}

cat > pancakes.txt <<'RECIPE'
Pancakes

1 cup flour
1 tablespoon sugar
2 teaspoons baking powder
a pinch of salt
1 cup milk
2 eggs
2 tablespoons melted butter
RECIPE
save "2026-11-02 08:00 +0000" "Start a pancake recipe"

cat >> pancakes.txt <<'RECIPE'

Mix the dry things.
Beat the milk, eggs and butter, and pour them in.
Stir until just combined. Lumps are fine.
Cook on a hot pan until bubbles pop, then flip.
RECIPE
save "2026-11-03 08:00 +0000" "Add the method"

sed -i.bak -e 's/^2 eggs$/20 eggs/' -e 's/^Mix the dry things\.$/Whisk the dry ingredients together./' pancakes.txt
rm pancakes.txt.bak
save "2026-11-05 08:00 +0000" "Tidy up wording"

cat >> pancakes.txt <<'RECIPE'

Serve with butter and maple syrup.
RECIPE
save "2026-11-08 08:00 +0000" "Add a serving suggestion"

sed -i.bak -e 's/^Pancakes$/Pancakes (serves 4)/' pancakes.txt
rm pancakes.txt.bak
save "2026-11-09 08:00 +0000" "Say how many it serves"

echo "A kitchen with some history is ready. Go there with:"
echo
echo "    cd $repo"

The git config lines without --global set Sam’s name for this one repository only, so your own settings stay as they are. Run the script, then go where it tells you:

$ bash story.sh
A kitchen with some history is ready. Go there with:

    cd /tmp/tmp.iRfmAG0r3R
$ cd /tmp/tmp.iRfmAG0r3R

Your folder name will be different. Look at the recipe:

$ cat pancakes.txt

Twenty eggs. Something went wrong, and you weren’t there. Start with the history:

$ git log --oneline
b8cdae8 Say how many it serves
6a6a0ff Add a serving suggestion
351a59c Tidy up wording
f4d546c Add the method
37907a1 Start a pancake recipe

None of those messages says “eggs.” That’s realistic. Nobody writes “made the recipe wrong” in a commit message. So read the changes instead of the labels:

$ git log -p

Page down until you find it. (While git is showing a page, pressing / and typing eggs searches for it.) Here’s the commit you’re looking for:

commit 351a59cd6251501e855780cf053050543c641886
Author: Sam Baker <sam@example.com>
Date:   Thu Nov 5 08:00:00 2026 +0000

    Tidy up wording

diff --git a/pancakes.txt b/pancakes.txt
index f116744..d44bc5f 100644
--- a/pancakes.txt
+++ b/pancakes.txt
@@ -5,10 +5,10 @@ Pancakes
 2 teaspoons baking powder
 a pinch of salt
 1 cup milk
-2 eggs
+20 eggs
 2 tablespoons melted butter
 
-Mix the dry things.
+Whisk the dry ingredients together.
 Beat the milk, eggs and butter, and pour them in.
 Stir until just combined. Lumps are fine.
 Cook on a hot pan until bubbles pop, then flip.

“Tidy up wording,” it says, and it slipped an extra zero onto the eggs. It also made a change that was a good idea: whisk is better than mix. Keep that in mind.

Undo the commit. Use the hash from your own log:

$ git revert --no-edit 351a59c
Auto-merging pancakes.txt
[main b73041a] Revert "Tidy up wording"
 Date: Sun Nov 22 15:34:30 2026 -0600
 1 file changed, 2 insertions(+), 2 deletions(-)

Two commits came after the bad one, and git kept both: the recipe still serves 4, and the maple syrup is still there. Revert takes out one commit’s changes and leaves everything later alone. Check what your new commit did:

$ git diff HEAD~1 HEAD

Two eggs again. But whisk went back to mix as well, because a revert undoes a whole commit, good changes and bad. That’s the price of a commit that did two things, and it’s the clearest argument for small commits you’ll ever get from a pancake.

So finish the job the ordinary way. Open pancakes.txt in your editor, change “Mix the dry things.” back to “Whisk the dry ingredients together.”, and save the file. Then look, and commit:

$ git diff
$ git add pancakes.txt
$ git commit -m "Whisk, don't mix"

Each of those prints something of its own. Then the log:

$ git log --oneline
0e2d15f Whisk, don't mix
b73041a Revert "Tidy up wording"
b8cdae8 Say how many it serves
6a6a0ff Add a serving suggestion
351a59c Tidy up wording
f4d546c Add the method
37907a1 Start a pancake recipe

Your top two hashes will differ from these, because they include the time you made them. Sam’s five should match, because the script fixes everything those five fingerprints are made from: the recipe, the messages, Sam’s name and the dates.

Read that log from the bottom up and it tells the story: the recipe, the slip, the fix, and the good idea put back. Nothing was erased. Anyone who reads it later can see what happened, in what order, and why.

When you’re done, the temporary folder can go: rm -rf the path the script printed, or leave it for your system to clear away. The story.sh in the project folder ends with more things to try in that kitchen before you do.

What I left out

This piece left out most of git, and it’s meant to.

There are no branches here: lines of history that split off so you can try something without disturbing the main line, and merging, which brings them back together. There are no remotes, the copies of a repository on another machine or a service like GitHub, with the clone, push and pull that move commits between them. That’s how teams share history, and it’s built on top of everything above. I also skipped rewriting history (rebase, amend), setting work aside for later (stash), telling git to ignore files (.gitignore), and what’s inside the .git folder.

Every one of those is built from the three things you now have: snapshots with parents, comparison between snapshots, and undo by adding a new snapshot. A branch is a name pointing at a commit. A merge is a commit with two parents.

When you want the rest, the official book is free: Pro Git, by Scott Chacon and Ben Straub. Git also ships with a short guide to the commands people use every day: run git help everyday.

Your own history

Find a project you’ve been working on for a while. If it isn’t in git yet, go into its folder and run git init, then git add . and git commit -m "Where things stand", and come back in a week.

If it is in git already, run:

$ git log -p

and pick one line of code you wrote. Bring this question: when did this line become what it is, and what did I think I was doing? The answer is in there, with a date on it.

  • Testing: commit tests with the code they check, and the history can tell you which commit made a test start failing.
  • Debugging: finding out what a program is doing. Knowing when a bug arrived, and which diff brought it, often finds it faster than any debugger.

Tested with git 2.47, November 2026.