Skip to main content

Getting Started with Git — Chapter 3 / 4

Branches

Branches let you work on different things at the same time. They are the most powerful feature of Git, and simpler than you think.

Chapter 3 / 4

Step 01 / 11

What is a branch?

Concept

Keyword: branch A branch is a name that points to a commit. It's like a sticky note placed on a page of a book. The sticky note says: "I am here". When you add a new commit, the sticky note moves to the new page.

Many people think a branch is a copy of the code. That's not true.

A branch is just a pointer. It's a small file that contains the hash of a commit. Creating a branch is instantaneous. It takes almost no disk space.

$ # Each branch points to a commit main → a1b2c3d (most recent commit on main) feature → e4f5g6h (most recent commit on feature) HEAD → main # HEAD says: "you are on the main branch"

Concept

Key point The main branch is not special. It's just a name. Git creates it by default, but you could call it anything. It has no special power.

“Creating a branch copies the whole project's code.” True or false?

Step 02 / 11

Why use branches?

Imagine you're working on a website. The site works well. Now you want to add a new page.

If you work directly on main, you risk breaking the site. Your changes in progress might not be finished.

With a branch, you work separately. The site stays functional on main. When your new page is ready, you bring it back into main.

The analogy

It's like having a draft. You write on the draft. When it's ready, you copy it to the final version.

Step 03 / 11

Creating a branch

Concept

Keyword: git branch The git branch command is used to manage branches. Without arguments, it displays the list of branches. With a name, it creates a new branch.

$ git branch

* main
  feature/header

$ git branch feature/about

# Creates a new branch called feature/about
# Note: you stay on the current branch

Step 04 / 11

Switching branches

Concept

Keyword: git switch The git switch command lets you change branches. When you switch branches, Git updates the files in your directory. The files match the state of the chosen branch.

$ git switch feature/about

# Switches to the feature/about branch
# Files change to match this branch

$ git switch -c feature/footer

# Creates a new branch AND switches to it
# This is a shortcut for: git branch + git switch

The -c option means "create". It's the most commonly used shortcut. You create and switch in a single command.

Which command switches you to another branch?

Step 05 / 11

Merging: bringing a branch back into main

Concept

Keyword: git merge Merging means combining the work of two branches. Typically, you merge your working branch into main. It's like copying your draft to the final version.

To merge, you must first be on the branch that will receive the changes. Usually, that's main.

$ # Step 1: go back to main git switch main # Step 2: merge the branch git merge feature/about

# The content of feature/about arrives in main

Step 06 / 11

Two ways to merge

Git can merge in two different ways. It depends on what happened on the branches.

Case 1: Fast-forward This happens when main hasn't moved since the branch was created. Git simply moves the pointer forward. No need to create a new commit.

The analogy

It's as if you had read pages 1 to 10 of a book, and your friend read pages 11 to 15. You just continue. No extra work needed.

$ # Before the merge A → B (main) → C → D (feature) # After the merge: main moves forward A → B → C → D (main, feature)

Case 2: Merge commit This happens when both branches have different commits. Git creates a new commit that combines the two. This commit has two parents.

The analogy

It's as if you and your friend each wrote a different chapter. You need to assemble both chapters to make the complete book.

$ # Before: the branches have diverged → C → D (feature) A → B → E (main) # After the merge: a commit M with 2 parents → C → D ↘ A → B M (main) → E ↗

When can Git do a simple “fast-forward” (no merge commit)?

Step 07 / 11

Visualizing branches

There's a very useful command to see all branches and commits. It draws a graph in the terminal.

$ git log --graph --oneline --all

*   f7a8b9c (HEAD → main) Merge branch 'feature'
|\
| * d4e5f6a (feature) Add feature
|/
* a1b2c3d Initial commit

This command shows:

  • Commits (each line with a *)
  • Branches (names in parentheses)
  • Links between commits (the lines |, / and \)
  • Where you are (HEAD →)

Step 08 / 11

Naming your branches well

A branch name should explain what you're doing on it. Here are the most common conventions:

  • feature/name — to add a feature. Example: feature/contact-page
  • fix/name — to fix a bug. Example: fix/mobile-menu
  • chore/name — for technical maintenance. Example: chore/update-dependencies

Step 09 / 11

Common mistakes

  • Forgetting to go back to main before merging. You must always be on the branch that receives the changes.
  • Creating a branch from another branch instead of main. Always check where you are with git status before creating a branch.
  • Working directly on main. Always create a branch, even for a small change. It's safer.
  • Thinking a branch is a copy of the code. It's just a pointer. Creating 100 branches takes almost no space.

Step 10 / 11

Going further

  • git checkout: the old command for switching branches
  • git log: exploring the history in detail
  • Resolving a Git merge conflict

Step 11 / 11

Test your knowledge

Before the exercises, make sure you understood branches. Pick the answer that looks right.

Quiz — Chapter 3

1. What happens when you create a branch with git branch feature?

2. What is the difference between a fast-forward and a merge commit?

3. Before merging a branch into main, you must first...

4. What does git switch -c feature/header do?

Your feedback matters

Help us improve this course by sharing your thoughts.

So we can follow up if needed.