Branches
Last updated on 2026-01-26 | Edit this page
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:
OUTPUT
On branch main
nothing to commit, working tree clean
We can also list the branches in the repository:
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:
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:
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
Switching Between Branches
How would you switch back to the main branch from the
add-cooking-difficulty branch?
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.
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.
Deleting Branches
If a branch is no longer needed, it can be deleted.
First, create a branch:
OUTPUT
Switched to a new branch 'add-garlic-in-cakes'
Check existing branches:
OUTPUT
add-cooking-difficulty 6f12a47 Initial commit
main 6f12a47 Initial commit
* add-garlic-in-cakes 6f12a47 Initial commit
Switch to another branch before deleting:
Delete the branch:
OUTPUT
Deleted branch add-garlic-in-cakes (was 6f12a47).
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.
Deleting the Current Branch
What happens if you try to delete the branch you are currently on?
Committing on a branch
Once we have successfully created this
add-cooking-difficulty branch, we can commit to it like
usual:
OUTPUT
# Guacamole
## Difficulty level
1/5
## Ingredients
- avocado
- lime
- salt
## Instructions
Viewing the commit history:
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
- Branches allow independent lines of development.
-
git switch -ccreates and switches to a new branch. -
git switchmoves between existing branches. -
git branchlists branches. -
git branch -mrenames a branch. -
git branch -ddeletes a branch;-Dforces deletion.