
Why Branches Matter
Branches are Git’s killer feature. They allow you to diverge from the main development line, experiment freely, and merge changes back when ready. Think of branches as parallel universes for your code.
Branch Fundamentals
bash
# List all branches
git branch
# Create a new branch
git branch feature/user-authentication
# Switch to the new branch
git checkout feature/user-authentication
# Or create and switch in one command
git checkout -b feature/user-authentication
# Modern syntax (Git 2.23+)
git switch feature/user-authentication
git switch -c feature/new-feature
Real-World Branching Example
Let’s implement a user authentication system:
bash
# Start from main branch
git switch main
# Create feature branch
git switch -c feature/auth-system
# Create authentication files
cat > auth.py << 'EOF'
class UserAuth:
def __init__(self):
self.users = {}
def register(self, username, password):
if username in self.users:
return False
self.users[username] = password
return True
def login(self, username, password):
return self.users.get(username) == password
EOF
# Stage and commit
git add auth.py
git commit -m "Add basic user authentication system"
# Continue development
echo "from auth import UserAuth" >> main.py
git add main.py
git commit -m "Integrate auth system with main application"
Merging Strategies
Fast-Forward Merge: When target branch hasn’t diverged, Git simply moves the pointer forward.
bash
git switch main
git merge feature/auth-system
Three-Way Merge: When both branches have new commits, Git creates a merge commit.
bash
# This creates a merge commit
git merge feature/complex-feature --no-ff
Squash Merge: Combines all feature commits into a single commit on target branch.
bash
git merge feature/small-fixes --squash
git commit -m "Implement multiple small UI fixes"
Handling Merge Conflicts
Conflicts occur when the same lines are modified in different branches:
bash
# When merge conflict occurs
git status # Shows conflicted files
# Edit conflicted files (look for conflict markers)
# <<<<<<< HEAD
# Your changes
# =======
# Their changes
# >>>>>>> branch-name
# After resolving conflicts
git add conflicted-file.py
git commit # Git provides default merge message
Hands-On Exercise: Create two branches that modify the same file differently. Practice resolving the merge conflict manually.
You Might Also Like
🛠️ Recommended Tools for Developers & Tech Pros
Save time, boost productivity, and work smarter with these AI-powered tools I personally use and recommend:
1️⃣ CopyOwl.ai – Research & Write Smarter
Write fully referenced reports, essays, or blogs in one click.
✅ 97% satisfaction • ✅ 10+ hrs saved/week • ✅ Academic citations
2️⃣ LoopCV.pro – Build a Job-Winning Resume
Create beautiful, ATS-friendly resumes in seconds — perfect for tech roles.
✅ One-click templates • ✅ PDF/DOCX export • ✅ Interview-boosting design
3️⃣ Speechify – Listen to Any Text
Turn articles, docs, or PDFs into natural-sounding audio — even while coding.
✅ 1,000+ voices • ✅ Works on all platforms • ✅ Used by 50M+ people