Branches

Last updated on 2026-01-26 | Edit this page

Estimated time: 30 minutes

Overview

Questions

  • What problem do branches solve?
  • How do I create and switch between branches?
  • How do I manage existing branches?

Objectives

  • Explain what Git branches are and why they are useful.
  • Create, list, rename, and delete branches.
  • Switch between branches.

When working alone or in very small teams, committing directly to main can be sufficient. As soon as multiple people work on the same repository, however, this approach becomes difficult. Several collaborators may modify files at the same time, increasing the risk of conflicts.

Git provides branches to address this problem.

So far we have been working on the main branch of our recipe book.

---
config:
  gitGraph:
    showCommitLabel: false
---
    gitGraph
        accDescr {A Git graph showing commits on the main branch.}
        commit
        commit
        commit

All commits have been added sequentially to the same branch (main).

We can confirm this with:

BASH

$ git status

OUTPUT

On branch main
nothing to commit, working tree clean

We can also list the branches in the repository:

BASH

$ git branch

OUTPUT

* main

What is a branch?


A branch is an independent line of development in a repository. Branches allow us to:

  • isolate work: develop new features or fixes without affecting main,
  • collaborate safely: work in parallel without interfering with each other,
  • experiment: try changes that can be discarded if they do not work,
  • organize work: associate one branch with one task or idea

Each branch represents a snapshot of the repository history that can evolve independently.

Creating Branches


When creating a branch, choose a descriptive name that reflects the work being done.

In this lesson, we are going to create a branch named add-cooking-difficulty.

There are several ways to create a branch and switch to the new branch:

After switching branches, confirm your location:

BASH

$ git status

OUTPUT

On branch add-cooking-difficulty
nothing to commit, working tree clean

At this point, the new branch exists but contains no new commits:

---
config:
  gitGraph:
    showCommitLabel: false
---
    gitGraph
        accDescr {A Git graph showing the main branch and a new add-cooking-difficulty branch with no commits.}
        commit
        commit
        branch add-cooking-difficulty

List all branches:

BASH

$ git branch

OUTPUT

* add-cooking-difficulty
  main

The * indicates we are now on the add-cooking-difficulty branch.

Branch Naming Conventions

Many projects define naming conventions for branches. These conventions are often documented in a CONTRIBUTING.md file or in the project documentation.

Common practices include:

  • using descriptive names
  • separating words with - or _
  • including ticket or issue numbers when applicable
Challenge

Switching Between Branches

How would you switch back to the main branch from the add-cooking-difficulty branch?

BASH

$ git switch main
Challenge

Fixing a Typo in a Branch Name

Help! Alfredo made a typo when naming their branch, cooooking-difficulty, how can they fix the branch name?

Hint: Chaeck the documentation for git branch.

Use the -m option to rename a branch:

BASH

$ git branch -m <old-branch-name> <new-branch-name>
Challenge

Branch start-points

So far, branches were created from HEAD because we were on main. How can you create a branch starting from an earlier commit?

Hint: Check the documentation for git switch.

Specify a start point when creating the branch:

BASH

$ git switch -c <branch-name> <start-point>

The start point can be a branch name, a commit ID, or a tag.

Deleting Branches


If a branch is no longer needed, it can be deleted.

First, create a branch:

BASH

$ git switch -c add-garlic-in-cakes

OUTPUT

Switched to a new branch 'add-garlic-in-cakes'

Check existing branches:

BASH

$ git branch -vv

OUTPUT

  add-cooking-difficulty    6f12a47 Initial commit
  main                      6f12a47 Initial commit
* add-garlic-in-cakes       6f12a47 Initial commit

Switch to another branch before deleting:

BASH

$ git switch add-cooking-difficulty

Delete the branch:

BASH

$ git branch -d add-garlic-in-cakes

OUTPUT

Deleted branch add-garlic-in-cakes (was 6f12a47).
Callout

Check your branch point

Before creating a new branch, make sure you are on the correct starting branch (often main), or explicitly specify the start point.

This avoids accidentally branching from another feature branch.

Challenge

Deleting the Current Branch

What happens if you try to delete the branch you are currently on?

Git prevents deleting the checked-out branch:

BASH

$ git switch -c gluten-free-recipes
$ git branch -D gluten-free-recipes

OUTPUT

error: Cannot delete branch 'gluten-free-recipes' checked out at '~/Documents/recipes'

Committing on a branch


Once we have successfully created this add-cooking-difficulty branch, we can commit to it like usual:

BASH

$ nano guacamole.md
$ cat guacamole.md

OUTPUT

# Guacamole
## Difficulty level
1/5
## Ingredients
- avocado
- lime
- salt
## Instructions

BASH

$ git add guacamole.md
$ git commit -m "add difficulty indication to all recipes"

Viewing the commit history:

BASH

$ git log --oneline

OUTPUT

3c5da67 (HEAD -> add-cooking-difficulty) add cooking difficulty in all recipes
0a508b4 (main) ignore png files and pictures folder
...

The branch now includes an additional commit:

---
config:
  gitGraph:
    showCommitLabel: false
---
    gitGraph
        accDescr {A Git graph showing the root-commit on the main branch and a new add-cooking-difficulty branch with no commits.}
        commit
        commit
        branch add-cooking-difficulty
        commit
Key Points
  • Branches allow independent lines of development.
  • git switch -c creates and switches to a new branch.
  • git switch moves between existing branches.
  • git branch lists branches.
  • git branch -m renames a branch.
  • git branch -d deletes a branch; -D forces deletion.