Stop 09 · Git & GitHubTopics · 5/9

Branches

Part of the Web Development Roadmap

In short

Want to try a new feature without breaking the version that works? Make a branch. It's a safe side track where you can experiment freely.

Branches let you work on a feature or fix separately from the main code, so unfinished work never breaks what already works.

The jaadu mascot pulls a junction lever as a toy train moves onto a side track.

Think of it like this

Railway tracks splitting at a junction

The main line keeps running on time. At a junction, one train goes onto a side track to test a new route. If the route works, it joins the main line later. If not, the main line was never disturbed.

Watch it happen

Two lines of work

c1 Initial
c2 Add navbar
c3 Fix footer
d1 Dark toggle
d2 Dark colours

1/5main has two commits. You're about to start dark mode.

The big ideas

What you need to know

A branch is just a label

A branch is a movable pointer to a commit. Creating one is instant and costs almost nothing.

Keep main working

Treat main as the version that always works. Build new things on their own branches.

switch moves you around

git switch changes which branch you're on. Your files update to match that branch.

Name it after the work

Names like feature/login-form or fix/navbar-overflow tell everyone what's happening there.

See it in code

Walk through the code

bash
1git branch                     # list branches, * marks current2git switch -c feature/dark-mode # create and move to new branch3 4# ...edit and commit as usual...5git commit -am "Add dark mode toggle"6 7git switch main                 # back to main, toggle is gone8git switch feature/dark-mode    # and it's back9 10git branch -d feature/dark-mode # delete after merging

1/4See your branches, then create feature/dark-mode and move onto it in one go.

git switch -c is the modern way to create a branch. Older tutorials use git checkout -b, which does the same thing.

Save yourself the pain

Common mistakes

Oops

Doing all your work directly on main.

Do this instead

Create a branch for every feature or fix. Main stays safe and you can open a clean pull request.

Oops

Switching branches with uncommitted changes and getting confused when they follow you.

Do this instead

Commit or stash your changes before you switch branches.

Your mission

Two versions, one folder

~15 min

Quick check

What does creating a new branch actually do inside Git?

Go deeper

  1. 1Create a branch with git switch -c and make a commit on it.
  2. 2Name branches clearly, like feature/dark-mode or fix/login-error.
  3. 3Switch between branches and watch your files change.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge