Branching Basics

Branches in Git serve as separate lines of development that allow multiple features or changes to be worked on simultaneously without affecting the main codebase. With branches, you can create isolated environments for different tasks, collaborate with others, and manage complex workflows.

What are Git Branches?

Think of Git branches as parallel universes for your code. Each branch represents a separate line of development where you can make changes without affecting other branches. This is like having multiple drafts of a document where you can experiment with different ideas simultaneously.

Why Use Branches?

  1. Isolation: Work on features without breaking the main code
  2. Collaboration: Multiple developers can work on different features simultaneously
  3. Experimentation: Try new ideas without risk
  4. Organization: Keep different types of work separated

Visual Understanding

Here's how branches work conceptually:

Initial Repository:
main: A---B---C

After Creating a Feature Branch:
main:    A---B---C
feature:   D---E

After More Development:
main:    A---B---C---F
         ↘         ↗
feature:   D---E---G

Common Branching Scenarios

Scenario 1: Feature Development

Situation: You need to add a new login system to your website.

Step-by-Step Process:

  1. Start from main branch:

    git checkout main
    git pull origin main  # Get latest changes
    
  2. Create and switch to feature branch:

    git checkout -b feature/login-system
    
  3. Work on your feature:

    # Make changes to files
    git add .
    git commit -m "Add login form HTML structure"
    git commit -m "Add login validation logic"
    git commit -m "Add login styling"
    
  4. Push your branch:

    git push origin feature/login-system
    

Expected Outcome: You now have a separate branch with your login feature that doesn't interfere with the main codebase.

Scenario 2: Bug Fix

Situation: There's a critical bug in production that needs immediate fixing.

Step-by-Step Process:

  1. Create hotfix branch from main:

    git checkout main
    git checkout -b hotfix/fix-payment-error
    
  2. Fix the bug:

    # Fix the payment processing issue
    git add src/payment.js
    git commit -m "Fix null pointer exception in payment processing"
    
  3. Push and deploy quickly:

    git push origin hotfix/fix-payment-error
    # Create pull request for review and merge
    

Expected Outcome: Bug is fixed in isolation and can be quickly merged back to main.

Branch States and Visualization

Before Creating Branch

Repository State:
main: [A] ← [B] ← [C] ← HEAD

Working Directory: Clean
Current Branch: main

After Creating Feature Branch

Repository State:
main:    [A] ← [B] ← [C]
feature: [A] ← [B] ← [C] ← HEAD

Working Directory: Clean
Current Branch: feature

After Making Changes on Feature Branch

Repository State:
main:    [A] ← [B] ← [C]
feature: [A] ← [B] ← [C] ← [D] ← [E] ← HEAD

Working Directory: Clean
Current Branch: feature

Best Practices for Beginners

1. Always Start from Updated Main

git checkout main
git pull origin main
git checkout -b your-new-branch

2. Use Descriptive Branch Names

  • ✅ Good: feature/user-authentication
  • ✅ Good: bugfix/payment-validation
  • ✅ Good: hotfix/security-patch
  • ❌ Bad: my-branch
  • ❌ Bad: temp
  • ❌ Bad: new-stuff

3. Keep Branches Focused

  • One branch = One feature or fix
  • Avoid mixing unrelated changes
  • Smaller branches are easier to review and merge

4. Regular Commits with Clear Messages

git commit -m "Add user input validation"
git commit -m "Implement password encryption"
git commit -m "Add error handling for failed login"

Common Beginner Mistakes and Solutions

Mistake 1: Working Directly on Main

Problem: Making changes directly on the main branch Solution: Always create a new branch for any work

# Instead of working on main, do this:
git checkout -b feature/my-new-feature

Mistake 2: Forgetting to Switch Branches

Problem: Making commits on the wrong branch Solution: Always check your current branch

git branch  # Shows current branch with *
git status  # Shows current branch in output

Mistake 3: Not Updating Main Before Branching

Problem: Creating branches from outdated main Solution: Always pull latest changes first

git checkout main
git pull origin main
git checkout -b new-feature

Real-World Example: E-commerce Website

Let's say you're working on an e-commerce website. Here's how different team members might use branches:

main:     [v1.0] ← [v1.1] ← [v1.2]
              ↘         ↘         ↘
feature/cart:   [add-item] ← [remove-item]
feature/search:       [search-bar] ← [filters]
hotfix/checkout:                    [fix-payment]

Alice works on the shopping cart feature:

  • Branch: feature/shopping-cart
  • Commits: Add items, remove items, calculate totals

Bob works on search functionality:

  • Branch: feature/product-search
  • Commits: Search bar, filters, sorting

Carol fixes a payment bug:

  • Branch: hotfix/payment-processing
  • Commits: Quick fix for critical payment issue

Each person works independently without interfering with others' work.

Free Resources