Trunk Based Development in Git: What It Is and How It Works
git is a workflow where all developers work on a single main branch called trunk or main. Developers make small, frequent commits directly to this branch or short-lived feature branches merged quickly, ensuring continuous integration and reducing merge conflicts.How It Works
Imagine a team working on a shared document where everyone writes on the same page instead of separate copies. In trunk based development, the "trunk" is like that shared page — the main branch where all changes happen. Developers either commit directly or create very short-lived branches that merge back quickly.
This approach keeps the codebase simple and up to date. Since everyone integrates their work often, it avoids big surprises or conflicts later. It’s like cleaning your room a little every day instead of letting it pile up.
Example
This example shows creating a short-lived feature branch, making a change, and merging it back to main quickly.
git checkout main # Make sure main is up to date git pull origin main git checkout -b feature-quick-fix # Make your changes here, e.g., edit a file git add . git commit -m "Quick fix on feature" git checkout main git merge feature-quick-fix git push origin main git branch -d feature-quick-fix
When to Use
Use trunk based development when you want fast feedback and fewer merge problems. It works well for small to medium teams that deploy often or practice continuous integration and delivery.
It’s great for projects where keeping the main branch stable and up to date is important, like web apps or services updated daily. Avoid long-lived branches that delay merging, as they can cause conflicts and slow down progress.
Key Points
- All work happens on a single main branch called
trunkormain. - Feature branches are short-lived and merged quickly.
- Frequent commits reduce merge conflicts and integration issues.
- Supports continuous integration and fast delivery.
- Helps teams keep code stable and up to date.