Branches

Last updated on 2025-03-04 | Edit this page

Overview

Questions

  • Understand what branches are and when to use them

Objectives

  • What are branches?

What are branches and when to use them?


Have a look at these slides introducing branches.


Here are some commands related to branches that you often use in practice.

Create a new branch and switch onto it:

git switch -c new-branch-name

OUTPUT

Switched to a new branch 'new-branch-name'

Switch to an existing branch, for example back to main:

git switch main

OUTPUT

Switched to branch 'main'

Often while working on a branch, the main branch has changes that you want to apply to the branch you are working on. You can do this by pulling changes from the main branch into the branch that you are working on:

git switch branch-name # Make sure you are on the branch into which you want to pull changes from main
git pull origin main

Key Points

  • A branch represents an independent line of development.
  • Subsequent changes are considered to belong to that branch.