By the end of this lesson, you will:
Definition: Branching creates parallel universes for your code. Work on features, experiment, and collaborate without affecting the main codebase.
Without Branches:
With Branches:
A branch is simply a movable pointer to a specific commit. That's it!
main: A---B---C
\
feature: D---E
# See current branch
git branch
# Create new branch
git branch feature-login
# Switch to new branch
git checkout feature-login
# Or do both in one command
git checkout -b feature-signup
# Switch branches (replaces checkout)
git switch main
git switch feature-login
# Create and switch
git switch -c feature-new-homepage
Feature Branches:
feature/user-authentication
feature/shopping-cart
feat/mobile-responsive
Bug Fix Branches:
fix/login-error
bugfix/payment-timeout
hotfix/critical-security-issue
Experimental Branches:
experiment/new-ui-framework
spike/performance-testing
branch1
or test
feature/PROJ-123-user-login
feature-name
not feature name
# Always start from updated main
git switch main
git pull origin main
# Create branch for specific feature
git switch -c feature/add-dark-mode
# Make your changes
echo "body.dark { background: #333; }" > dark-mode.css
# Stage and commit
git add .
git commit -m "Add dark mode CSS styles"
# Continue developing...
git add .
git commit -m "Add dark mode toggle button"
# First push of new branch
git push -u origin feature/add-dark-mode
# Subsequent pushes
git push
# After PR is approved, switch back to main
git switch main
git pull origin main
# Delete local feature branch
git branch -d feature/add-dark-mode
# Delete remote branch (if not done automatically)
git push origin --delete feature/add-dark-mode
When no commits happened on main:
git switch main
git merge feature/simple-fix
# No merge commit created
Before: main: A---B
\
feature: C---D
After: main: A---B---C---D
When both branches have new commits:
git switch main
git merge feature/complex-feature
# Creates merge commit
Before: main: A---B---E
\
feature: C---D
After: main: A---B---E---M
\ /
feature: C---D
Combines all feature commits into one:
git switch main
git merge --squash feature/many-small-commits
git commit -m "Add complete user profile feature"
# List all branches
git branch
# List remote branches
git branch -r
# List all branches (local + remote)
git branch -a
# Delete branch (safe)
git branch -d branch-name
# Force delete branch
git branch -D branch-name
# Rename current branch
git branch -m new-name
# Show branch history
git log --oneline --graph --all
# Show branches with last commit
git branch -v
# Show merged branches
git branch --merged
# Show unmerged branches
git branch --no-merged
# Switch to existing branch
git switch branch-name
# Create and switch to new branch
git switch -c new-branch-name
# Track remote branch
git switch -c local-name origin/remote-branch
# If branch exists locally
git switch existing-branch
# If you want to recreate it
git branch -D old-branch
git switch -c new-branch
# Update your branch
git pull origin branch-name
# Or rebase to avoid merge commits
git pull --rebase origin branch-name
We'll cover this in detail in the next lesson!
# Stay up-to-date with main
git switch main
git pull origin main
git switch feature/my-branch
git merge main
# List old branches
git for-each-ref --format='%(refname:short) %(committerdate)' refs/heads | sort -k2
# Delete merged branches
git branch --merged | grep -v main | xargs -n 1 git branch -d
On GitHub:
# Basic tree view
git log --oneline --graph --all
# Detailed view
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --all
Try this workflow:
git switch -c practice/my-experiment
git switch main
git switch -c practice/another-test
git branch
git log --oneline --graph --all
You now understand:
Next up: Collaboration workflows where branching really shines!