Git Branching Strategy
What is a Git Branching Strategy?
A Git branching strategy defines how developers create, use, and merge branches to manage code changes safely and efficiently.
1. Git Flow Branching Strategy
Main branches:
- main → production-ready code
- develop → integration branch
Supporting branches:
- feature/* → new features
- release/* → preparing release
- hotfix/* → urgent production fix
Workflow Example:
git checkout develop git checkout -b feature-login # work done git add . git commit -m "Login feature" git checkout develop git merge feature-login git checkout main git merge develop git push origin main
Best for: Large projects with scheduled releases.
2. GitHub Flow Branching Strategy
Main branch:
- main → always stable
Workflow:
- Create feature branch from main
- Push branch to GitHub
- Create Pull Request
- Review and merge into main
Example:
git checkout -b feature-payment git add . git commit -m "Payment feature" git push origin feature-payment # Create Pull Request and merge to main
Best for: CI/CD and small to medium teams.
3. Trunk-Based Development
Main branch:
- main (trunk) → only long-lived branch
Workflow:
- Developers commit small changes frequently to main
- Use feature flags instead of long-lived branches
Example:
git checkout main git pull origin main # small change git add . git commit -m "Small update" git push origin main
Best for: High automation and fast delivery.
Comparison of Branching Strategies
| Strategy | Branches | Complexity | Best Use |
|---|---|---|---|
| Git Flow | main, develop, feature, release, hotfix | High | Large enterprise projects |
| GitHub Flow | main + feature | Medium | Web apps with CI/CD |
| Trunk-Based | main only | Low | Microservices & fast release |
Best Practices
- Keep main branch stable
- Use Pull Requests for merging
- Write meaningful commit messages
- Delete feature branches after merge
- Use CI/CD for validation
Important Git Commands
| Action | Command |
|---|---|
| Create branch | git checkout -b branchname |
| Switch branch | git checkout branchname |
| Merge branch | git merge branchname |
| Delete branch | git branch -d branchname |
| Push branch | git push origin branchname |
| Pull latest | git pull origin main |
Source: sureshtechlabs.com