Skip to content
subuilds.dev

← logbuild #8 passedf071749

Git Branching Strategy: A Practical Case

A simple Git workflow based on GitHub Flow / trunk-based development: one main branch, short feature and hotfix branches, and a preview for every PR. It works for 3 developers and for 300.

· 13 min read

Broken code reaches production on a Friday night. A merge conflict shows up days too late. Do you know these problems?

Most Git workflow guides are written for very large teams that ship versioned software. For a modern web or SaaS product, you need something simpler. This is true for a small team and for a large one.

This guide covers GitHub Flow / trunk-based development. It has three parts:

  • One long-lived branch, main.
  • Short-lived feature/* branches. You test each one on a preview deployment, a temporary copy of the site built from that branch.
  • hotfix/* branches for urgent production fixes.

It is the same pattern that works from 3 developers to 300 at Google, Shopify, GitHub, and Netflix.

In Simple Words#

The problem
Everyone pushes straight to main. Broken code reaches production, merge conflicts grow, and nobody knows which code is deployed where.
The root cause
The team has no shared rules for branches. Nothing tests or reviews a change before it lands on main.
The solution
Keep one protected branch, main. Do each change on a short-lived branch and open a PR (pull request). Test it on a preview URL, get one review, then squash-merge.
Think of it like this
main is the kitchen that serves the guests. You do not try a new recipe there. You cook it on a side table, another cook tastes it, and only then it goes on the menu.

Why Bother With Git Strategy?#

Without rules, people push straight to main. Merge conflicts grow. Nobody knows which code is deployed where.

A simple workflow fixes that:

  • No Friday disasters — branch protection catches mistakes before production
  • Code gets reviewed — review is required, not optional
  • Bugs are caught before production — every PR (pull request) gets a preview deployment
  • New developers start fast — they know where to push on day one

The Branching Model#

One long-lived branch, two temporary ones:

Branch Purpose Lifetime Protected
main Production. Always deployable. Permanent ✅
feature/* New features and changes Temporary ❌
hotfix/* Emergency production fixes Temporary ❌

The key idea: every PR gets a preview deployment. A PR asks the team to merge your branch into main. You test on the preview URL of the feature branch, not on a separate long-lived “staging” branch. When the preview passes, the feature merges to main and goes to production.

  1. main is production. It is protected, so nobody can push to it directly.
  2. Create feature/user-auth from main. Commit small and push often.
  3. Open a PR to main. CI runs the tests and deploys a preview URL.
  4. A teammate reviews the code, and someone tests on the preview URL. If they ask for changes, you commit again.
  5. Squash-merge: the PR becomes one commit on main. Tag it v1.1.0, production deploys, and the branch is deleted.
  6. A production bug? A short hotfix/payment-timeout branch goes the same way and gets the tag v1.1.1.
6 / 6

Walkthrough: Two Devs, Parallel Features#

Sarah builds auth (the login). Mike builds the dashboard. They work on two features at the same time, with no problems.

Phase 1: Project Setup (one-time)#

bash
git init
git commit -m "chore: initial commit"
git push -u origin main

Protect main in the GitHub/GitLab settings (explained below).

Phase 2: Sarah Builds Auth#

bash
git checkout main && git pull
git checkout -b feature/user-auth

# Work, commit small, push often
git add src/auth/
git commit -m "feat(auth): add login form + JWT"
git push -u origin feature/user-auth

Sarah opens a PR: feature/user-auth → main. CI is the automatic pipeline that runs on every push. It runs the tests and deploys a preview URL. Vercel, Cloudflare Pages, Netlify, ArgoCD, or another tool in your stack can do this. Mike reviews the code. QA or Sarah clicks through the preview to check that the auth flow works. When everything passes:

bash
# Squash-merge via the PR UI, then tag from main:
git checkout main && git pull
git tag -a v1.1.0 -m "Release v1.1.0: auth"
git push origin v1.1.0

The automatic deploy to production starts from the tag. It can also start from the merge to main; this depends on your CI. The branch is deleted.

Phase 3: Mike Builds Dashboard (in parallel)#

bash
git checkout main && git pull   # picks up Sarah's auth
git checkout -b feature/dashboard

# ...work, commit, push, PR → main

Mike works from the latest main, which already has Sarah’s auth. His PR gets its own preview URL. The flow is the same: review, test on the preview, squash-merge, tag.

Phase 4: Production Bug at 11pm#

A bug in payment processing breaks checkouts. You cannot wait for a normal release.

bash
git checkout main && git pull
git checkout -b hotfix/payment-timeout

# Fix, commit, push
git commit -m "fix(payment): increase API timeout to 30s"
git push -u origin hotfix/payment-timeout

# PR: hotfix/payment-timeout → main (fast-track review)
# After merge, tag the patch:
git checkout main && git pull
git tag -a v1.1.1 -m "Hotfix: payment timeout"
git push origin v1.1.1

That is all. There is no second branch to update after the fix. The hotfix is on main, and main is the only source of truth: the one place everyone trusts.

Branch Protection#

Branch protection is a set of rules on the Git server. The rules decide who can change a branch, and how. Set this once in GitHub/GitLab. One branch is protected:

main

  • 1 approval required
  • CI must pass (build + tests + security scan)
  • Preview deployment must succeed (status check)
  • No force push

Skip signed commits, conversation resolution, and branch deletion protection. At this team size they only add work.

Naming Conventions#

Branches#

<type>/<short-description> — kebab-case (lowercase words joined with -), 2–4 words.

✅ Good ❌ Bad
feature/user-auth fix-stuff
feature/payment-integration johns-branch
bugfix/login-redirect-loop temp
hotfix/payment-timeout wip

Rules: lowercase, kebab-case, no special characters except / and -. Add the issue number when you have one (feature/123-user-dashboard).

Commits#

Three types cover 95% of the commits you will write:

text
feat: add JWT refresh mechanism
fix: resolve timeout in user API
chore: update dependencies

That is all. docs, refactor, test, and ci exist for when you need them. But do not make the team learn the full list on day one.

Tags#

Use semver (semantic versioning) on main: vMAJOR.MINOR.PATCH.

  • v1.0.0 — initial release
  • v1.1.0 — new feature (backward compatible)
  • v1.1.1 — bug fix
  • v2.0.0 — breaking change
bash
git tag -a v1.2.0 -m "Release v1.2.0: dashboard"
git push origin v1.2.0

Pull Requests#

Title#

<type>: <what you did>, 50–72 characters.

✅ feat: add user notification system ✅ fix: resolve login redirect loop on expired tokens ❌ Update stuff / WIP / Final fix v2

Description (keep it short)#

Three things:

  • What changed — one paragraph
  • Why — link the issue, or explain what changes for the user
  • How to test — exact steps the reviewer can run on the preview URL

Skip the long company template with 7 sections. If those three things fit in five sentences, the PR is small enough. That is a good PR.

Reviewer checklist#

  • Follows team conventions
  • No obvious bugs or security issues
  • Tests cover the change
  • Preview deployment verified
  • No debug code or commented-out blocks left

Merge strategy#

Squash and merge for everything. Squash means that all commits of the PR become one commit. One PR = one commit on main. The history stays clean and in one line. Reverts are easy, and git bisect is easy.

Handling Merge Conflicts#

A merge conflict means your branch and main changed the same lines. Git cannot choose for you.

bash
git checkout feature/user-profile
git merge main
# CONFLICT (content): Merge conflict in src/components/Profile.tsx

Open the file and fix the part between the conflict markers:

text
<<<<<<< HEAD
your changes
=======
changes from main
>>>>>>> main

Then:

bash
git add src/components/Profile.tsx
git commit -m "merge: resolve conflicts with main"
git push

If the conflict is in code you did not write, fix it together with the person who wrote it. Do not guess.

Automating Quality Checks with Git Hooks#

Git hooks are scripts that Git runs before a commit or a push. They catch bad code before it reaches the repo: failing tests, bad commit messages, hardcoded secrets.

It takes 5 minutes to set up. It saves hours of debugging.

Branch Lifecycle#

How a feature branch goes from creation to deletion:

  1. main is production. It is protected, so nobody can push to it directly.
  2. Create feature/user-auth from main. Commit small and push often.
  3. Open a PR to main. CI runs the tests and deploys a preview URL.
  4. A teammate reviews the code, and someone tests on the preview URL. If they ask for changes, you commit again.
  5. Squash-merge: the PR becomes one commit on main. Tag it v1.1.0, production deploys, and the branch is deleted.
  6. A production bug? A short hotfix/payment-timeout branch goes the same way and gets the tag v1.1.1.
6 / 6
Branch type Lifespan
feature/* 1–7 days
bugfix/* 1–3 days
hotfix/* 2–4 hours
main permanent

If a feature branch is more than a week old, expect slow reviews and old conflicts. If it is more than two weeks old, split the feature into smaller parts.

Troubleshooting#

”I committed to wrong branch”#

bash
# You're on main but should be on a feature branch.
git checkout -b feature/my-feature   # carries your commits
git checkout main
git reset --hard origin/main         # rewinds main to remote
git checkout feature/my-feature      # your work is here, safe

“I need to undo my last commit”#

bash
git reset --soft HEAD~1   # undo commit, keep changes staged
git reset --hard HEAD~1   # undo commit, discard changes
git revert HEAD           # if already pushed — creates a new "undo" commit

“My feature branch is behind main”#

bash
git checkout feature/my-feature
git fetch origin
git merge origin/main

Cheat Sheet#

bash
# Start a feature
git checkout main && git pull
git checkout -b feature/name

# Sync with main mid-flight
git checkout main && git pull && git checkout - && git merge main

# Finish (after PR is squash-merged)
git checkout main && git pull
git branch -d feature/name

# Hotfix
git checkout main && git pull
git checkout -b hotfix/name
# ...fix, push, PR to main, merge, tag
Branch Approvals Auto-deploys to
main 1 Production
feature/* — Preview URL (per PR)
hotfix/* 1 (fast-track) Production (after merge)

Scaling This Workflow#

The same pattern works from 3 developers to 300. You add two things as the team grows:

  1. Preview deployments per PR — every PR gets its own short-lived URL. A PR can merge to main only after it passes the test on its preview. Tools: Vercel, Cloudflare Pages, Netlify, AWS Amplify, ArgoCD preview environments, GitLab Review Apps.

  2. Feature flags — a feature flag is a switch in your code that turns a feature on or off. Merge the code behind a flag and ship it to production at once. Turn the flag on when the feature is ready for users. This separates deployment (continuous) from release (controlled). It is how big companies deploy hundreds of times a day without breaking anything. Tools: LaunchDarkly, Unleash, GrowthBook, Flagsmith, or your own if (flags.X) table.

You do not need them on day one. Add them when:

  • You are tired of waiting for a shared dev environment → add preview-per-PR
  • You want to merge half-finished features without showing them to users → add feature flags

Branch protection, squash merges, and a clean linear main history work at every team size. Only the infrastructure around them changes.

Summary#

This workflow gives you:

  • No Friday disasters — branch protection blocks mistakes
  • Code reviews that really happen — required, not optional
  • Bugs caught before production — the preview for every PR is your last check
  • A linear main history — git bisect, revert, and audit are easy
  • A workflow that grows with you — the same pattern at 3 developers and at 300

Protect main this week. Add preview deployments in the next sprint. Add feature flags when the shared dev environment is not enough any more. Do not add develop and staging branches. They create more problems than they solve.

More Resources#