Branches Explained π³
Branches are one of Git's most powerful features. They allow parallel development, experimentation, and organized workflows. Let's master them!
What is a Branch? πβ
A branch is an independent line of development. Think of it as creating a copy of your project where you can make changes without affecting the main code.
main: oββoββoββo (stable code)
feature: βββoββoββo (your new feature)
Each branch points to a specific commit, and you can switch between branches to work on different features simultaneously.
Creating Branchesβ
List Branchesβ
# List local branches
git branch
# Output example:
# * main
# feature-login
# bugfix-typo
# The * indicates current branch
# List all branches (including remote)
git branch -a
# List remote branches
git branch -r
Create a New Branchβ
# Create branch (doesn't switch to it)
git branch feature-new-ui
# Create and switch in one command
git checkout -b feature-new-ui
# Modern way (Git 2.23+)
git switch -c feature-new-ui
Delete a Branchβ
# Delete local branch
git branch -d feature-new-ui
# Force delete (even if not merged)
git branch -D feature-new-ui
# Delete remote branch
git push origin --delete feature-new-ui
Switching Between Branchesβ
Checkout (Traditional Way)β
# Switch to existing branch
git checkout main
# Switch and create branch
git checkout -b feature-auth
Switch (Modern Way - Git 2.23+)β
# Switch to branch
git switch main
# Switch and create branch
git switch -c feature-auth
# Go back to previous branch
git switch -
What Happens When You Switch?β
Before switch:
my-project/
βββ file1.js (version from main)
βββ file2.js
βββ README.md
git switch feature
After switch:
my-project/
βββ file1.js (version from feature)
βββ file2.js
βββ README.md
Files update to match the branch!
Branching Strategies πβ
Git Flow (Complex Projects)β
Perfect for projects with scheduled releases:
main ββoββoββoββo (production-ready)
β β
βββββββββmerge
develop ββoββoββoββo (development)
β β β
β / release-1.0.0
ββoββββo (release prep)
feature-auth ββoββoββo (features)
feature-ui ββoββo
bugfix-typo ββo
Branches:
main- Production code onlydevelop- Integration branchfeature/*- New featuresrelease/*- Release preparationbugfix/*- Bug fixes
Workflow:
# Create feature from develop
git checkout develop
git checkout -b feature/user-auth
# Work on feature
git add .
git commit -m "Add user authentication"
# Create pull request to develop
# After review: merge to develop
# When ready for release
git checkout -b release/1.0.0
# Fix release issues only
# Merge to main and develop
git checkout main
git merge release/1.0.0
git tag v1.0.0
GitHub Flow (Simple & Fast)β
Perfect for continuous deployment:
main ββoββoββo (always deployable)
β β β
βββ¬ βββ¬ ββββ feature branches
β β
β β (merge via PR)
main branches
Branches:
main- Always production-readyfeature/*- Short-lived feature branches
Workflow:
# Create feature branch
git checkout -b feature/dark-mode
# Work and commit
git add .
git commit -m "Add dark mode support"
# Push to remote
git push origin feature/dark-mode
# Create pull request on GitHub
# Review and merge
# Delete branch
# Pull latest main
git checkout main
git pull origin main
Trunk-Based Development (Rapid Deployment)β
For teams doing frequent deployments:
main ββoββoββoββoββoββo (many small commits)
Principles:
- All developers commit to main frequently (daily)
- Short-lived feature branches (if any)
- Feature flags for incomplete features
- Continuous integration & testing
Merging Branchesβ
Fast-Forward Mergeβ
When target branch hasn't changed:
main: oββoββo
βββoββo feature
git merge feature
Result (no new commit):
main: oββoββoββoββo
Command:
git checkout main
git merge feature
# Result: Fast-forward merge
# Branch pointer just moves forward
Regular Merge (Merge Commit)β
When both branches have new commits:
main: oββoββoββββββo (merge commit)
βββoββoββoββ
Command:
git checkout main
git merge feature
# Git creates a merge commit
# Preserves branch history
# Both parent commits visible
Preventing Fast-Forwardβ
# Force merge commit even if possible
git merge --no-ff feature
# Creates merge commit with message
# Helpful for feature tracking
Handling Merge Conflicts β οΈβ
Conflicts occur when Git can't automatically merge changes.
What Causes Conflicts?β
Same file, same location, different changes:
main: Edit line 5: "Welcome!"
feature: Edit line 5: "Hello there!"
Result: CONFLICT
Resolving Conflictsβ
Step 1: See conflicted files
git status
# Output:
# Both modified: app.js
# Both added: config.json
Step 2: View conflict markers
<<<<<<< HEAD
Welcome!
=======
Hello there!
>>>>>>> feature
# HEAD section = your current branch
# feature section = incoming branch
# Choose which to keep
Step 3: Resolve
# Edit file manually
# Remove markers
# Keep desired version
# Result:
Welcome and Hello!
# Or choose one completely:
Welcome!
Step 4: Complete merge
git add app.js
git commit -m "Merge feature: resolve conflicts"
Conflict Resolution Strategiesβ
# Keep your version entirely
git checkout --ours app.js
# Keep their version entirely
git checkout --theirs app.js
# Abort merge and start over
git merge --abort
Rebasing Branches πβ
Rebase re-applies commits onto a new base. It rewrites historyβuse carefully!
Basic Rebaseβ
Before:
main: oββoββo
βββoββo feature
git switch feature
git rebase main
After:
main: oββoββo
βββoββo feature
# Same commits, but on new base
When to use: Cleaning up local branches before pushing When NOT to use: Branches already pushed to shared repository
Interactive Rebaseβ
Edit, reorder, squash, or drop commits:
# Rebase last 3 commits
git rebase -i HEAD~3
# Opens editor:
# pick a1b2c3d Add auth
# pick x1y2z3d Fix typo
# pick m1n2o3p Add tests
# Change to:
# pick a1b2c3d Add auth
# squash x1y2z3d Fix typo
# reword m1n2o3p Add comprehensive tests
# Result: 2 commits, second one renamed
Rebase vs Mergeβ
| Aspect | Merge | Rebase |
|---|---|---|
| History | Preserves branching | Rewrites, linear |
| Commits | Creates merge commit | Re-applies existing |
| Shared branches | Safe | Dangerous |
| Readability | Shows all activity | Cleaner history |
| Use case | Shared branches | Local cleanup |
Remote Branches πβ
Tracking Remote Branchesβ
# Create local branch tracking remote
git checkout --track origin/feature-auth
# Shorthand (auto-creates with same name)
git checkout feature-auth
# Set upstream for existing branch
git branch -u origin/feature-auth
Pull and Mergeβ
# Pull = fetch + merge
git pull origin main
# Equivalent to:
git fetch origin main
git merge origin/main
# Pull with rebase instead
git pull --rebase origin main
Push Branchesβ
# Push to remote with same name
git push origin feature-auth
# Set upstream while pushing
git push -u origin feature-auth
# Push all branches
git push origin --all
# Delete remote branch
git push origin --delete feature-auth
Branching Best Practices π―β
Naming Conventionsβ
# Good branch names:
feature/user-authentication
feature/dark-mode-support
bugfix/login-button-crash
hotfix/critical-security-issue
docs/api-documentation
refactor/database-layer
# Clear pattern: type/description
# Avoid:
fix1, new-stuff, temp, asdfghjkl
Branch Hygieneβ
# Delete merged branches regularly
git branch -d feature-auth
# List merged branches
git branch --merged
# Delete all merged branches
git branch --merged | grep -v "*" | xargs git branch -d
Keep Branches Updatedβ
# Fetch latest changes
git fetch origin
# Rebase on latest main
git rebase origin/main
# Or merge latest main
git merge origin/main
Review Before Mergeβ
Always use pull requests:
git push origin feature-auth
# Create PR on GitHub
# Wait for review
# Address feedback
# Then merge
Common Branching Workflowsβ
Feature Branch Workflowβ
# 1. Create feature branch
git checkout -b feature/new-api-endpoint
# 2. Make changes
git add .
git commit -m "Add new API endpoint"
# 3. Push branch
git push origin feature/new-api-endpoint
# 4. Create pull request
# (on GitHub)
# 5. After approval, merge
git checkout main
git pull origin main
git merge feature/new-api-endpoint
# 6. Delete branch
git branch -d feature/new-api-endpoint
git push origin --delete feature/new-api-endpoint
Hotfix Workflowβ
# Critical bug in production!
# 1. Create hotfix from main
git checkout main
git checkout -b hotfix/critical-security-issue
# 2. Fix the bug
git add .
git commit -m "Fix critical security issue"
# 3. Merge to main
git checkout main
git merge --no-ff hotfix/critical-security-issue
git tag v1.0.1
# 4. Also merge to develop
git checkout develop
git merge --no-ff hotfix/critical-security-issue
# 5. Delete hotfix
git branch -d hotfix/critical-security-issue
Advanced Scenariosβ
Recovering Deleted Branchβ
# See recent HEAD positions
git reflog
# Find the commit you deleted
# a1b2c3d HEAD@{5}: commit: Last commit on deleted branch
# Recreate branch from that commit
git checkout -b feature-recovered a1b2c3d
Moving Commits Between Branchesβ
# Oops! Committed to main instead of feature
# Find commit hash
git log
# Create feature branch at current location
git branch feature-fix
# Reset main to previous commit
git reset --hard HEAD~1
# Work continues on feature-fix
git checkout feature-fix
Now you understand branches! Next, learn about Diff, Stash & Tags for more advanced workflows. π
Next: Diff, Stash & Tags