Contents Lesson 15 of 21

9 min read · foundations

Branches — changing things without risking what works

By the end of this lesson you will have made a branch, changed something you would not dare change on main, merged it back — and thrown a second branch away unmerged.

You can commit. That means every saved moment is recoverable. Branches are the next thing: a way to try something large without the working version and the experiment ever being the same thing.

What a branch actually is

main is your line of commits. A branch is a second name pointing at the same moment, which then moves independently as you commit.

That is genuinely all it is. Not a copy of your files, not a separate folder. A name that moves.

git switch -c try-sorting     # new name here, and go to it
# ...change things, commit, change more...
git switch main               # back — your files are as they were

If git switch main answers fatal: invalid reference: main, your main line is called master (some git installs still name it that way). Check with git branch; rename it once with git branch -m main and carry on. Lesson 4 does this right after git init, so if you followed it you will not see the message.

Switching back does not undo your work. The commits are still there under the other name, waiting.

Why this matters before Build Your First Tool

In Build Your First Tool an assistant writes most of the code. You will ask for something substantial and get thirty lines back across four files. Two things can then be true at once: the change is interesting, and you are not sure about it.

On main, that is a decision under pressure — accept and hope, or refuse and lose the work. On a branch it is not a decision at all. Accept it, run it, read it, live with it for ten minutes. If it is good, merge it. If it is not, switch away and delete the branch, and nothing you cared about was ever at risk.

git switch main
git merge try-sorting         # bring it in
git branch -d try-sorting     # done with the name

Or, if it was not good:

git switch main
git branch -D try-sorting     # capital D — throw it away unmerged

Branches are cheap, and that is the point

Making one costs nothing and takes a second. Delete them without ceremony. A branch is not a commitment to finish something; it is permission to try.

A useful habit: before you ask an assistant for anything bigger than a one-line change, make a branch. It costs a second and it changes the question from "is this safe?" to "is this good?" — and only the second question is worth your attention.

When two changes touch the same line

If main moved while you were on your branch and both changed the same line, merging stops and shows you both versions with markers. Git is not confused and nothing is lost. It is telling you that this one is a judgement call, because only you know which version is right.

You will meet this properly in Ship Your Tool. For now, knowing it is a question rather than a disaster is enough.

Try it now

On the small program from unit 2, make a branch and change something you would not dare change on main — rename every variable, restructure a function. Commit it. Switch to main and confirm your files are back as they were. Then merge it in. Then do it once more, and throw the branch away instead.

The prompt

When both branches are done, run git log --oneline --graph --all and paste what it printed into your chat assistant:

I am learning git branches. Below is what git log --oneline --graph
--all printed after I merged one branch into main and deleted another
without merging it. Tell me, line by line, which commits are on main,
which one came in from the merged branch, and whether anything from
the deleted branch is still listed. Do not suggest commands.

[paste the output]

A good answer finds the merged commit on main, often with no separate merge commit at all, and says the thrown-away branch's commit is no longer listed.

Check it yourself

  1. On the branch, git branch lists both names, with * beside the one you are on.
  2. After git switch main, the file is back as it was, and git log --oneline does not show the branch's commit.
  3. After git merge, git log --oneline on main does show it.
  4. After git branch -D, git branch lists main alone, and the experiment is gone from git log --oneline --all.

Compare with ours

Our session, output shortened
$ git switch -c try-renames
$ git add -A
$ git commit -m "Rename the loop variables in changes.js to say what they hold"
$ git switch main
$ git merge try-renames
Fast-forward
$ git branch -d try-renames
$ git switch -c try-rewrite
$ git add -A
$ git commit -m "Rewrite the loop as a map"
$ git switch main
$ git branch -D try-rewrite
Deleted branch try-rewrite (was 4e1d2c9).

Fast-forward means main had not moved since you branched, so git moved the name forward to your commit and needed no merge commit.

Checkpoint

The commit you made on the first branch is the one that stays; the merge brought it to main. Check with git log --oneline: it is listed on main, and git status says there is nothing to commit. If you did not give that commit a clear message, you will find it there anyway; the branch you threw away left nothing behind.

Then answer this lesson's questions.