Branches: work without fear

What a branch is; isolate experiments.

1. What is this?

A branch is a movable label on a line of history. main is the default branch. Creating a branch (git branch feature) and switching to it (git checkout feature, or git switch -c feature) lets you experiment without touching main. Merging later combines the lines.

2. Why do we use it?

Fix a bug while a feature is half-done; try an idea and discard it cleanly. Branches are cheap and disposable.

3. Syntax breakdown

  • git branch

    Lists branches; * marks the current one.

  • git checkout -b feature

    Creates branch feature and switches to it in one step.

  • git status

    Confirm which branch you are on before committing.

5. Common mistakes

  • Committing on the wrong branch — check status first.
  • Never merging or deleting stale branches — tidy up.
  • Fear of branches: they are just labels, safe to create and delete.

Practice task

Create and switch to a branch called experiment, then list branches.

Hint

git checkout -b experiment then git branch

Try it below — tests run automatically when you press Run.

git

Output

Press Run to see output.

Mini quiz

3 questions. Answer, then check.

  1. 1. What is a branch?

  2. 2. Which creates and switches in one step?

  3. 3. Why work on a branch?

4. Example — edit and run

git

Output

Press Run to see output.