Sub topic 16
Branching workflows for teams
This chapter assumes you can create, merge, and rebase branches, and that you know how remote-tracking branches and git push -u work. A workflow is a team agreement about which branches exist, who may update them, and how work reaches main. Git enforces none of it, so the team has to.
The feature branch workflow
Most teams start here. main always holds code that can be released. Every change is made on a short-lived branch and merged after review, usually through a pull request (a hosting-service feature that asks reviewers to approve a branch before it merges).
git switch main
git pull
git switch -c feature/login
# edit files, then commit as you go
git push -u origin feature/login
# after the pull request is merged on the server:
git switch main
git pull
git branch -d feature/login
git push origin --delete feature/loginCommon workflow models
| Model | Long-lived branches | Best fit |
|---|---|---|
| Feature branch | `main` | Most teams that review changes before merging |
| Trunk-based | `main` | Teams with strong automated tests who deploy often |
| Git Flow | `main`, `develop` | Products with scheduled, versioned releases |
In trunk-based development, branches live hours or a day or two. Unfinished features are merged behind a feature flag, a setting that keeps the code switched off. The long-lived branch is the thing to avoid, because the longer a branch lives, the more it diverges and the harder the merge conflicts become.
Git Flow adds a develop branch for integration, release/* branches for stabilizing a version, and hotfix/* branches cut from main for urgent fixes. It suits teams that support several released versions. For a web service deployed continuously, it adds ceremony without benefit. Backporting a fix to an old release is a place to use git cherry-pick.
Rules that make a workflow work
- Protect
mainon the hosting service. Require pull requests and passing checks, and block force pushes. - Pick one merge strategy per repository and write it down. Merge commits keep the true history. Squash merges give one commit per feature. Rebase-and-merge gives a linear history.
- Rebase only branches that you alone use. If a teammate has based work on your branch, rewriting it forces them to repair their history. Use
git push --force-with-leaseinstead of--forceso you don't overwrite commits you haven't fetched. - Sync often. Run
git fetchand merge or rebasemaininto your branch daily so conflicts stay small. - Delete branches after they merge. Stale branches hide which work is still active.
Common errors
- Committing directly to
main. The fix is branch protection, not discipline alone. - Branching from an outdated
main. Rungit pullbeforegit switch -c. - Giant branches. A branch with weeks of unrelated changes is hard to review. Split it into smaller branches.
- Unclear names. Use a consistent prefix such as
feature/,fix/, orrelease/so the branch list is readable.
Exercise
Your team deploys several times a day and has a good automated test suite. A teammate proposes Git Flow with a long-lived develop branch. Which workflow fits better, and why?
Show answer
Trunk-based development, or the plain feature branch workflow with short-lived branches. Frequent deployment means changes should reach main quickly. A long-lived develop branch delays integration and increases merge conflicts, and the release branches Git Flow adds are not needed when every merge to main can be released.