Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

Git branch explained: understanding branches

Branches are at the heart of Git. They let you work on multiple features in parallel without risking breaking the main code. Here is everything you need to know.

What is a Git branch?

A branch is an independent line of development. Imagine a tree: the trunk is your main branch (main), and each branch stems from the trunk to develop a feature without touching the stable code.

Technically, a Git branch is simply a pointer to a commit. When you create a branch, Git creates a new pointer. That's why Git branches are so fast and lightweight: no file copying, just a pointer.

When your feature is finished and tested, you merge (merge) your branch into the main branch. The code rejoins the trunk.

Why use branches?

Branches are essential for working cleanly, whether in a team or solo.

Protect stable code

The main branch always stays functional. Experiments happen on separate branches.

Work as a team

Each developer works on their own branch. No risk of overwriting each other's work.

Develop in parallel

Work on a feature, fix a bug, and test an idea — each on its own branch, at the same time.

Facilitate code review

One branch = one feature = one pull request. Code review is clear and focused.

Create

Create a new branch

The command git branch <name> creates a branch but does not switch to it. To create and switch in one command, use git checkout -b <name>.

The new branch starts from the current commit (HEAD). All commits you make afterward will belong to this branch, not to main.

Navigate

Switch between branches

The git switch (or git checkout) command lets you move from one branch to another. When you switch, Git updates the files in your working directory to match the target branch's state.

git switch is the newer and recommended command. It is more explicit than git checkout which does too many different things.

List

View all branches

git branch without arguments lists local branches. The asterisk (*) indicates the branch you are currently on.

Add the -a flag to also see remote branches (those on GitHub, GitLab, etc.), or -r for remote branches only.

Branch naming conventions

Good naming makes the repository readable and branches identifiable at a glance. Here are the most widely used conventions in the industry.

feature/

New feature

feature/add-cart

fix/

Bug fix

fix/header-responsive

hotfix/

Urgent production fix

hotfix/auth-vulnerability

refactor/

Refactoring without functional changes

refactor/user-component

release/

Preparing a production release

release/v2.1.0

test/

Experimentation or testing

test/new-api

Visualizing branches: the mental model

Imagine a tree seen from above. Each branch stems from a point on the trunk and develops independently.

main ────────●────────●────────●────────●→

                  ╲

          feature/login ──●────●────●

                                       ╲

                            fix/typo ──●

Each represents a commit. Branches stem from an existing commit and evolve independently until they are merged.

Part of the Git Branching Workflow guide

A

GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.

Questions fréquentes

Ready to master Git branches?

Practice creating, navigating, and merging branches in GitQuest investigations. It's free.

Get started now