Lesson 1 of 7 in Branching & Merging

Branches

Story: Before you split a real timeline, Juno wants you practising on labels.

A branch is a name for a line of work. Under the hood it's tiny: a label that points at one commit. Here, every new repository starts on a branch called main, today's usual name for the first branch. On your own computer git may still call it master, as Basics › A new repository explains.

HEAD tells git which branch you're on. When you commit, the branch you're on moves forward to the new commit, and every other branch stays where it was. That's what lets you try something on one branch while main stays untouched.

Command What it does
git branch Lists the branches. A * marks the one you're on.
git branch <name> Creates a branch on the commit you're on. You stay where you are.
git switch <name> Switches to a branch: HEAD moves to it, and your files change to match its commit.
git switch -c <name> Creates a branch and switches to it, in one step.
git branch -m <old> <new> Renames a branch. With one name, it renames the branch you're on.
git branch -d <name> Deletes a branch, but only if its commits are already in the branch you're on.
git branch -D <name> Deletes a branch even if its commits are on no other branch.

Creating, renaming or deleting a branch never changes a commit. Only the labels move, and the Live Graph shows them moving.

Git won't delete the branch you're on: switch to another one first. And -D really does throw away the label. A commit that was only on that branch is then on no branch at all, and the Live Graph shows it faded.

Switching with uncommitted changes

Changes you haven't committed yet don't belong to any branch: they're just in your files. When you switch, they come along. git switch -c <name> always takes them with you, because the new branch starts on the commit you're on. So if you started some work on main that belongs on its own branch, make the branch, then commit there: main stays as it was.

Switching to an existing branch takes them along too, unless that branch has a different version of a file you changed. Then git refuses to switch rather than overwrite your work, and tells you which files are in the way: commit them first.

New commands

  • git branch
  • git branch -d
  • git branch -D
  • git branch -m
  • git switch
  • git switch -c

Explainer: A branch is a movable label

project: your repo, you are here37d7cf7 Add a README37d7cf7ab89288 Add the home pageab89288a4b8d97 Style the headinga4b8d970ef3377 Add a contact page (unreachable)0ef3377HEAD → main
The repository at the end of the Explainer.
Graph as text

project: your repo, you are here

  • HEAD → main at a4b8d97 "Style the heading"

Branches and tags

  • main at a4b8d97 "Style the heading"

Commits, newest first

  1. a4b8d97 "Style the heading"; parent ab89288; refs: HEAD → main
  2. ab89288 "Add the home page"; parent 37d7cf7
  3. 37d7cf7 "Add a README"; no parent

Unreachable

  • 0ef3377 "Add a contact page"; parent a4b8d97

Challenge 1 of 4

Story: New timeline, new label. Open one for the contact page.

Create a branch called contact-page and switch to it.

Unlocks after Basics.

Terminal

Unlocks after Basics.

This Lesson can be played once Basics is complete. Its text and Explainer are open to everyone.