Git Branch Strategy for Beginners: Streamlining Feature Development, Bug Fixes, and Deployment
Learn an effective Git branch strategy for smoothly managing features, bug fixes, and deployments, tailored for beginner developers.
Git Branch Strategy for Beginners: Streamlining Feature Development, Bug Fixes, and Deployment
When you're starting out in development, figuring out which Git branch to work on can be confusing. Sticking to the main branch might seem swift until a bug arises and throws off your commits and deployment schedule. By organizing branches by their roles, your code can remain tidy whether you're working solo or in a team.
Three Basic Principles of Branch Organization
- Keep Deployable Code Clear: Only have deployable code on one branch, typically
main(ormaster). - Keep Work Units Specific: Create dedicated branches for features and bugs to keep your commits neat.
- Review Before Merging: Make sure code passes at least local tests before merging.
Recommended Branch Structure (Ideal for Beginners)
1) main: For Production Deployment
- The version users actually use.
- Contains only code ready for immediate deployment.
- Avoid direct commits; instead, merge through PRs.
2) develop: For Integrating Development (Optional)
- A space to merge multiple features for the next release.
- Can be skipped if you're developing solo with frequent deployments.
- Acts as a buffer zone for team development.
3) feature/*: For Feature Development
- Examples:
feature/login,feature/profile-edit - Clearly define the branchโs purpose to keep your commits clean.
4) bugfix/*: For Routine Bug Fixes
- Examples:
bugfix/null-pointer-on-home - Used for bug fixes found before deployment (in
develop).
5) hotfix/*: For Urgent Bug Fixes
- Examples:
hotfix/payment-crash - Used for immediately fixing issues found in already deployed
main. - Ensure changes are merged back into
developto prevent bugs from recurring.
Example Flow: Feature Development โ Integration โ Deployment
Standard Routine for Developing a Single Feature
git checkout develop
git pull
git checkout -b feature/login
# Work...
git add .
git commit -m "feat: Added login form and API integration"
git checkout develop
git merge feature/login
git push
- Develop in
feature/login. - Once complete, merge into
develop. - When enough features are ready, merge into
mainfor deployment.
During Deployment (e.g., v1.2.0)
git checkout main git pull git merge develop git tag v1.2.0 git push --tags git push
Tagging is optional but useful for tracking deployment versions.
Example of Handling Urgent Bug Fixes with Hotfix
Situation: App crashes on the payment screen, immediate fix needed.
git checkout main
git pull
git checkout -b hotfix/payment-crash
# Fix...
git add .
git commit -m "fix: Handled exception causing payment crash"
git checkout main
git merge hotfix/payment-crash
git tag v1.2.1
git push --tags
git push
git checkout develop
git merge hotfix/payment-crash
git push
Key Point: Ensure the fix is reflected in both main and develop to prevent regression.
Minimum Guidelines for Branch/Commit Naming
Branch Names
feature/summary-of-featurebugfix/cause-or-symptomhotfix/service-impact-issue
Commit Messages (Simple Rules Only)
feat: ...for adding featuresfix: ...for bug fixesrefactor: ...for refactoring with minimal behavior changeschore: ...for configuration or routine tasks Example:fix: Prevented NPE on entering home screen
Four Common Mistakes Beginners Make (and Solutions)
- Combining Features, Bugs, and Refactoring in One Branch โ Mixing purposes complicates reviews and rollbacks. Keep one purpose per branch.
- Committing Directly to
mainโ Themainshould be a safe zone. Always create a branch to merge changes. - Not Merging Hotfixes Back to
developโ This can cause the bug to resurface. Ensure hotfixes are merged back. - Failing to Clean Up Merged Branches
โ Delete completed
feature/*branches to keep the repository organized.
Conclusion: "Divide Small, Merge Safely"
The goal of a Git branch strategy isn't to memorize complex rules but to develop habits that prevent chaos. By dedicating main for deployments, feature/ for new development, bugfix/ for bugs, and hotfix/* for urgent issues, you'll elevate the clarity and efficiency of your development process.
โฌ๏ธ If this helped, please click the ad below! It supports me a lot ๐โโ๏ธ โฌ๏ธ
