Mastering Git Branch Strategies: From Git Flow to Trunk-Based Development
This article explains Git's branch fundamentals, common commands, and compares three mainstream branching models—Git Flow, GitHub Flow, and Trunk-Based Development—highlighting their suitable scenarios, trade‑offs, and practical recommendations for teams of different sizes and release cadences.
Branch fundamentals
In Git a branch is a pointer to a commit. Creating a branch writes a 41‑byte file (40‑byte SHA‑1 plus a newline). Switching a branch moves the HEAD pointer, and deleting a branch removes the pointer file.
┌─────────────────────────────────────────────────────────────────┐
│ Branch is a pointer │
├─────────────────────────────────────────────────────────────────┤
│ │
│ main ──► [C0] ◄── HEAD │
│ │
│ Create feature branch: │
│ │
│ main ──► [C0] ◄── HEAD │
│ │ │
│ feature ──────┘ │
│ │
│ Commit on feature: │
│ │
│ main ──► [C0] │
│ │ │
│ feature ──► [C1] ◄── HEAD │
│ │
└─────────────────────────────────────────────────────────────────┘Common branch operations
Basic commands
# view branches
git branch # local branches
git branch -r # remote branches
git branch -a # all branches
# create branch
git branch feature/login # create without checkout
git checkout -b feature/login # create and checkout
git switch -c feature/login # Git 2.23+ recommended
# switch branch
git checkout main # traditional way
git switch main # Git 2.23+ recommended
# delete branch
git branch -d feature/login # delete merged branch
git branch -D feature/login # force delete unmerged branch
# rename branch
git branch -m old-name new-nameMerging
# merge feature into main
git checkout main
git merge feature/login
# delete feature after merge
git branch -d feature/loginViewing differences
# diff between two branches
git diff main..feature
# common ancestor
git merge-base main feature
# branch graph
git log --graph --oneline --allThree mainstream branch strategies
Git Flow (classic)
Suitable for projects with fixed release cycles (e.g., traditional software, games). Main branches:
master/main – production code, permanent.
develop – development line, permanent.
feature/* – new feature development, temporary.
release/* – release preparation, temporary.
hotfix/* – urgent fixes, temporary.
Typical deployment frequency: weekly or monthly.
GitHub Flow (lightweight)
Designed for continuous deployment of web applications. Only one long‑living branch ( main ). All changes happen on short‑lived feature branches, submitted via Pull Request, reviewed, merged, and deployed immediately. Deployment can occur multiple times per day.
Trunk‑Based Development
Everyone works directly on main . Feature branches live less than a day, merges happen frequently, and feature toggles control what is released. Requires mature automated testing. Deployment can be hourly.
Choosing a strategy
Git Flow – high complexity, large teams, weekly/monthly releases.
GitHub Flow – medium complexity, medium‑small teams, daily deployments.
Trunk‑Based – low complexity, mature teams, hourly deployments.
Common scenarios
Developing a new feature
# 1. create branch from main
git checkout main
git pull origin main
git checkout -b feature/payment
# 2. develop and commit
git add .
git commit -m "feat: 完成支付功能"
# 3. push remote
git push origin feature/payment
# 4. create PR, merge, delete local branch
git checkout main
git pull origin main
git branch -d feature/paymentEmergency hotfix
# 1. create hotfix from production
git checkout main
git checkout -b hotfix/1.0.1
# 2. fix and commit
git add .
git commit -m "fix: 修复支付回调超时问题"
# 3. merge into main and develop (Git Flow)
git checkout main
git merge hotfix/1.0.1
git checkout develop
git merge hotfix/1.0.1
# 4. delete hotfix branch
git branch -d hotfix/1.0.1Common pitfalls
Long‑lived branches
Branches that stay alive for more than three days increase merge‑conflict difficulty. Recommended lifespan < 3 days.
Pushing directly to main
Changes should go through a Pull Request / Merge Request and code review before reaching main.
Unclear branch names
# Bad names
git checkout -b test
git checkout -b fix
git checkout -b a
# Good names
git checkout -b feature/user-login
git checkout -b bugfix/order-status
git checkout -b hotfix/1.0.1-paymentQuick command reference
# view branches
git branch
git branch -r
git branch -a
# create and switch
git checkout -b <name>
git switch -c <name>
# merge
git merge <branch>
# delete
git branch -d <branch>
# view graph
git log --graph --oneline --allSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Coder Trainee
Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
