Trunk-Based Development: A Guide to Efficient Software Collaboration

Listen to this Post

Trunk-based development is one of the most widely used branching methodologies. It helps teams collaborate and build and deliver software efficiently. Below are the core pillars of trunk-based development, along with practical commands and code snippets to implement this methodology.

Core Pillars of Trunk-Based Development

1. Single Source of Truth

The trunk is the main branch, also known as the mainline or trunk, and is the single source of truth. All changes need to propagate to this at regular intervals.
Git Command to Create and Switch to Main Branch:

git checkout -b main

2. Code Review

Reviews are the core of trunk-based development since changes are frequently committed. A robust review process should be in place to enable frequent code merges.

Git Command to Push Changes for Review:

git push origin main

3. Frequent Commits

Developers should commit small, incremental changes frequently to avoid large merge conflicts.

Git Command to Commit Changes:

git add .
git commit -m "Small incremental change"

4. Automated Testing

Automated tests ensure that the main branch is always in a deployable state.

Example of Running Tests:

pytest tests/

5. Continuous Integration (CI)

CI tools like Jenkins or GitHub Actions can be used to automate the build and test process.

Example GitHub Actions Workflow:

name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: pytest tests/

What Undercode Say

Trunk-based development is a powerful methodology that promotes collaboration, reduces merge conflicts, and ensures a stable main branch. By adhering to its core principles, teams can deliver software faster and with higher quality. Below are some additional commands and practices to enhance your workflow:

  • Rebasing to Keep History Clean:
    git pull --rebase origin main
    

  • Squashing Commits for Clean History:

    git rebase -i HEAD~3
    

  • Checking Branch Status:

    git status
    

  • Viewing Commit History:

    git log --oneline
    

  • Creating Feature Branches:

    git checkout -b feature/new-feature
    

  • Merging Feature Branches:

    git checkout main
    git merge feature/new-feature
    

  • Resolving Merge Conflicts:

    git mergetool
    

  • Automating Deployment with CI/CD:
    Use tools like Jenkins, Travis CI, or GitHub Actions to automate your deployment pipeline.

  • Monitoring Build Status:

    jenkins-cli build-status <job-name>
    

  • Rolling Back Changes:

    git revert <commit-hash>
    

  • Stashing Changes Temporarily:

    git stash
    

  • Applying Stashed Changes:

    git stash apply
    

  • Tagging Releases:

    git tag -a v1.0 -m "Release version 1.0"
    git push origin --tags
    

  • Cleaning Up Local Branches:

    git branch --merged | grep -v "*" | xargs -n 1 git branch -d
    

  • Viewing Remote Repositories:

    git remote -v
    

  • Fetching Latest Changes:

    git fetch origin
    

  • Resetting to a Previous Commit:

    git reset --hard <commit-hash>
    

  • Creating Patches:

    git format-patch -1 <commit-hash>
    

  • Applying Patches:

    git apply <patch-file>
    

  • Checking Out Remote Branches:

    git checkout -b <branch-name> origin/<branch-name>
    

  • Deleting Remote Branches:

    git push origin --delete <branch-name>
    

  • Viewing Git Configuration:

    git config --list
    

  • Setting Upstream Branch:

    git branch --set-upstream-to=origin/main main
    

  • Viewing File Changes:

    git diff
    

  • Ignoring Files with .gitignore:
    Add files to `.gitignore` to exclude them from version control.

  • Cloning a Repository:

    git clone <repository-url>
    

  • Creating a New Repository:

    git init
    

  • Adding a Remote Repository:

    git remote add origin <repository-url>
    

  • Pushing Changes to Remote:

    git push origin main
    

  • Pulling Latest Changes:

    git pull origin main
    

By following these practices and commands, you can ensure a smooth and efficient trunk-based development workflow. For more detailed insights, read the full article here: Trunk-Based Development.

References:

Hackers Feeds, Undercode AIFeatured Image