DEV Community

Cover image for ๐Ÿงฑ Trunk-Based Development โ€” The Easiest Git Example You'll Ever See
Latchu@DevOps
Latchu@DevOps

Posted on

๐Ÿงฑ Trunk-Based Development โ€” The Easiest Git Example You'll Ever See

๐ŸŽฏ Goal: Learn what Trunk-Based Development is and how to use it using a very basic HTML project (myshop-demo).

๐Ÿ’ก No big branches. No long pull requests. Just fast commits to main โ€” like a continuous conveyor belt of improvements.


๐Ÿง  What Is Trunk-Based Development?

Everyone works in one branch, usually main, and commits small, frequent changes.

Optionally, short-lived feature branches (1โ€“2 hours max) are used and merged quickly.

** โœ… Pros โŒ Caution **
Simple, fast, and clean Risky without automated testing
Great for CI/CD & small teams Requires discipline (no broken code)
Ideal for feature flags & toggles Not for teams with slow release cycles

๐Ÿ›’ Project: MyShop Demo

Letโ€™s say you want to:

  • Add a message: โ€œWelcome Offer: 10% Offโ€
  • Add a wishlist button (optional toggle)

Instead of creating feature branches, weโ€™ll do everything in main.


๐Ÿงช Letโ€™s Do It โ€” Step-by-Step

โœ… Step 1: Initialize Your Project

mkdir myshop-demo
cd myshop-demo
git init
echo "<h1>Welcome to MyShop</h1>" > index.html
git add .
git commit -m "Initial MyShop homepage"

Enter fullscreen mode Exit fullscreen mode

โœ… Step 2: Make a Small Change on main

Letโ€™s simulate a welcome offer banner.

echo "<p>๐ŸŽ‰ Welcome Offer: 10% Off</p>" >> index.html
git commit -am "Add welcome offer banner"

Enter fullscreen mode Exit fullscreen mode

โœ… You just committed directly to main โ€” no feature branch.


โœ… Step 3: Add Wishlist Button Behind a Feature Flag

Simulate a feature toggle (this is common in Trunk-Based Dev):

echo "<!-- TODO: Show Wishlist button when feature flag enabled -->" >> index.html
git commit -am "Add wishlist TODO comment (feature flag off)"

Enter fullscreen mode Exit fullscreen mode

โœ… Feature is part of the codebase, but not active yet โ€” safe!


โœ… Step 4: Push to GitHub (Optional)

git remote add origin <your-repo-url>
git branch -M main
git push -u origin main

Enter fullscreen mode Exit fullscreen mode

Now your main branch reflects real-time updates.


๐Ÿง  Real-World Example

At companies like Netflix, Google, Facebook, developers:

  • Commit multiple times a day to main
  • Use automated tests + feature flags
  • Avoid long-lived branches and massive PRs

๐Ÿ’ก The focus is on fast feedback, stability, and speed.


๐Ÿ“Š Summary

Strategy Element Trunk-Based Way
Base Branch main
Branch lifespan 0โ€“1 day (or commit directly)
Testing Required via CI
Deployment Often automated (CD pipelines)
Feature flags Used to toggle incomplete code

๐Ÿง  When Should You Use Trunk-Based Dev?

โœ… You have CI/CD pipelines
โœ… You want to deploy fast and often
โœ… You can write small, safe changes
โœ… Your team values rapid feedback


๐Ÿš€ Bonus: Optional Short-Lived Branch (If Needed)

If you're nervous about breaking main, you can use a short branch:

git checkout -b wishlist-button
# Make changes
git commit -am "Add wishlist feature"
git checkout main
git merge wishlist-button

Enter fullscreen mode Exit fullscreen mode

Just merge it quickly โ€” donโ€™t let it sit for days!


๐Ÿ Final Thoughts

Trunk-Based Development isnโ€™t just about Git โ€” itโ€™s a mindset:
โ€œShip fast. Test often. Donโ€™t wait.โ€

โœ… Itโ€™s perfect for teams using:

  • GitHub Actions
  • GitLab CI/CD
  • CircleCI
  • Jenkins pipelines

๐Ÿงก Drop a like or comment โ€” or check out my next post on GitHub Flow or CI/CD-ready branching.

Top comments (3)

Collapse
 
vidakhoshpey22 profile image
Vida Khoshpey

So good ๐Ÿ‘๐Ÿป

Collapse
 
latchudevops profile image
Latchu@DevOps

Thanks Vida. I'm aslo Senior DevOps and Cloud Architect. You may connect me over GitHub - github.com/kohlidevops

I have done lot of projects and shared with everyone to learn!

Collapse
 
vidakhoshpey22 profile image
Vida Khoshpey

Wowww I followed you and let's learn and grow together ๐Ÿ˜๐Ÿ’ช๐Ÿป๐Ÿ’ช๐Ÿป๐Ÿ’ป

Some comments may only be visible to logged-in visitors. Sign in to view all comments.