Git Workflows: GitHub Flow, Gitflow and Trunk-Based

Updated · 12 min read
mediumGit 2.53gitworkflowsgithub-flowgitflowtrunk-basedinterview

A Git workflow (or branching strategy) is a team's agreement on how to use branches: which long-lived branches exist, where new work starts, how changes are reviewed and merged, and how releases and urgent fixes are made. Git itself doesn't enforce any workflow. It gives you branches, merges and tags, and the team decides the rules. Choosing one, writing it down and enforcing it with protected branches is what lets several people change the same project every day without stepping on each other.

Every kitchen has its own routine: one cook prepares and serves each dish alone, another kitchen has stations that pass plates down a line, and a big restaurant has a separate pastry section with its own timing. All of them make food. They organize the same work differently, according to their size and menu.

The Common Workflows

WorkflowLong-lived branchesNew work starts fromTypical for
Centralizedmain onlyCommits directly on mainOne or two people, small scripts
Feature branchmainA branch per feature, merged when doneMost small teams
GitHub flowmain (always deployable)A short branch per change, merged via pull request, deployed right afterWeb apps and services deployed continuously
Gitflowmain (releases) and develop (integration)develop, plus release/* and hotfix/* branchesSoftware with versioned, scheduled releases
Trunk-basedmain (the trunk)Very short branches (hours), or small commits straight to mainTeams with strong automated tests, continuous delivery
ForkingYour fork plus the originalA branch in your fork, pull request to the originalOpen-source projects

The forking workflow is covered in "Forking a Repository". The examples below build each of the others in a small repository, so you can compare the shape of the history they leave.

The same team, three ways of branching

GitHub Flow

GitHub flow has one rule above all: main is always deployable. Every change, however small, goes through the same steps:

  1. Create a branch from main with a descriptive name.
  2. Commit, push, and open a pull request.
  3. Review, and let the automated checks pass.
  4. Merge into main, and deploy.
  5. Delete the branch.
Terminal
mkdir github-flow
cd github-flow
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Start the project"
for feature in late-fee receipts exam-fee; do
  git switch --quiet -c "feature/$feature"
  echo "$feature" >> features.txt
  git add features.txt
  git commit --quiet -m "Add $feature"
  git switch --quiet main
  git merge --quiet --no-ff --no-edit "feature/$feature"
  git branch --quiet -d "feature/$feature"
done
git log --oneline --graph
Output
*   14b5bda (HEAD -> main) Merge branch 'feature/exam-fee'
|\
| * d07ff62 Add exam-fee
|/
*   62c8bbc Merge branch 'feature/receipts'
|\
| * e39db23 Add receipts
|/
*   2ded88f Merge branch 'feature/late-fee'
|\
| * b701929 Add late-fee
|/
* 80f35b1 Start the project

Explanation: each feature lived on its own short branch, was merged into main with a merge commit (as a pull request's merge button does), and the branch was deleted. The history is a series of small bumps on a straight main. With "Squash and merge", it would be a straight line of one commit per feature.

Gitflow

Gitflow separates the code being developed from the code that is released. It uses two long-lived branches and three kinds of short-lived ones:

BranchBranches fromMerges intoPurpose
mainOnly released versions, each tagged
developmainIntegration of finished features
feature/*developdevelopOne feature
release/*developmain and developStabilizing a version: only fixes, version number, notes
hotfix/*mainmain and developUrgent fix for a released version
Terminal
cd ..
mkdir gitflow
cd gitflow
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Start the project"
git switch --quiet -c develop
git switch --quiet -c feature/late-fee
echo "late fee" >> features.txt
git add features.txt
git commit --quiet -m "Add the late fee"
git switch --quiet develop
git merge --quiet --no-ff --no-edit feature/late-fee
git switch --quiet -c release/1.0
echo "1.0" > VERSION
git add VERSION
git commit --quiet -m "Set version 1.0"
git switch --quiet main
git merge --quiet --no-ff --no-edit release/1.0
git tag v1.0
git switch --quiet develop
git merge --quiet --no-ff --no-edit release/1.0
git switch --quiet -c hotfix/1.0.1 main
echo 'late fee is $250' >> features.txt
git commit --quiet -am "Fix the late fee text"
git switch --quiet main
git merge --quiet --no-ff --no-edit hotfix/1.0.1
git tag v1.0.1
git switch --quiet develop
git merge --quiet --no-ff --no-edit hotfix/1.0.1
git log --oneline --graph --all
Output
*   d5d479d (HEAD -> develop) Merge branch 'hotfix/1.0.1' into develop
|\
* \   1ed8217 Merge branch 'release/1.0' into develop
|\ \
| | | *   31e2f13 (tag: v1.0.1, main) Merge branch 'hotfix/1.0.1'
| | | |\
| | | |/
| | |/|
| | * | f7574ea (hotfix/1.0.1) Fix the late fee text
| | |/
| | *   46e3278 (tag: v1.0) Merge branch 'release/1.0'
| | |\
| | |/
| |/|
| * | 45a2d86 (release/1.0) Set version 1.0
|/ /
* |   6ca0a8b Merge branch 'feature/late-fee' into develop
|\ \
| |/
|/|
| * 71293cf (feature/late-fee) Add the late fee
|/
* 7d50364 Start the project

Explanation: the feature went into develop. A release branch was cut from develop, given its version, and merged into main (tagged v1.0) and back into develop. The hotfix started from main, and was merged into both. Even for one feature, one release and one fix, the graph has many merge commits and two lines that must be kept in step. That is Gitflow's cost.

Gitflow fits software that is shipped in versions and supported for a while: desktop or mobile apps, libraries, on-premise products. For a web application deployed many times a day, the author of Gitflow has since recommended simpler workflows such as GitHub flow.

Trunk-Based Development

Trunk-based development keeps everyone on main (the "trunk"). Changes are small, and either committed directly to main or on branches that live only a few hours and are rebased before merging. Unfinished features are hidden behind feature flags (settings that switch code on or off) instead of long-lived branches.

Terminal
cd ..
mkdir trunk-based
cd trunk-based
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Start the project"
for step in "Add the late fee behind a flag" "Show the late fee on receipts" "Switch the late fee flag on"; do
  git switch --quiet -c short-lived
  echo "$step" >> changes.txt
  git add changes.txt
  git commit --quiet -m "$step"
  git switch --quiet main
  git merge --quiet --ff-only short-lived
  git branch --quiet -d short-lived
done
git log --oneline --graph
Output
* 623ae6f (HEAD -> main) Switch the late fee flag on
* 046c423 Show the late fee on receipts
* 79ac6b0 Add the late fee behind a flag
* 0e05736 Start the project

Explanation: every change was small, reached main within its own short branch, and merged as a fast-forward. The history is one straight line. This only works with a strong safety net: automated tests on every commit, and feature flags for anything not ready to be seen. Large companies use it with thousands of developers precisely because no branch ever drifts far from main.

Choosing a Workflow

If your team...Consider
Is one or two people on a small projectFeature branches, or even centralized
Deploys a web application continuouslyGitHub flow
Ships numbered releases and supports old versionsGitflow, or GitHub flow plus release branches
Has strong automated tests and deploys many times a dayTrunk-based development
Accepts contributions from outsideForking, on top of any of the above

A common middle way is GitHub flow plus release branches: everyone works with short branches and pull requests on main, and when a version ships, a release/1.0 branch is created from it. Fixes go to main first and are backported to the release branch with git cherry-pick -x, as in "Git Cherry-Pick".

Enforcing the Workflow

A workflow written in a document is easy to forget. Hosting platforms enforce it:

SettingEffect
Protected branch for main (and develop, release/*)No direct pushes, no force pushes, no deletion
Required pull request reviewsAt least one (or two) approvals before merging
Required status checksThe build and tests must pass before merging
Allowed merge methodsOnly merge commits, only squash, or only rebase, for a consistent history
Merge queue (GitHub) or merge trains (GitLab)Busy repositories test each pull request together with the ones queued before it, so main never breaks

"Editing Code Online" shows where these settings are on each platform. Write the chosen workflow in CONTRIBUTING.md, so newcomers know it.

Common Mistakes

Copying Gitflow by habit. For a web app deployed daily, its develop and release branches add work without benefit.

Long-lived feature branches in any workflow. The longer a branch lives, the harder its merge. Merge or rebase from main often, and split big features.

No written rules. Everyone follows a slightly different workflow. Write it down and enforce it with branch protection.

Committing straight to main without tests in "trunk-based". Trunk-based development depends on automated tests and feature flags. Without them, it is just risky.

Mixing merge methods randomly. One squash, one rebase, one merge commit: the history becomes hard to read. Pick one per repository.

Under the Hood

Workflows use only ordinary Git: every branch in this lesson is the same kind of pointer, and every integration is a merge, a fast-forward, a squash or a cherry-pick. What differs is which branches exist and which direction work flows between them. That is why you can change workflow at any time without migrating anything: create or stop using branches, change the protection rules, and update CONTRIBUTING.md.

Interview Corner

What is GitHub flow?

A lightweight workflow where main is always deployable. Each change is made on a short-lived branch, reviewed in a pull request with automated checks, merged into main, deployed, and the branch is deleted.

What is Gitflow, and what are its drawbacks?

A branching model with main for released versions and develop for integration, plus feature/*, release/* and hotfix/* branches. It suits versioned releases, but adds many merges and long-lived branches, which is heavy for continuously deployed applications.

What is trunk-based development?

A workflow where everyone integrates small changes into main (the trunk) at least daily, using very short-lived branches or direct commits, automated tests and feature flags instead of long-lived branches.

How do you handle a hotfix for a released version?

Create a branch from the release tag or release branch, fix and test it, release it (tag v1.0.1), and bring the fix into main (Gitflow merges the hotfix branch into both. GitHub flow plus release branches fixes main first and backports with git cherry-pick -x).

How can a team make sure everyone follows the workflow?

Protect important branches on the hosting platform: require pull requests, reviews and passing checks, forbid force pushes, and allow only the agreed merge method. Document the workflow in CONTRIBUTING.md.

Practice

Challenge: count the merges. In the three example repositories, count the merge commits on main with git rev-list --count --merges main.

What does Git print? Before you open the solution, look at the three graphs and predict each count.

Show solution
Terminal
cd ..
for repo in github-flow gitflow trunk-based; do
  echo "$repo: $(git -C "$repo" rev-list --count --merges main)"
done
Output
github-flow: 3
gitflow: 3
trunk-based: 0

GitHub flow has one merge commit per feature: three. Gitflow's main also has three: the release merge and the hotfix merge, plus the feature's merge into develop, which became part of main's history when the release (cut from develop) was merged. The whole repository has even more, the merges back into develop. Trunk-based development has none: every change was a fast-forward. git -C <folder> runs Git as if you were in that folder.

Checkpoint. For a team of three building the Fee Tracker, write a one-page CONTRIBUTING.md that chooses a workflow, explains why, names the branches, the merge method and the review rules, and lists the branch protection settings to switch on.

Quick check: Git workflows

4 questions
1In GitHub flow, what is always true of main?
2In Gitflow, where do feature branches start?
3What does trunk-based development use instead of long-lived feature branches?
4How does a platform enforce a workflow?

Key Takeaways

Quick reference

CommandIt is used to
git switch -c feature/<name>Start work in a feature-branch workflow
git merge --no-ff <branch>Merge with a merge commit (GitHub flow, Gitflow)
git merge --ff-only <branch>Keep a linear trunk
git switch -c hotfix/<v> <tag>Start a hotfix from a release
git cherry-pick -x <commit>Backport a fix to a release branch
git rev-list --count --merges mainCount merge commits on main

Was this tutorial helpful?

Lesson 69 of 106