Git Branching Strategies
Overview
Version control is essential for collaboration. This tutorial covers the most popular Git branching strategies: Git Flow, GitHub Flow, and others. You'll learn how to structure your branches, manage releases, and collaborate effectively with your team.
1. Git Basics Review
Git is a distributed version control system. Key concepts:
- Commit: A snapshot of changes.
- Branch: A pointer to a commit; enables parallel development.
- Merge: Combines changes from one branch into another.
- Rebase: Moves a branch to a new base commit (rewrites history).
2. Git Flow
Git Flow is a strict branching model with multiple branch types:
- main (or master): Production‑ready code.
- develop: Integration branch for features.
- feature/*: Features in development (branch from develop).
- release/*: Release preparation (branch from develop, merge to main and develop).
- hotfix/*: Emergency fixes (branch from main, merge to main and develop).
3. GitHub Flow
GitHub Flow is a simpler, trunk‑based strategy:
- main: Always deployable.
- Feature branches: Branch from main, use pull requests to merge.
- No release branches or develop branch.
- Deployments happen from main or via release tags.
4. Other Strategies
- Trunk‑Based Development: All developers work on a single branch (trunk). Short‑lived feature branches. Good for large teams with CI/CD.
- Release Branches (without develop): Similar to Git Flow but without a permanent develop branch.
- Fork‑based workflow: Used in open‑source projects; contributors fork the repo and submit PRs.
5. Choosing a Strategy
Consider these factors:
- Team size: Small teams → GitHub Flow, large teams → Git Flow or Trunk‑Based.
- Release frequency: Frequent releases → GitHub Flow, less frequent → Git Flow.
- Complexity: Simpler is better for most projects.
- Culture: Some teams prefer strict processes, others prefer flexibility.
Quiz
Question 1
In Git Flow, which branch is used for preparation of a new release?
- main
- develop
- release/*
- hotfix/*
Show answer
Question 2
Which branching strategy is recommended for teams practicing continuous delivery?
- Git Flow
- GitHub Flow
- Trunk‑Based Development
- Both B and C
Show answer
Question 3
What is the purpose of a hotfix branch in Git Flow?
- To add new features
- To fix critical bugs in production
- To prepare release documentation
- To refactor code
Show answer
Exercises
Exercise 1
Describe the steps to create a new feature using GitHub Flow. Include branch creation, commits, pull request, and merge.
Sample answer
git checkout -b feature/new-feature- Make changes and commit:
git commit -m "..." - Push:
git push origin feature/new-feature - Open a pull request on GitHub.
- After review, merge the PR into
main. - Delete the feature branch.
Exercise 2
Compare Git Flow and GitHub Flow. When would you choose one over the other?
Sample answer
Git Flow: Choose for projects with scheduled releases, complex versioning, and large teams. Provides clear separation of features, releases, and hotfixes.
GitHub Flow: Choose for continuous delivery, small to medium teams, projects with frequent deployments. Simple and fast.
Homework
Homework 1
Choose a branching strategy for your capstone project and write a 200‑word justification. Then create a branch structure diagram showing your branches and their relationships.
Sample outline
- Strategy: GitHub Flow (simple, continuous integration).
- Justification: Small team, frequent updates, easy to manage.
- Diagram: main → feature/* → PR → main.
Mini‑Project
Branching Strategy Workshop
Design a branching strategy for a team of 10 developers working on a financial application with quarterly releases. Create a detailed document that includes:
- Branch types and their purposes
- Naming conventions
- Workflow for features, releases, and hotfixes
- Merge and deployment process
Sample outline
- Branch types: main, develop, feature/*, release/*, hotfix/*.
- Naming: feature/ISSUE-123-short-desc, release/v1.0.0, hotfix/CRITICAL-123.
- Workflow: Features from develop → PR to develop → release from develop → PR to main and develop → hotfix from main → PR to main and develop.
- Deployment: main is production, releases are tagged.
Tutorial Summary
You learned the major Git branching strategies, including Git Flow and GitHub Flow, and their trade‑offs. You can now choose the right strategy for your team and project, and implement it effectively.
Key takeaway: A clear branching strategy is essential for team collaboration. Choose a strategy that matches your team's size, release frequency, and complexity.