0
0
Gitdevops~5 mins

Trunk-based development in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Trunk-based development is a way to work on code where everyone shares one main branch called trunk or main. It helps avoid big conflicts by making small, frequent updates instead of long separate branches.
When a team wants to avoid complicated merges by integrating code daily.
When you want to keep your main branch always ready to release.
When you want to reduce bugs caused by long-lived feature branches.
When you want to encourage collaboration and quick feedback on code changes.
When you want to simplify your version control process for faster delivery.
Commands
Switch to the main branch where all changes are integrated.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Update your local main branch with the latest changes from the remote repository to avoid conflicts.
Terminal
git pull origin main
Expected OutputExpected
Already up to date.
--rebase - Reapply your changes on top of the updated main branch to keep history clean
Create a short-lived branch for your new feature or fix, based on the latest main branch.
Terminal
git checkout -b feature-xyz
Expected OutputExpected
Switched to a new branch 'feature-xyz'
Stage your changes to prepare them for committing.
Terminal
git add .
Expected OutputExpected
No output (command runs silently)
Save your changes locally with a clear message describing what you did.
Terminal
git commit -m "Add feature xyz with tests"
Expected OutputExpected
[feature-xyz abc1234] Add feature xyz with tests 3 files changed, 45 insertions(+), 2 deletions(-)
Switch back to the main branch to prepare for integration.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Make sure your main branch is up to date before merging your feature branch.
Terminal
git pull origin main
Expected OutputExpected
Already up to date.
Integrate your feature branch changes into the main branch.
Terminal
git merge feature-xyz
Expected OutputExpected
Updating abc1234..def5678 Fast-forward file1.txt | 10 +++++----- file2.txt | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-)
Send your updated main branch with the new feature to the remote repository for others to use.
Terminal
git push origin main
Expected OutputExpected
Enumerating objects: 10, done. Counting objects: 100% (10/10), done. Delta compression using up to 4 threads Compressing objects: 100% (6/6), done. Writing objects: 100% (7/7), 1.23 KiB | 1.23 MiB/s, done. Total 7 (delta 2), reused 0 (delta 0) To https://github.com/example/repo.git abc1234..def5678 main -> main
Delete your feature branch locally after merging to keep your workspace clean.
Terminal
git branch -d feature-xyz
Expected OutputExpected
Deleted branch feature-xyz (was def5678).
Key Concept

If you remember nothing else from this pattern, remember: always keep your main branch clean and integrate small changes frequently.

Common Mistakes
Working on a feature branch for too long without merging back to main.
This causes big conflicts and makes integration harder.
Make small, frequent commits and merge back to main often.
Not updating your local main branch before starting a new feature branch.
Your feature branch may be based on outdated code, causing conflicts later.
Always pull the latest main branch before creating a new feature branch.
Pushing feature branches directly to main without review or testing.
This can introduce bugs or unstable code into the main branch.
Use code review and testing before merging to main.
Summary
Switch to the main branch and update it before starting work.
Create short-lived feature branches for small changes.
Merge feature branches back into main frequently to avoid conflicts.
Push updates to the remote main branch to share with the team.
Delete feature branches after merging to keep the workspace clean.