Fundamentals 8 min read

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.

Coder Trainee
Coder Trainee
Coder Trainee
Mastering Git Branch Strategies: From Git Flow to Trunk-Based Development

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-name

Merging

# merge feature into main
git checkout main
git merge feature/login

# delete feature after merge
git branch -d feature/login

Viewing differences

# diff between two branches
git diff main..feature

# common ancestor
git merge-base main feature

# branch graph
git log --graph --oneline --all

Three 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/payment

Emergency 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.1

Common 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-payment

Quick 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 --all
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

software-developmentgitversion controltrunk-based-developmentbranchinggit-flowgithub-flow
Coder Trainee
Written by

Coder Trainee

Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.