Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Projects and Merge Requests

Merge Methods and Protected Branches

Choose between merge commits, semi-linear history and fast-forward merges, use squash, and protect branches and tags so main stays safe.

How a merge request lands on main decides what your history looks like for years. GitLab lets each project pick a merge method and a squash policy, and lets you protect important branches so nobody can rewrite or bypass review. This lesson explains the options and when to pick each.

The three merge methods

Open the project's Settings, then Merge requests, and look for the merge method.

  1. Merge commit (the default). GitLab always creates a merge commit, even if a fast-forward would be possible. History shows exactly where each branch joined. It is the most faithful record but the graph gets busy.
  2. Merge commit with semi-linear history. GitLab creates a merge commit, but only if the source branch is already rebased on the latest target. If main moved, the author must rebase first. The graph stays tidy with real merge points.
  3. Fast-forward merge. No merge commit at all. The target branch simply moves forward to the MR's last commit, which requires the branch to be rebased first. History is a straight line.
gitGraph
  commit id: "A"
  commit id: "B"
  branch feature
  checkout feature
  commit id: "F1"
  commit id: "F2"
  checkout main
  commit id: "C"
  merge feature id: "merge !7"

With the merge commit method, "merge !7" has two parents: C (main) and F2 (the feature).

Let us see the three shapes in a stand-in that models histories as lists of commit names. It is a toy model of the ideas, not Git itself.

Python Stand-in: how each merge method shapes history

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The primes (F1') mark commits that were rewritten by a rebase: they carry the same changes but new identifiers, because their parent changed.

Which to choose?

  • Merge commit fits teams that value a complete, unaltered record and use features like revert of an entire merge.
  • Semi-linear is a good compromise: readable graph, explicit merge points, CI results that match what lands.
  • Fast-forward gives the simplest log and works well with a strict one-commit-per-change habit (often combined with squash).

Squash commits

Squash combines every commit of the MR into one when merging. Settings let you choose:

  • Do not allow: squashing is not offered.
  • Allow: the checkbox exists and is off by default.
  • Encourage: the checkbox exists and is on by default.
  • Require: every merge is squashed.

Squashing hides messy work-in-progress commits ("fix typo", "oops") and gives you one reviewed, revertible unit per MR. GitLab prefills the squash message from the MR title (or the single commit) and lets you edit it, and can use a template. The downside is that useful fine-grained history is lost, so keep MRs small and focused. Squash plus fast-forward yields one tidy commit per MR on a straight line.

Merged results pipelines and merge trains

Passing CI on your branch does not guarantee that it passes after merging, because main may have changed. Two features address this:

  • Merged results pipelines test the combination of your branch and the target before merging. They are enabled in settings and require the pipeline to run for merge requests (see variables and rules).
  • Merge trains (Premium) queue merge requests and test each one on top of the ones ahead of it, so the target branch never breaks, without authors rebasing again and again.

Protected branches

By default, every branch can be pushed to by Developers and force-pushed by anyone with rights. That is fine for feature branches but dangerous for main. A protected branch adds rules:

  • Who may push (for example No one, Maintainers, or Developers and Maintainers).
  • Who may merge (usually Developers and Maintainers, so that merge requests are the route in).
  • Whether force push is allowed (leave off).
  • Whether code owner approval is required (paid tiers).
  • Protected branches cannot be deleted by ordinary users.

Find them under Settings, then Repository, then Protected branches. The default branch is protected when the project is created. A common secure setup is "Allowed to push: No one; Allowed to merge: Developers + Maintainers". Wildcards work too: protecting release/* covers every branch whose name starts with release/.

Protected tags work the same way: restrict who can create tags like v*, so release tags cannot be forged. CI/CD variables can be limited to protected branches and tags, which keeps production secrets away from feature branches (more in the variables lesson).

This stand-in checks permissions for push and merge against a protection rule.

Python Stand-in: may this person push or merge?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Putting it together

A solid default for a team project:

  1. Protect main: no direct pushes, merges allowed for Developers and Maintainers, no force push.
  2. Require a passing pipeline and resolved threads.
  3. Use semi-linear history or fast-forward merges with squash encouraged.
  4. Enable "delete source branch after merge" so stale branches disappear.
  5. Protect release tags and restrict who can create them.

These rules are the guardrails that make the pipelines you build later trustworthy.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which merge method never creates a merge commit?

    Fast-forward simply moves the target branch pointer forward, producing a straight line of commits.
  2. 2. What does squash do when merging?

    Squash collapses the branch's commits into a single commit on the target.
  3. 3. Which setup makes main safest?

    Protecting main forces changes through review and CI, and disabling force push prevents history rewrites.
  4. 4. What is the purpose of merged results pipelines?

    They run CI on the result of merging, catching problems that only appear in the combination.

Summary

  • Choose between merge commit, semi-linear history and fast-forward merges per project.
  • Squash gives one commit per MR; settings control whether it is optional or required.
  • Merged results pipelines and merge trains test the merged state, not just the branch.
  • Protect main, release branches and release tags so changes must come through review.
  • Wildcards such as release/* protect families of branches.

Next: Branching Strategies and GitLab Flow