Sub topic 16
Branching Workflows for Teams
This chapter assumes you can create, merge, and rebase branches, push with an upstream, and read git log --graph. It covers how teams turn those mechanics into an agreed process, which common workflows exist, and where each one fails.
Why a team needs a workflow
Git does not care how you use branches. Any branch can merge into any other, and any branch can be force-pushed. A workflow is a set of team rules that answers four questions:
- Which branches are long-lived, and what does each one mean?
- Where do new work branches start, and where do they end up?
- How does a change get reviewed before it lands?
- How are merged changes shipped and, when needed, fixed after release?
Without agreed answers, you get surprises such as two people fixing the same bug on different branches, or a half-finished feature reaching production because it was merged into the wrong place.
Short-lived and long-lived branches
Workflows differ mostly in how they mix two kinds of branch.
- A long-lived branch exists for the life of the project. Examples are
main,develop, andrelease/1.0. Other branches merge into it, and it is rarely or never deleted. - A short-lived branch (also called a topic or feature branch) holds one change. It is created from a long-lived branch, merged back, and deleted.
The shorter a topic branch lives, the less it drifts from main, and the smaller its merge conflicts will be. Most of the advice in this chapter reduces to keeping topic branches small and short.
Workflow 1: Feature branches with a protected main
This is the simplest workflow that works for most teams. It is often called GitHub flow, after the hosting service that popularized it.
mainis always in a deployable state.- Every change starts as a topic branch created from the latest
main. - You push the branch and open a pull request (a request on the hosting service, also called a merge request on GitLab, asking to merge your branch).
- Teammates review it and automated checks run.
- After approval, the branch is merged into
mainand deleted.
The commands for one change look like this:
git switch main
git pull --ff-only
git switch -c feature/login
# edit files, then commit
git add .
git commit -m "Add login form"
git push -u origin feature/logingit pull --ff-only refuses to create a merge commit. If your local main has diverged from origin/main, the command fails, which tells you that something unexpected happened on main locally.
The hosting service can enforce the rules. A protected branch setting on main typically blocks direct pushes, requires a passing build, and requires a number of approvals. Enforcing rules in the server is more reliable than relying on people to remember them.
Workflow 2: Gitflow
Gitflow uses several long-lived branches and a fixed set of short-lived branch types.
| Branch | Lifetime | Starts from | Merges into |
|---|---|---|---|
| `main` | Long-lived | n/a | n/a. Holds released versions, each tagged |
| `develop` | Long-lived | `main` | `release/*` |
| `feature/*` | Short-lived | `develop` | `develop` |
| `release/*` | Short-lived | `develop` | `main` and `develop` |
| `hotfix/*` | Short-lived | `main` | `main` and `develop` |
Features accumulate on develop. When the team wants to ship, it cuts a release/* branch, which accepts only bug fixes and version bumps. The release branch then merges into main (where it gets a tag) and back into develop, so fixes made during stabilization are not lost.
The diagram below shows the flow.
Gitflow fits software with explicit versions that must be supported in parallel, such as a desktop application or a library with several maintained releases. It is a poor fit for a web service deployed many times a day. The extra branches add merge work, and develop can hold unreleased changes for weeks, which makes integration problems appear late.
Workflow 3: Trunk-based development
In trunk-based development, everyone integrates into one branch (the trunk, usually main) at least daily. Topic branches, if used, live for hours rather than days. Unfinished work is hidden from users by a feature flag, a configuration switch that turns code on or off at runtime, instead of by keeping it on a separate branch.
The trade-off is that it demands strong automation. Tests must be fast and reliable, because a broken trunk blocks everyone. It also requires the team to split work into small, safely mergeable steps. In return, you rarely face large merge conflicts, because no branch drifts far.
Choosing a workflow
| Situation | Reasonable choice |
|---|---|
| Web service, continuous deployment, good test coverage | Trunk-based development, or feature branches with short lifetimes |
| Small team, simple release process | Feature branches with a protected `main` |
| Several released versions maintained at once | Gitflow, or `main` plus `release/*` branches |
| Open source project with outside contributors | Forks and pull requests |
You can also mix approaches. A common compromise is feature branches for daily work plus a release/x.y branch cut from main only when a version needs long-term support. Fixes land on main first and are then backported to the release branch with git cherry-pick.
Forks versus shared repository
In a shared repository model, every contributor can push branches to the same remote. This suits a team that trusts its members.
In a fork model, each contributor copies the repository to their own account on the hosting service. They push branches to their fork and open a pull request against the original. Maintainers need not grant write access to anyone. Locally, a fork setup usually has two remotes:
git remote add upstream https://example.com/original/project.git
git fetch upstream
git switch main
git merge --ff-only upstream/main
git push origin mainHere origin is your fork and upstream is the original project. The commands bring your fork's main up to date with the original. Using --ff-only makes the command fail if you committed to main by mistake, instead of silently creating a merge commit.
Merge strategies on the hosting service
When a pull request is accepted, hosting services usually offer three options. Your choice shapes the history that every later reader sees.
| Option | Result on `main` | Trade-off |
|---|---|---|
| Merge commit | All branch commits plus a merge commit | Preserves full history. Graph is busier |
| Squash and merge | One new commit holding the whole change | Linear and tidy, but individual commits are lost |
| Rebase and merge | Branch commits replayed one by one, no merge commit | Linear and keeps commits, but the commits get new hashes |
Squash merging has a side effect. The squashed commit is not a Git merge, so Git does not know the branch was merged. Running git branch --merged will not list it, and git branch -d will refuse to delete it. You must use git branch -D after confirming the pull request was merged.
Team conventions that prevent trouble
- Never rewrite shared history. Do not rebase or force-push
mainor any branch other people have based work on. When you must update your own pushed topic branch after a rebase, usegit push --force-with-lease, which fails if someone else pushed in the meantime. - Pull before you branch, and update during long work. Rebasing or merging
maininto your branch every day or two keeps conflicts small. - Name branches consistently, such as
feature/login,fix/null-session, or a ticket ID prefix. Consistent names let tools and people filter them. - Delete merged branches. Many hosting services can do this automatically on merge. Locally,
git fetch --pruneremoves remote-tracking branches that no longer exist on the remote. - Use tags for releases. A tag such as
v1.4.0marks the exact commit that shipped, no matter how branches move later. - Keep pull requests small. Review quality drops sharply as the diff grows.
A cleanup routine that many people run weekly:
git fetch --prune
git branch --merged main
git branch -d feature/loginThe second command lists local branches whose tips are already reachable from main. Only delete what the list shows, and remember that squash-merged branches will not appear in it.
Common failure modes
- Long-running feature branch. It drifts from
main, and the final merge becomes a large, risky conflict resolution. Fix it by mergingmainin regularly, or by splitting the work. - Two people share one topic branch and one rebases. The other's local copy now has commits that no longer match the remote. Agree that a topic branch has one owner, or that nobody rewrites it.
- Hotfix forgotten on
developormain. In multi-branch workflows, a fix applied to one branch has to reach the others. Make "merged into every target branch" a checklist item. - Direct pushes to
main. Turn on branch protection so the process does not depend on discipline.
Exercise
Your team deploys a web service several times a day and uses feature branches. A squash-merged branch feature/search is still on your machine, and git branch -d feature/search fails with "not fully merged". Why does it fail, and what should you do?
Show answer
Squash and merge creates a new commit on main that holds the same changes but is not a Git merge, so the branch's original commits are not reachable from main and Git considers the branch unmerged. Confirm on the hosting service that the pull request was merged, then delete the branch with git branch -D feature/search.
Exercise
Your team maintains versions 2.x and 3.x of a library at the same time and ships fixes to both. Which workflow from this chapter fits best, and how would you get a bug fix made on main onto the 2.x line?
Show answer
Gitflow, or main plus long-lived release/* branches, fits because several released versions are supported in parallel. Make the fix on main, then switch to the 2.x release branch and run git cherry-pick <commit-sha> to apply that commit there.