What is Branching?
Branching is one of Git's most powerful features. A branch allows you to diverge from the main line of development and work independently without affecting other branches.
Why Use Branches?
1. Parallel Development
Work on multiple features simultaneously:
main ────●────●────●────●────●
│
feature-a └──●──●──●
│
feature-b └──●──●
2. Isolated Changes
Keep experimental work separate from stable code:
bash
1# Create a branch for experiments
2git checkout -b experiment
3
4# Try risky changes safely
5# If it works: merge
6# If it fails: delete the branch3. Team Collaboration
Each team member works on their own branch:
main ────●────●────●────●
│
alice-feature └──●──●──●
│
bob-bugfix └──●──●
How Branches Work
The Pointer Concept
A branch is simply a pointer to a commit:
HEAD
↓
main ──●──●──●
↑
branch pointer
Creating a branch just creates a new pointer:
main ──●──●──●
↑
feature (new branch)
HEAD Pointer
HEAD points to your current branch:
bash
1# Check where HEAD points
2cat .git/HEAD
3# ref: refs/heads/mainBranch Naming Conventions
Common Patterns
bash
1# Feature branches
2feature/user-authentication
3feature/shopping-cart
4feature/payment-integration
5
6# Bug fix branches
7bugfix/login-error
8fix/header-alignment
9
10# Hotfix branches (urgent fixes)
11hotfix/security-patch
12
13# Release branches
14release/v1.0.0
15release/2024-01Best Practices
- Use lowercase -
feature/loginnotFeature/Login - Use hyphens -
feature-namenotfeature_name - Be descriptive -
fix/user-login-timeoutnotfix/bug1 - Include ticket numbers -
feature/JIRA-123-user-profile
Common Branching Strategies
1. Git Flow
main ─────●─────────────────●───────●
\ / /
develop ────●────●────●───●────●───●
\ /
feature ●────●
main- Production codedevelop- Integration branchfeature/*- New featuresrelease/*- Release preparationhotfix/*- Emergency fixes
2. GitHub Flow
main ────●────●────●────●────●
\ /
feature ●──●──●
Simpler workflow:
- Branch from
main - Make changes
- Open Pull Request
- Merge to
main
3. Trunk-Based Development
main ────●────●────●────●────●
\ /
feature ●──● (short-lived)
- Short-lived feature branches
- Merge frequently
- Feature flags for incomplete work
Visualizing Branches
Command Line
bash
1# Simple log with branches
2git log --oneline --graph --all
3
4# Example output:
5* abc1234 (HEAD -> main) Latest commit
6| * def5678 (feature) Feature work
7|/
8* 789abcd Previous commitGUI Tools
- GitKraken - Visual branching
- SourceTree - Free Git GUI
- VS Code - Built-in Git graph extensions
Summary
Branches in Git:
- ✅ Allow parallel development
- ✅ Keep changes isolated
- ✅ Enable team collaboration
- ✅ Are just pointers (lightweight)
- ✅ Can be created/deleted easily
Next, let's learn how to create and switch between branches!
