Skip to main content

Working with Git Branches

In this lesson, we'll explore one of Git's most powerful features: branches. Think of branches as parallel universes where you can develop features, fix bugs, or experiment without affecting the main codebase until you're ready.

Learning Goals:

  • Understand what Git branches are and why they're useful
  • Create, list, and switch between branches
  • Learn how to merge branches back together
  • Develop confidence working with multiple branches

What Are Git Branches?

A Git branch is essentially a lightweight movable pointer to a commit. When you create a new branch, you're creating a new pointer that you can move around independently of other branches.

The default branch is typically called main (or master in older repositories). Every time you commit, the current branch pointer moves forward automatically.

Visualizing branch relationships
main:     A---B---C
feature: \
D---E

Creating and Listing Branches

Let's start by exploring the branches in your repository.

View existing branches
git branch

To create a new branch:

Create a new branch
git branch feature/login

This creates the branch but doesn't switch to it. You can verify it exists:

List all branches (including the new one)
git branch
tip

Use descriptive branch names that indicate the purpose, like feature/user-authentication, bugfix/login-error, or hotfix/critical-bug.

Switching Between Branches

To start working on your new branch, you need to switch to it:

Switch to the new branch
git checkout feature/login

Or use the shorter combined command:

Create and switch in one command
git checkout -b feature/login

Let's make some changes on our new branch:

Make changes on the feature branch
echo "// New login feature" >> login.js
git add login.js
git commit -m "Add login feature skeleton"

Understanding HEAD and Branch Pointers

The HEAD is a special pointer that refers to the current branch you're working on. When you switch branches, HEAD moves to point to the new branch.

Check where HEAD is pointing
git log --oneline -1
git status

Comparing Branches

You can see what's different between branches:

Compare current branch with main
git diff main

Or compare any two branches:

Compare two specific branches
git diff main..feature/login

Merging Branches (Preview)

While we'll cover merging in detail in the next lesson, here's a quick preview of how to bring your feature work back to the main branch:

Merge feature branch into main
git checkout main
git merge feature/login
warning

Always ensure your working directory is clean (no uncommitted changes) before switching branches. Use git status to check, and commit or stash any changes first.

Common Branch Operations

Here are the essential branch commands you'll use daily:

# Create branch
git branch new-feature

# Create and switch
git checkout -b new-feature

Common Pitfalls

  • Forgotten branch switches: Always check which branch you're on with git status or git branch
  • Working on the wrong branch: Commit or stash changes before switching branches
  • Long-lived feature branches: Try to keep branches short-lived to avoid complex merges
  • Unclear branch names: Use descriptive names that your team understands
  • Not deleting old branches: Clean up merged branches to reduce clutter

Summary

Git branches are powerful tools that enable parallel development. You can:

  • Create isolated environments for features, fixes, or experiments
  • Switch between contexts quickly and safely
  • Compare work across different branches
  • Keep your main branch stable while developing new features

Remember: branches are cheap and easy to create, so use them liberally!

Quiz

Working with Branches – Quick Check

What command creates a new branch but does not switch to it?

Question 1/5