Imagine you're writing a story, and you want to explore different plot directions without ruining your main narrative. That's exactly what Git branches do!
A branch is simply a movable pointer to a commit. Think of it as:
| Branch Type | Purpose | Real Example | Lifetime |
|---|---|---|---|
main/master |
Production-ready code | Your live website | Forever |
develop |
Integration branch | Next release preparation | Forever |
feature/* |
New features | feature/user-login | Days to weeks |
bugfix/* |
Bug fixes | bugfix/cart-total-error | Hours to days |
hotfix/* |
Emergency fixes | hotfix/security-patch | Hours |
Creating a new branch:
git branch feature-navbar # Creates branch git checkout feature-navbar # Switches to branch # Or do both in one command: git checkout -b feature-navbar
Viewing branches:
git branch # List local branches git branch -a # List all branches (including remote) git branch -v # List branches with last commit
Merging is how we combine work from different branches. Git offers several merge strategies:
Sometimes, two branches modify the same part of a file differently. Git needs your help to decide what to keep:
<<<<<<< HEAD function calculateTotal(items) { return items.reduce((sum, item) => sum + item.price, 0); } ======= function calculateTotal(items) { const subtotal = items.reduce((sum, item) => sum + item.price, 0); return subtotal * 1.08; // Added tax } >>>>>>> feature-branch
| Strategy | Best For | Complexity | Key Features |
|---|---|---|---|
| Git Flow | Scheduled releases | High | Multiple branch types, release planning |
| GitHub Flow | Continuous deployment | Low | Simple, main + feature branches |
| GitLab Flow | Multiple environments | Medium | Environment branches |
mkdir recipe-website
cd recipe-website
git init
echo "# Amazing Recipes" > README.md
git add README.md
git commit -m "Initial commit"
# Developer A: Adding desserts section
git checkout -b feature-desserts
echo "## Desserts" > desserts.md
git add desserts.md
git commit -m "Add desserts section"
# Developer B: Adding navigation
git checkout main
git checkout -b feature-navigation
echo "## Navigation" > nav.html
git add nav.html
git commit -m "Add navigation"
# Merge desserts
git checkout main
git merge feature-desserts
# Merge navigation
git merge feature-navigation
Sometimes you need just one specific commit from another branch:
git cherry-pick abc123 # Copy specific commit to current branch
git branch -d feature-namefeature/user-authentication not new-stuffgit fetch --prune to remove deleted remote branchesgit branch --merged shows safe-to-delete branchesSymptom: "Oh no, I made commits to main instead of my feature branch!"
Solution:
# Move commits to correct branch
git checkout -b feature-branch # Create new branch with commits
git checkout main
git reset --hard HEAD~3 # Remove last 3 commits from main
Symptom: Massive conflicts when trying to merge
Prevention:
# Regularly update your feature branch
git checkout feature-branch
git merge main # Or: git rebase main
Excellent work! You've mastered branching and merging. In the next lesson, we'll explore:
๐ก Remember: Branches are not scary! They're your playground for experimentation. The more you use them, the more confident you'll become. Create branches liberally, merge carefully, and always keep your main branch stable!