SkillGridLearn computer science by doing

Git: undo, history, and branches

Version control is a save point that never forgets why. It lets you undo a change weeks later, and it answers the question every bug hunt starts with: which change broke this?

13 min · 42 XP

You changed one line. One line. And now the whole site is broken: the prices are wrong, the buttons do nothing, and you cannot remember what that line looked like before. This is the single most common moment in programming, and it has a proper answer. It is not a better memory and it is not Ctrl+Z. It is version control.

The simple version of undo is Ctrl+Z. It works for about a minute, then it runs out: you close the editor, or you save over the file, or you keep going and the undo history is gone. You have also probably made a copy at some point. Be honest about what those copies look like: site-final.zip, site-final-2.zip, site-final-real.zip. Copies do not explain anything. A version control system records every change together with who made it, when, and what they were trying to do. Weeks later you can still ask exactly what that line said, and why it was changed.

A commit is a snapshot of the whole project, saved together with a short message. It is not a difference and not a copy of one file: it is the state of everything at that moment. Each commit points back to the commit before it, so the commits form a chain — this is what people mean by the history of a project.

9f1c2ab
   |
a17d3e0
   |
64b8f21
   |
  (start)

$ git diff 64b8f21 a17d3e0
--- a/cart.js
+++ b/cart.js
-  return net;
+  return net * (1 + VAT);
mainexperimentgit switch -c experimenttry the new idea, committhe site goes on working heregit switch maintwo lines of history, one project folder

A branch is a separate line of history that starts from the one you are on. The point is that it is cheap: creating one takes a moment and costs almost nothing, so you can try an idea without betting the working site on it. If the idea works, you merge the branch back into the main line. If it does not, you throw the branch away and nothing that worked has changed. In a team, this is how several people work at the same time without stepping on each other: everybody has their own line and the main line stays in a state that can be released.

A merge conflict happens when two branches changed the same lines. Git refuses to guess and puts both versions in the file, marked with a separator:

<<<<<<< HEAD
price = 19.00
=======
price = 21.00
>>>>>>> experiment

Practice

  1. Put the honest path from “this is broken” back to “this is fixed” in order.

    Hints
    • You cannot undo what was never recorded, so everything starts from the save.
    • The users get a working site first; writing the proper fix and telling the others about it comes after.

    15 XP

  2. Match each everyday problem with the thing that actually solves it.

    Hints
    • A branch only helps if the main line keeps working while you experiment — that is the whole point of having two lines.
    • A conflict is information: it tells you two people touched the same line, and only a person can decide which version is right.

    13 XP

  3. You are about to save a change. Which commit message is actually useful to someone reading the history in three months?

    Hints
    • Ask what the next person needs in order to understand this change without opening the code.
    • A useful message says what changed and why; the code already shows how.

    14 XP