Lesson 7: Branches

What You'll Learn

In this lesson, you'll learn about branches - one of Git's most powerful features. Branches let you work on new features or experiments without affecting your main code.

What Are Branches?

A branch is like a parallel universe for your code:

  • The main branch contains your stable, production code
  • Feature branches let you develop new features independently
  • Changes in one branch don't affect other branches
  • When ready, you merge branches back together

Real-world analogy:

Imagine writing a book:

  • Main branch = The published version
  • Feature branch = A draft where you try a new chapter
  • You can experiment freely without changing the published book
  • When the new chapter is perfect, you add it to the main book

Part 1: Viewing Branches

List All Branches

git branch

Output shows all branches, with * indicating your current branch:

* main
  feature/new-header
  bugfix/nav-alignment

List Remote Branches Too

git branch -a

Part 2: Creating Branches

Create a New Branch

git branch feature/add-contact-form

This creates the branch but doesn't switch to it.

Create and Switch in One Command

git checkout -b feature/add-contact-form

Modern alternative:

git switch -c feature/add-contact-form

Branch Naming Conventions

Prefix Purpose Example
feature/ New features feature/user-authentication
bugfix/ Bug fixes bugfix/login-error
hotfix/ Urgent production fixes hotfix/security-patch
docs/ Documentation docs/api-documentation

Part 3: Switching Branches

Switch to Existing Branch

git checkout feature/add-contact-form

Modern alternative:

git switch feature/add-contact-form

What Happens When You Switch?

  • Your working directory updates to match the branch
  • Files appear/disappear based on the branch's state
  • You can only switch if there are no uncommitted changes (or use git stash)

Part 4: Working with Branches

Typical Workflow

# 1. Create and switch to new branch
git switch -c feature/new-homepage

# 2. Make changes to files
# Edit index.html, style.css, etc.

# 3. Commit your changes
git add .
git commit -m "Create new homepage design"

# 4. Push branch to GitHub
git push -u origin feature/new-homepage

# 5. Switch back to main when done
git switch main

Part 5: Deleting Branches

Delete Local Branch

git branch -d feature/old-feature

Use -D (capital D) to force delete:

git branch -D feature/experimental

Delete Remote Branch

git push origin --delete feature/old-feature

Part 6: Why Use Branches?

Benefit Description
Isolation Work on features without breaking main code
Experimentation Try new ideas safely - delete if they don't work
Parallel Development Multiple people work on different features simultaneously
Code Review Review changes before merging to main
Clean History Organize commits by feature or fix

Part 7: Branch Best Practices

  • Keep main stable - only merge tested, working code
  • Create branches from main - start with the latest code
  • Use descriptive names - feature/add-user-login not feature/stuff
  • Keep branches short-lived - merge within days, not weeks
  • Delete merged branches - keep your branch list clean
  • Pull main regularly - keep your feature branch up to date

Practice Exercise

  1. In your git-practice repository:
    # Create a new branch
    git switch -c feature/add-about-page
    
    # Create a new file
    echo "# About Us" > about.html
    
    # Commit the change
    git add about.html
    git commit -m "Add about page"
    
    # Push to GitHub
    git push -u origin feature/add-about-page
    
    # Switch back to main
    git switch main
    
    # Notice about.html is gone! (It's in the feature branch)
    
    # Switch back to see it again
    git switch feature/add-about-page

Summary

Essential Branch Commands

Command Purpose
git branch List branches
git switch -c name Create and switch to new branch
git switch name Switch to existing branch
git branch -d name Delete local branch

What's Next?

In the next lesson, you'll learn how to merge branches - combining the work from your feature branch back into the main branch.