DevOps, CI/CD & Containers
Git Branching and Workflows: Merge, Rebase, Git Flow and Trunk-Based Development
Intermediate9 min readUpdated
Branches are cheap in Git, so the real decisions are about how long they live and how they are integrated. This guide explains fast-forward and three-way merges, rebasing and its rules, conflict resolution, and then compares the workflows teams actually use — feature branches with pull requests, Git Flow, GitHub Flow and trunk-based development — with guidance on which fits a given release model.
What you will learn
- Explain what a merge commit records and when Git fast-forwards instead
- Use rebase to keep history linear and know precisely when not to rebase
- Resolve merge conflicts methodically and avoid the common ways of botching them
- Compare Git Flow, GitHub Flow and trunk-based development and pick one for a team
- Apply cherry-pick, interactive rebase and protected branches to real release situations
A branch in Git costs nothing to create, which shifts the interesting questions from "should we branch?" to "how long does a branch live, how does it get back into the main line, and what does the history look like afterwards?". Different answers produce very different day-to-day experiences: a team that rebases short-lived branches onto main several times a day lives in a different world from one that maintains develop, release/* and hotfix/* branches with a merge ceremony for each. This guide explains the mechanics — merge, rebase, conflicts — precisely enough that you can predict what a command will do, then compares the workflows built on top of them.
Branches, merge base and fast-forward
A branch is a pointer to a commit. When two branches share a common ancestor and one has moved on, Git calls that ancestor the merge base. Merging brings the other branch's changes since the merge base into the current one.
If the current branch has not moved since the merge base — main has no new commits and feature has three — there is nothing to combine. Git simply moves the main pointer forward to the tip of feature. This is a fast-forward merge: no merge commit, a linear history, and no trace that a branch ever existed.
If both branches have moved, Git performs a three-way merge: it compares the merge base with each tip, applies both sets of changes, and records the result in a merge commit with two parents. The merge commit is what makes the branch visible in history afterwards.
git switch main
git merge feature # fast-forward if possible, merge commit otherwise
git merge --no-ff feature # always create a merge commit, even when a fast-forward was possible
git merge --ff-only feature # refuse to merge unless a fast-forward is possible
--no-ff is used by teams that want every feature to appear as a single merge commit in git log --first-parent, which lets them revert a whole feature with git revert -m 1 <merge-commit>. --ff-only is used by teams that insist on linear history and rebase branches before merging.
Rebase
git rebase main, run on feature, takes each commit on feature since the merge base, and re-applies it on top of the current tip of main, creating new commits with new hashes. The effect is as if you had started the branch today: your changes sit on top of everyone else's, history is linear, and a subsequent merge into main fast-forwards.
Rebase is the right tool for keeping a private branch current. It is the wrong tool for anything already shared, because the rewritten commits have different hashes from the ones colleagues fetched, and their next pull produces duplicate commits and baffling conflicts. The rule is short: never rebase commits that exist outside your repository unless everyone involved knows and agrees (a personal pull-request branch that only you push to is the common exception).
Interactive rebase (git rebase -i main) opens the list of commits about to be replayed and lets you reorder, squash, edit, reword or drop them. It is the way to turn eight "wip" commits into two coherent ones before opening a pull request.
pick 3f2a1c9 Add DiscountCode entity
squash 8b7d0e2 fix typo
squash c41e77a wip
pick 92ad5b1 Apply discount codes at checkout
reword d05f3a6 tests
After saving, Git replays the commits according to the instructions and prompts for messages where needed. Combined with git add -p, interactive rebase is what makes it possible to hand reviewers a small number of commits that each do one thing.
Resolving conflicts
A conflict happens when both sides changed the same lines (or one deleted a file the other edited) and Git cannot decide. It stops, marks the file, and waits.
<<<<<<< HEAD
BigDecimal total = subtotal.add(shipping);
=======
BigDecimal total = subtotal.add(shippingCost).add(tax);
>>>>>>> feature/tax
The section above ======= is the current branch (HEAD), the section below is the branch being merged. The procedure:
See what is conflicted
git statuslists the paths under "Unmerged paths".git diffshows the conflicts with both sides.Edit each file to the intended result
Remove the markers and write the code that should exist — often a combination of both sides, sometimes neither. Compile and run the tests before moving on.Mark it resolved
git add <file>tells Git the conflict is settled.Finish the operation
git merge --continue(orgit commit) for a merge;git rebase --continuefor a rebase, which may then present the next conflicting commit.--aborton either returns to the state before you started.
Merge tools (git mergetool, or the three-way view in IntelliJ IDEA and VS Code) show base, ours and theirs side by side, which is far easier than reading markers for anything beyond a line or two. Enabling rerere ("reuse recorded resolution") makes Git remember how you resolved a conflict and apply the same resolution automatically the next time it sees it — invaluable when rebasing a long branch repeatedly.
Two habits reduce conflicts more than any tool: integrate from main daily so divergence stays small, and avoid mass reformatting or file moves in the same commit as logic changes.
Workflows compared
With the mechanics in hand, the workflows are recipes for when to branch and how to merge.
Feature branches with pull requests
Each change gets a branch from main, work happens there, and a pull request (merge request in GitLab) collects review comments and CI results before the branch is merged and deleted. This is the baseline on GitHub, GitLab and Bitbucket. The variables are branch lifetime and merge style: branches kept to a day or two and merged with squash or rebase give a clean, linear main; branches that live for weeks recreate the integration problems continuous integration exists to avoid.
GitHub Flow
A deliberately minimal model: main is always deployable; every change is a branch and a pull request; merging to main deploys. There are no release branches, because a release is whatever main contains. It fits services deployed continuously and teams with strong automated tests.
Git Flow
The elaborate model with two permanent branches and three kinds of temporary ones:
| Branch | Branched from | Merged into | Purpose |
|---|---|---|---|
main | — | — | Production releases only, each tagged with a version |
develop | main | main via release | Integration branch for the next release |
feature/* | develop | develop | Individual features |
release/* | develop | main and develop | Stabilise a release: version bump, bug fixes only |
hotfix/* | main | main and develop | Urgent production fix |
Git Flow answers real questions for software with versioned releases and several supported versions in the field: where do fixes for version 2.3 go while 2.4 is being stabilised and 2.5 is in development? Its cost is ceremony and long-lived branches — develop can drift from main for weeks — and for a web service deployed several times a day it is simply overhead. The model's own author has noted that it was designed before continuous delivery became common practice.
Trunk-based development
Everyone commits to main (the trunk) at least daily, either directly or through pull requests that last hours. Incomplete features are kept dark with feature flags or by building the new path alongside the old ("branch by abstraction") rather than on a branch. Release branches, if used at all, are cut from trunk and receive only cherry-picked fixes, never new work. DORA's research associates this model with higher delivery performance, and it is the natural partner of continuous integration. It requires trust in the test suite, small changes and an investment in flagging; teams without those find it uncomfortable.
Choosing
| Situation | Reasonable choice |
|---|---|
| Web service deployed continuously, one production version | GitHub Flow or trunk-based |
| Product with numbered releases, one version supported | Feature branches plus a short-lived release branch per version |
| Library or on-premise product with several supported versions | Git Flow or a "release branches with cherry-picks" model |
| Large team, monorepo, strong automation | Trunk-based with feature flags |
| Regulated release approval | Any of the above, with the approval on the deployment, not the merge |
The recurring lesson is that the workflow should follow the release model, not the other way round.
Release branches, cherry-picks and tags
Even trunk-based teams sometimes need a release branch: a version has shipped to customers and needs a fix while main has moved on. The pattern is to branch release/2.4 from the tagged commit, fix the bug on main first (so it is never lost), then git cherry-pick <hash> onto the release branch, tag v2.4.1 there, and build from the tag. Fixing on main first avoids the classic failure where a fix goes only to the release branch and reappears as a regression in the next version.
Protect the important branches in the hosting platform: require pull requests, require the CI check to pass, forbid force-pushes and deletions on main and release/*. Protection rules cost nothing and prevent the most destructive accidents.
Commit conventions
A branching model works better with consistent commits. Conventional Commits (feat:, fix:, refactor:, with an optional scope and a ! for breaking changes) is a widely used format that tools can parse to generate changelogs and compute the next semantic version automatically. Whether or not you adopt the prefix format, keep the branch's commits meaningful after rebase, name branches by intent (fix/null-shipping-cost, feat/discount-codes) and reference the issue tracker in the pull request rather than in every commit.
Common mistakes
- Rebasing a shared branch. Colleagues get duplicate commits and conflicts on their next pull. Rebase only what is yours; merge everything else.
- Long-lived feature branches. Three weeks of work integrated at once produces a conflict marathon and hides integration bugs. Merge small pieces behind flags.
- Resolving a conflict by keeping "ours" everywhere. Blindly choosing one side discards the other person's change and reintroduces the bug they fixed. Read both sides.
- Merging
maininto a feature branch repeatedly, then merging back. The history becomes a lattice of merge commits that hides what the feature changed. Rebase private branches instead, or merge frommainsparingly. - Hotfix only on the release branch. The fix never reaches
mainand the bug returns in the next release. Fix forward first, then cherry-pick back. - Deleting the branch before the pull request is merged. On most platforms the pull request closes with it. Let the platform delete the branch on merge.
- Force-pushing without
--force-with-lease. Overwrites a colleague's commit pushed a minute earlier.
Key takeaways
- A merge fast-forwards when the target has not moved; otherwise it creates a merge commit with two parents recording the combination.
- Rebase re-creates commits on top of another branch and yields linear history; use it on private branches and never on commits others may have fetched.
- Resolve conflicts by reading both sides, editing to the intended result, testing, then
addand--continue;rerereand daily integration make conflicts rare and small. - GitHub Flow and trunk-based development suit continuous deployment; Git Flow suits versioned products with several supported releases; match the workflow to the release model.
- Fix on
mainfirst and cherry-pick to release branches, tag releases with annotated tags, and protectmainagainst force-pushes and unreviewed merges. - Small, well-named commits and branches, tidied with interactive rebase before review, make every workflow easier to live with.
Sources and further reading
Frequently asked questions
- Should I merge or rebase my feature branch onto main?
- Rebase while the branch is private to you: it keeps history linear and moves your commits on top of the latest main so the final merge is trivial. Once the branch is pushed and others may have based work on it, stop rebasing and merge instead. Many teams combine both: rebase locally to update, then squash-merge or merge with --no-ff through a pull request.
- Is Git Flow still recommended?
- Its own author has said it suits software with explicit versioned releases and multiple supported versions — desktop applications, libraries, on-premise products — and that web services deployed continuously are better served by a simpler model. If you deploy from main several times a day, the develop and release branches of Git Flow add ceremony without benefit.
- How do I get one bug-fix commit from main into an old release branch?
- Use git cherry-pick with the commit hash while the release branch is checked out. Git applies the same change as a new commit on the release branch. Cherry-picks work best when the original commit is small and self-contained; if it depends on other changes you will hit conflicts, and it is often cleaner to fix on the oldest supported branch first and merge forward.
- What is a squash merge and when is it a bad idea?
- A squash merge combines all commits from a branch into a single commit on the target, giving a clean one-commit-per-feature history. It is a good default for pull requests with messy work-in-progress commits. It is a bad idea when the branch's individual commits are meaningful — a refactor followed by a behaviour change — or when you will merge the same branch again later, because Git cannot tell the squashed commit came from that branch and will replay conflicts.