Beginner15 min1 prerequisite

Use Git branches to develop features safely, fix bugs, and merge changes without breaking production.

Branching & Workflows

Branches let you work on features without affecting the main codebase—essential when AI generates significant changes.

Why Branches Matter

Without Branches

Terminal
main: ●──●──●──💥 (broken by AI changes)
                
                └── Everyone affected

With Branches

Terminal
main:    ●──●──●──●──●──●  (always working)
              \       /
feature:       ●──●──💥     (experiment safely)
               
               └── Only affects this branch

Branch Basics

View Branches

Terminal
# List local branches
git branch

# List all branches (including remote)
git branch -a

# Output:
# * main                    <- Current branch
#   feature/auth
#   remotes/origin/main

Create Branch

Terminal
# Create and switch to new branch
git checkout -b feature/new-ui

# Or (newer syntax)
git switch -c feature/new-ui

Switch Branches

Terminal
# Switch to existing branch
git checkout main

# Or (newer syntax)
git switch main

Delete Branch

Terminal
# Delete local branch (after merging)
git branch -d feature/old

# Force delete (unmerged)
git branch -D feature/abandoned

Branch Naming

Conventions

Terminal
# Features
feature/user-authentication
feature/dark-mode
feature/payment-integration

# Bug fixes
fix/login-redirect
fix/mobile-layout
fix/api-timeout

# Hotfixes (urgent production fixes)
hotfix/security-patch

# Experiments
experiment/new-ai-model

Good Names

Terminal
feature/add-user-dashboard      # Clear purpose
fix/header-overflow             # Specific issue
feature/supabase-auth           # Technology context

Bad Names

Terminal
new-stuff                       # Unclear
my-branch                       # Not descriptive
test                            # Too vague

Branch Workflow

Feature Development

Terminal
# 1. Start from main
git checkout main
git pull                        # Get latest

# 2. Create feature branch
git checkout -b feature/user-profile

# 3. Work on feature (with AI tools)
# ... make changes ...

# 4. Commit regularly
git add .
git commit -m "Add profile page layout"

# 5. Push branch to remote
git push -u origin feature/user-profile

# 6. Create Pull Request on GitHub

# 7. After merge, clean up
git checkout main
git pull
git branch -d feature/user-profile

Quick Fix

Terminal
# For small, urgent fixes
git checkout main
git pull
git checkout -b fix/typo-homepage
# ... fix ...
git add .
git commit -m "Fix typo on homepage"
git push -u origin fix/typo-homepage
# Create PR  Merge  Done

Merging Branches

Merge via Pull Request (Recommended)

Terminal
GitHub:
1. Push branch
2. Create Pull Request
3. Review changes
4. Click "Merge"
5. Delete branch

Merge Locally

Terminal
# Switch to target branch
git checkout main

# Merge feature branch
git merge feature/user-profile

# Push merged changes
git push

Merge Conflicts

When Git can't auto-merge:

Terminal
git merge feature/auth
# CONFLICT (content): Merge conflict in src/app/page.tsx

# Open file and resolve manually:
<<<<<<< HEAD
// Your current code
=======
// Incoming changes
>>>>>>> feature/auth

# After resolving:
git add .
git commit -m "Merge feature/auth, resolve conflicts"

Staying Updated

Pull Latest Main

Terminal
# On your feature branch
git checkout main
git pull

# Go back to feature and merge main
git checkout feature/user-profile
git merge main

Rebase (Advanced)

Replay your commits on top of latest main:

Terminal
git checkout feature/user-profile
git rebase main

# If conflicts, resolve then:
git rebase --continue

AI Development Patterns

Before AI Session

Terminal
# Create clean branch
git checkout main
git pull
git checkout -b feature/ai-auth

# Now let AI make changes

After AI Session

Terminal
# Review what AI changed
git status
git diff

# Commit if good
git add .
git commit -m "Add authentication (AI-generated)"

# If bad, reset
git checkout .

Multiple AI Attempts

Terminal
# First attempt
git checkout -b feature/auth-v1
# AI generates code
git add . && git commit -m "Auth attempt 1"

# Not happy? Try again on new branch
git checkout main
git checkout -b feature/auth-v2
# AI generates different approach
git add . && git commit -m "Auth attempt 2"

# Compare branches, keep best

Stashing Changes

Temporarily save uncommitted changes:

Terminal
# Save current work
git stash

# Do something else
git checkout main
# ... quick fix ...
git checkout feature/my-work

# Restore saved work
git stash pop

# List stashes
git stash list

# Apply specific stash
git stash apply stash@{0}

Common Scenarios

Wrong Branch

Terminal
# Made changes on wrong branch?

# If not committed yet:
git stash
git checkout correct-branch
git stash pop

# If already committed:
git checkout correct-branch
git cherry-pick abc123      # Copy commit
git checkout wrong-branch
git reset --hard HEAD~1     # Remove from wrong branch

Abandoned Branch

Terminal
# Made a mess? Abandon and start fresh:
git checkout main
git branch -D feature/messy

# Start clean
git checkout -b feature/clean-start

Update Branch with Main

Terminal
# Your branch is behind main?
git checkout feature/my-work
git merge main

# Or rebase (cleaner history)
git rebase main

Branch Protection (GitHub)

Protect important branches:

Terminal
GitHub  Settings  Branches  Add Rule

Branch name: main
 Require pull request before merging
 Require status checks to pass
 Require conversation resolution

Visual Tools

VS Code / Cursor

Terminal
Source Control panel (Ctrl+Shift+G):
- View branches
- Switch branches
- Commit changes
- View diffs

GitHub Desktop

Terminal
Good for:
- Visual branch management
- Easy conflict resolution
- Commit history viewing

GitLens (Extension)

Terminal
Shows:
- Line-by-line blame
- Branch comparisons
- Commit history

Summary

CommandPurpose
git branchList branches
git checkout -b nameCreate and switch
git checkout nameSwitch to branch
git merge branchMerge branch into current
git branch -d nameDelete branch
git stashTemporarily save changes

Next Steps

Learn GitHub collaboration with Pull Requests and issues.

Mark this lesson as complete to track your progress