What is the right way to merge?
The right way to merge, in modern software development using Git, is to keep your main branch protected, develop on short-lived feature branches, rebase locally to clean up your work, and integrate via a reviewed pull request that passes automated checks—using a merge commit or squash merge per your team’s policy, and never rebasing public/shared branches. In practice, “right” means preserving history clarity and code integrity while minimizing risk, which can vary slightly by workflow (trunk-based, GitHub Flow, or GitFlow). If you meant merging documents or data rather than code, best practice focuses on conflict-free structure and auditability, which we cover below.
Contents
- The core principle: preserve history clarity and code integrity
- Recommended Git merge workflow in 2025
- Choosing a merge strategy
- Merge vs. rebase: when to prefer each
- Conflict handling and quality gates
- Repository settings that make merging safer
- Special cases
- If you meant “merging data or documents” instead
- Common mistakes to avoid
- Quick command reference
- Summary
The core principle: preserve history clarity and code integrity
Regardless of tooling, the goal of merging is to integrate changes without breaking users, losing context, or corrupting history. That means enforcing guardrails (reviews, CI, required checks), choosing a history model your team understands (merge commits vs linear), and handling conflicts deliberately, not hurriedly. In Git since 2.34+, the default “ort” merge strategy makes merges faster and more accurate, but process still matters most.
Recommended Git merge workflow in 2025
The following sequence reflects broadly accepted practice across GitHub, GitLab, and Bitbucket for teams aiming at reliability and traceability.
- Sync main:
git fetch origin;git switch main;git pull --ff-only. - Create a short-lived branch:
git switch -c feature/concise-name. - Commit in small, logical steps with clear messages; include tests/docs as you go.
- Rebase locally to keep your branch current:
git fetch origin;git rebase origin/main(resolve conflicts, rerun tests). - Push and open a PR/MR:
git push -u origin feature/concise-name. - Let CI run; fix issues until all required checks pass (tests, lint, security scans).
- Get code review approvals and address feedback with additional commits or fixups.
- Choose merge method per policy: merge commit, squash, or rebase-and-merge (see below).
- After merge, delete the feature branch; pull latest main locally.
- Tag releases and generate notes when relevant; monitor post-merge telemetry.
This workflow keeps integration disciplined, minimizes divergence, and ensures every merged change is test-validated and peer-reviewed, reducing regressions and simplifying audits.
Choosing a merge strategy
Different strategies trade off historical fidelity, readability, and ease of reverts. Pick one and document it as policy.
- Merge commit (default): Preserves full branch history and context; useful for multi-commit features. Use
--no-ffif you want an explicit merge even when fast-forward is possible. Easiest to revert entire features via the merge commit. - Squash merge: Produces a single commit on main, keeping history linear and tidy. Great for noisy branches and small features. Loses intra-branch commit boundaries on main (still available on the original branch pre-deletion).
- Rebase-and-merge: Rewrites the branch onto main, preserving individual commits but avoids a merge commit. Yields a linear history with granular commits. Never use it for branches others have already pulled; rebases rewrite history.
Teams that prioritize auditability often choose merge commits; teams that value a terse main line favor squash or rebase-and-merge. Consistency matters more than the specific choice.
Merge vs. rebase: when to prefer each
Both integrate changes, but they serve different purposes and risks. These rules of thumb help you decide quickly.
- Rebase locally to clean up your work before it’s shared; it keeps your branch current and avoids unnecessary merge commits.
- Merge to integrate public work; it preserves shared history and avoids forcing teammates to reconcile rewritten commits.
- Don’t rebase branches that are already pushed and consumed by others unless everyone agrees and you use
--force-with-leasesafely.
In short: rebase private, merge public. This balances neat history with team safety.
Conflict handling and quality gates
Conflicts are normal; what matters is how you resolve and verify them.
- Update and attempt the merge or rebase; when conflicts arise, use a focused diff/merge tool (
git mergetoolor IDE integration). - Resolve conflicts minimally; avoid mixing refactors with conflict fixes in the same hunk.
- Run the full test suite and linters locally after resolving; re-run CI.
- Leverage
git rererefor repeated conflict patterns (git config rerere.enabled true). - For risky merges, consider pairing and require a second review on conflict resolutions specifically.
- Sign your merge if your org requires provenance:
git merge --no-ff -Sor sign commits with GPG/Sigstore.
Treat conflict resolution as a change worthy of review and testing; most post-merge defects stem from rushed conflict handling.
Repository settings that make merging safer
Platform controls can enforce your process and reduce human error.
- Protect main and release branches; block direct pushes.
- Require status checks (CI, security scans, coverage thresholds) to pass before merging.
- Require reviews and CODEOWNERS for sensitive areas; enforce “conversations resolved.”
- Allow only approved merge methods (e.g., squash only) to standardize history.
- Enable signed commits/merges for supply-chain integrity.
- Auto-delete merged branches; enable auto-merge when checks/approvals complete.
- Configure
pull.rebase,rebase.autosquash, andfetch.prunefor cleaner local workflows. - Use
.gitattributesfor custom merge drivers (e.g., lockfiles, generated files) and line-ending normalization.
These guardrails shift correctness from habit to policy, reducing “it worked on my machine” risks and accidental history rewrites.
Special cases
Long-lived branches and release trains
If you use release branches, regularly merge forward (release → main) to avoid drift. Cherry-pick hotfixes to the oldest supported branch, then forward-merge to newer ones to keep code in sync.
Monorepos and large binaries
Adopt narrow, well-scoped changes; use sparse checkout/partial clone for performance. Keep large assets in Git LFS and avoid merging generated artifacts; regenerate them post-merge.
Open-source projects
Use DCO or CLA as required, enforce signed-off-by, and prefer merge commits or squash with clear PR context. Maintainers often rebase contributor branches locally before merging to tidy history without rewriting public main.
If you meant “merging data or documents” instead
Merging outside of Git focuses on preventing data loss and maintaining provenance.
- Word/Docs: Use track-changes and compare/merge tools; resolve by section and keep a changelog.
- Spreadsheets: Normalize structure first; use unique keys; merge via lookups or Power Query; audit with change tracking.
- CSV/JSON datasets: Define stable keys; validate schemas; use deterministic merge rules (source precedence, timestamp); keep an audit trail.
- Databases: Use staging tables; upsert with constraints; run data-quality checks; wrap in transactions.
The consistent theme: agree on identity keys, define conflict rules upfront, and record lineage so you can explain and, if needed, undo merges.
Common mistakes to avoid
These pitfalls cause most merge headaches and regressions.
- Rebasing branches others already pulled without coordination.
- Skipping tests after resolving conflicts.
- Merging with a dirty working tree or untracked generated files.
- Forgetting
--force-with-leasewhen pushing a legitimate history rewrite. - Line-ending and whitespace churn due to missing
.editorconfig/.gitattributes. - Neglecting submodules or LFS pointers, leaving repos in inconsistent states.
Avoiding these errors preserves trust in your history and reduces time spent firefighting after merges.
Quick command reference
These commands cover the most common, safe merge scenarios.
- Fast-forward main:
git switch main;git pull --ff-only - Create branch:
git switch -c feature/x - Rebase onto latest main:
git fetch origin;git rebase origin/main - Merge commit on main:
git switch main;git merge --no-ff feature/x - Squash merge via platform UI or locally:
git merge --squash feature/x;git commit - Resolve repeated conflicts faster:
git config rerere.enabled true
Stick to these patterns and you’ll avoid most history and conflict issues while keeping your repository healthy.
Summary
The right way to merge is a disciplined, policy-backed workflow: branch per change, rebase privately, review and test thoroughly, then integrate via a consistent merge strategy that your team understands. Protect main, automate checks, and treat conflict resolution as a first-class change. If your context is documents or data, the analogous principles are stable keys, explicit conflict rules, and traceable lineage. Consistency, not dogma, is what makes merges safe and sustainable.
What is the best merge strategy?
Best Practices for Merging Strategies
- Fast-Forward Merge. Occurs when the branch being merged has no diverging changes from the target branch.
- Three-Way Merge. Creates a new merge commit that combines changes from both branches.
- Rebase Merge.
- Squash Merge.
What are the 5 steps of merging?
Changing Lanes: What To Do
- Step #1: Turn on your turn signal.
- Step #2: Check your rearview and side mirrors.
- Step #3: Look over your shoulder to check your blind spot.
- Step #4: Change lanes!
- Step #5: Turn your turn signal off.
- Mistake #1: Take too long to do all the steps.
What is the proper way to merge?
To properly merge, first signal your intent to change lanes and check your mirrors and blind spot for a large enough gap in traffic. Accelerate on the ramp to match the speed of the traffic you’re merging into, and then smoothly and gradually steer your vehicle into the open space, maintaining a steady speed. If there isn’t a good gap, wait for one rather than forcing your way in.
Steps for Merging
- 1. Signal Early: Turn on your turn signal to let other drivers know you intend to merge.
- 2. Use the Acceleration Lane: Accelerate to match the speed of the highway traffic. This is crucial for a smooth and safe merge.
- 3. Check Mirrors and Blind Spot: Check your side mirrors and your rear-view mirror, then perform a shoulder check to ensure there is a safe, large enough gap in the lane you want to enter.
- 4. Find a Gap: Look for a sufficient space in traffic that allows you to enter the lane without causing other drivers to brake or swerve.
- 5. Enter Smoothly: Once a gap is found, gradually steer your car into the lane at a relaxed pace. Avoid sudden or abrupt movements.
- 6. Maintain Speed: After you’ve merged, keep your speed consistent with the flow of traffic and observe your surroundings.
This video explains how to merge like a zipper to help keep traffic moving: 56sNZ Transport Agency Waka KotahiYouTube · Mar 10, 2021
Tips for a Safe Merge
- Don’t Stop: Avoid stopping on the merging ramp if possible, as it can disrupt traffic flow.
- Yield: If another driver has created a gap for you, you should merge within a few seconds before they close it.
- Practice: If you’re a nervous driver, practice merging on a less busy highway on a Sunday to build confidence.
- Zipper Merge: In construction zones or heavy traffic, practice the “zipper merge,” where drivers in both the through lane and the lane that is ending take turns entering the single open lane.
What is the most efficient way to merge?
Did you know the zipper merge is the most effective way to keep traffic moving? Everyone stays in two lanes until the last possible second, then each car merges into the exit one at a time. It uses the most surface area on the highway to get people onto the exit.


