Find a course

Module 3: Branching and MergingLesson 6 of 1211 min

Branches: Parallel Lines of Work

Understand branches as lightweight pointers, create and switch between them with git switch, and name them so your team knows what each one is for.

Step 1 of 14Learn

Experiment without fear

Lena's recipe site is live, built from the main branch. She wants to try a completely new layout, which will take a couple of weeks of evenings. Meanwhile, her brother keeps sending typo fixes that should go live right away.

If she edits the layout directly on main, the site is half-redesigned for two weeks, and the typo fixes are tangled up with unfinished work. If she copies the folder to recipes-redesign, she's back to the final_v2 problem.

A branch solves this. It's an independent line of development inside the same repository. Lena creates a branch called new-layout and does all the redesign work there. Whenever a typo fix arrives, she switches back to main, makes the fix, commits, and the live site updates. Then she switches back to new-layout and keeps going. When the redesign is finished, she merges it into main.

Branches are how nearly all teams work: every feature, bug fix or experiment gets its own branch, and main stays stable and deployable. If an experiment fails, you delete the branch and main never knew it existed.

Step 2 of 14Learn

What a branch really is

In many older systems, creating a branch meant copying every file. In Git, a branch is just a pointer: a tiny file containing the hash of one commit. Creating one takes milliseconds and almost no space, no matter how big the project is.

When you commit on a branch, Git creates the new commit with the current commit as its parent, then moves the branch pointer forward to the new commit. Other branches don't move.

Git also keeps a special pointer called HEAD, which marks where you are right now. Normally HEAD points at a branch name, such as main, and that branch points at a commit. When you switch branches, HEAD moves to the other branch, and Git updates the files in your working directory to match that branch's latest commit.

So after Lena creates new-layout and makes two commits on it:

A --- B --- C        main
             \
              D --- E   new-layout  <- HEAD

Commits A, B and C are shared history. D and E exist only on new-layout. If she switches to main, her files go back to how they looked at C, and the layout work is safely stored in D and E, waiting.

Step 3 of 14Diagram

A branch is a pointer

Creating a branch writes one small file. Committing moves the current branch's pointer to the new commit.

A branch is a pointerProcess diagram: Commit A → Commit B → Commit C <- main → Commit D <- new-layout (HEAD).Commit ACommit BCommit C <-mainCommit D <-new-layout(HEAD)

Tap a shape with to see more.

Commit A
Commit B
Commit C <- main

The main pointer is just a file holding this commit's hash.

Commit D <- new-layout (HEAD)

Committing on new-layout moved only that pointer forward. HEAD marks the branch you are on.

Step 4 of 14Practice · True or false

Creating a new branch in Git makes a full copy of every file in the project.

Step 5 of 14Learn

Branch commands

List branches; the current one is marked with *:

git branch

Create a branch and switch to it in one step:

git switch -c new-layout

Switch between existing branches:

git switch main

git switch new-layout

The new branch starts from whatever commit you're on, so switch to main first if you want to branch from the latest stable code.

You'll also see the older command git checkout, which does many jobs: git checkout -b new-layout creates and switches, and git checkout main switches. git switch (and git restore for files) were added in Git 2.23 in 2019 to split those jobs into clearer commands. Both work; this course uses the newer ones.

Other useful commands:

  • git branch -m new-name renames the current branch.
  • git branch -d new-layout deletes a branch that has been merged. Git refuses if it contains unmerged work.
  • git branch -D new-layout force-deletes it anyway, throwing away its unmerged commits.
  • git branch -a also lists branches on the remote, once you're connected to GitHub.

Step 6 of 14Tap to reveal

Lena's two weeks, step by step

  1. 1. Start the experiment

    git switch -c new-layout creates the branch at the current commit and moves HEAD to it.

  2. 2. Work on the layout

    Commits land on new-layout; main stays where it was and the live site is untouched.

  3. 3. A typo report arrives

    git switch main, fix, commit. The live site gets the fix immediately.

  4. 4. Back to the layout

    git switch new-layout. The files on disk change back to the layout work.

Step 7 of 14Practice · Match the pairs

Match each command to its effect.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. git switch -c fix-footer
  2. git switch main
  3. git branch
  4. git branch -d fix-footer
  5. git checkout -b fix-footer

Step 8 of 14Learn

Switching with uncommitted changes

What happens to edits you haven't committed when you switch branches? Git tries to carry them with you. If the files you changed are identical on both branches, the switch works and your uncommitted edits come along into the other branch.

If switching would overwrite your uncommitted changes, because the other branch has a different version of the same file, Git refuses:

error: Your local changes to the following files would be overwritten by checkout

This is Git protecting your work. You have three choices:

  1. Commit the changes on the current branch, if they belong there.
  2. Stash them with git stash, switch, do your work, come back and git stash pop.
  3. Discard them with git restore, if you don't want them.

The cleanest habit is to commit or stash before switching. Carrying uncommitted changes into another branch works, but it's an easy way to end up committing redesign code onto main by accident.

Run git status before switching. If it says working tree clean, the switch is always safe.

Step 9 of 14Practice · Diagram

Switching with uncommitted edits

Your edited file is different on the target branch. What happens?

Tap the shape that answers the question, then press Check.

Switching with uncommitted editsProcess diagram: Uncommitted edits → Same file differs on target? → No: edits come along → Yes: Git refuses.UncommittededitsSame filediffers ontarget?No: editscome alongYes: Gitrefuses
Uncommitted edits

Changes in your working directory that are not committed yet.

Same file differs on target?

Git checks whether the branch you switch to has a different version of the files you changed.

No: edits come along

The switch works and your edits follow you to the other branch.

Yes: Git refuses

Commit, git stash, or discard the edits, then switch.

Step 10 of 14Practice · Code challenge

A team names branches like feature/42-add-dark-mode: the word feature/, the issue number, a hyphen, then the issue title in lowercase with spaces replaced by hyphens. Write branchName(issue, title) to build that name. Remove any character that isn't a lowercase letter, a digit or a hyphen after the conversion.

Runs in a sandbox in your browser.
  • branchName(42, "Add dark mode")
  • branchName(7, "Fix login bug")
  • branchName(108, "Update README!")
  • branchName(5, "Support emoji, accents & symbols")

Step 11 of 14Note

Step 12 of 14Practice · What would you do?

What would you do?

You're on the new-layout branch with uncommitted edits to header.html that aren't ready. An urgent typo on the live site needs fixing on main, and Git refuses to switch because main has a different header.html. What's the best move?

Step 13 of 14Practice · Choose one

You are on main and run git switch -c add-search, then make two commits. Where does main point now?

Step 14 of 14Practice · Fill in the blanks

Fill in the blanks

To create a branch called fix-footer and switch to it in one command, run `git -c fix-footer`.