Branching — Git

A branch is a separate line of work. In Git, a branch is just a name that points at one commit, and HEAD (the "you are here" marker) points at the active…

Your website already works. Now you want to try a new idea for the navbar (the menu at the top of the page), and that idea might break everything. If you edit right away and the idea fails, you have to undo the changes one by one. There's a calmer way.

A branch is a separate line of work in your repo: you can try a new feature without disturbing the version that already works. It's like save slots in a game. Your save in the "main" slot has reached level 3, and you want to try a risky path. You make a new slot from the same point and keep playing there. The "main" slot stays at level 3, and if the experiment goes wrong, you just go back to it. In Git, each slot is one branch, and the last save in a slot is the newest commit on that branch.

A branch is a name that points at one commit

You already know each commit is one snapshot of your project. Inside Git, a branch is just a name that points at one commit: the newest commit on that line of work. Making a branch copies no files at all, so it's always instant, however big the project is.

Make a branch and switch to it

There are two ways:

# Way 1: two steps
git branch feature-navbar      # make a new branch, you're still on main
git switch feature-navbar      # move to feature-navbar

# Way 2: one step, make it and move to it at once
git switch -c feature-navbar

Here's what happens in the terminal, in a repo whose index.html holds <nav>Home</nav> and which already has its first commit:

$ git branch
* main
$ git switch -c feature-navbar
Switched to a new branch 'feature-navbar'
$ git branch
* feature-navbar
  main

git branch with no name lists every local branch (the branches on your computer), sorted alphabetically, with a * in front of the active branch. Say there are main, feature-a and feature-b, and you're on main. It prints feature-a, feature-b, then * main.

A commit only moves the active branch

Let's continue with that repo. On feature-navbar, you add a line to index.html (echo "<a>Products</a>" >> index.html), run git add index.html, then commit. In the table, C1 is the first commit, and C2 is this new commit. Before you read the table, guess: when C2 is made, does main move forward too, or stay where it is?

Step Command main at feature-navbar at HEAD
1 git commit -m "initial" C1 not yet main
2 git switch -c feature-navbar C1 C1 feature-navbar
3 git commit -m "add Products link" C1 C2 feature-navbar
4 git switch main C1 C2 main

That's the point of a branch: main stays the version that works, and your experiment grows on its own line until it's ready to be joined. How to join it is in the Merging lesson.

⚠️ Three ways making a branch goes wrong

TL;DR: A branch is a separate line of work. In Git, a branch is just a name that points at one commit, and HEAD (the "you are here" marker) points at the active branch, where the next commit goes. git branch lists the branches (alphabetical, * = active branch). git branch name makes a branch without moving. git switch name (or git checkout name) moves to that branch. git switch -c name (or git checkout -b name) makes a branch and moves to it at the same time. A commit only moves the active branch, and the others stay in place. The main branch is usually main (master in older repos), and it only exists after the first commit.

⚡ A branch is a separate line of work

Meaning: A branch is a separate line of work, for trying an idea without disturbing the version that works. In Git, a branch is just a name that points at one commit, the newest commit on that line. Making a branch copies no files, so it's always instant. The main branch is main, and it only exists after the first commit.

⚡ Make it and move to it

Write:

git switch -c feature-navbar

Meaning: git switch moves you to a branch. The option -c (short for create) makes the branch first, so this command makes feature-navbar and moves you to it at the same time. The older command is git checkout -b feature-navbar. With just git branch feature-navbar, the branch is made but you haven't moved.

⚡ A commit only moves the active branch

Guess first: you make a commit on feature-navbar. Does main move forward too?

No. Only the branch that HEAD (the "you are here" marker) points at moves forward, and that is feature-navbar. main stays on its old commit, and when you run git switch main, your folder goes back to that commit's snapshot.

Meaning: A new commit only moves the active branch, the branch HEAD points at. The other branches stay in place. Your change isn't lost: it's safe on feature-navbar, waiting to be joined into main.

⚡ Check where you are

Write:

git branch

Meaning: git branch lists every local branch (the branches on your computer), in alphabetical order, with a * in front of the active branch, the branch you're on.

What you will learn