Module 05 of 10 · branch · switch · checkout

Branching Basics

What is a Branch?

A branch is a lightweight, movable pointer to a commit. Think of it as a parallel timeline. You can create a branch, make changes there, and the main branch is completely unaffected until you decide to merge.

main: A → B → C
feature: A → B → D → E

This is how teams work in parallel — one developer works on a login feature while another fixes a bug, each on their own branch.

Listing Branches

git branch

The current branch is marked with * and shown in green. When you first init a repo you have one branch: main.

Creating a Branch

git branch feature-login

This creates the branch but doesn't switch to it yet. You're still on main.

Switching Branches

The modern command (git 2.23+):

git switch feature-login

The older command (still widely used):

git checkout feature-login

Create and Switch in One Step

git switch -c new-feature       # modern
git checkout -b new-feature     # classic

What is HEAD?

HEAD is just a pointer to the branch you're currently on. When you make a commit, HEAD (and your current branch) moves forward to the new commit.

HEAD → feature-login → commit E

When you switch branches, HEAD moves to point at the new branch.

Best practice: Never commit directly to main on a real project. Always create a feature branch, do your work there, then merge back (or open a pull request).

Deleting a Branch

After merging, clean up old branches:

git branch -d feature-login     # safe delete (only if merged)
git branch -D feature-login     # force delete
Watch the commit graph on the right — when you create a branch and make commits on it, you'll see the graph split into two parallel lines.

Your Challenges

Create a branch called feature: git branch feature
Switch to it: git switch feature
Make a commit on the feature branch (add a file, then add & commit)
Switch back to main: git switch main
Module 5 complete! Branching is one of git's most powerful features. Next we'll learn how to bring branches back together.
student@git-mastery: ~/my-project (main)