GitLab’s Merge Requests (MRs) are more than just a way to merge code; they’re a powerful platform for collaborative code review and quality assurance.

Imagine you’re working on a new feature. You’ve committed your changes locally, and now you want to get them into the main project branch. This is where a GitLab Merge Request shines. Instead of just pushing your code, you create an MR. This MR becomes a dedicated space where your changes are presented, discussed, and polished before they’re integrated.

Let’s see this in action. Suppose you’ve made a change to a Python file, app/models/user.py, adding a new validation rule.

--- a/app/models/user.py
+++ b/app/models/user.py
@@ -10,3 +10,4 @@
         self.email = email
         self.password_hash = self._hash_password(password)
         self.is_active = True
+        self.username = username
 

You push this to a new branch, feature/add-username-validation, and then create a Merge Request targeting the main branch.

GitLab presents this MR with several key components:

  • Changes Tab: This is where you see the exact lines added, removed, or modified. It’s a diff, but within a structured interface. You can see + self.username = username highlighted.
  • Discussion/Notes: This is the heart of the review. Reviewers can leave comments directly on specific lines of code, ask questions, suggest alternatives, or approve changes.
  • Pipelines: If you have CI/CD configured, GitLab automatically runs your tests and builds on the MR. You’ll see a status indicator (running, passed, failed) right in the MR.
  • Assignees & Reviewers: You can assign specific people to review your code or request reviews from team members.
  • Labels & Milestones: For organization, you can apply labels (e.g., bug, feature, documentation) and link the MR to a project milestone.

The problem MRs solve is the chaos of ad-hoc code merging. Without them, you’d have developers pushing directly to main, leading to merge conflicts, unreviewed code, and a much higher risk of introducing bugs. MRs create a formal process, ensuring that every piece of code is inspected by at least one other set of eyes before it goes live.

Internally, GitLab treats an MR as a special kind of branch. It’s a pointer to a commit, and the target branch is another pointer. The "changes" you see are the differences between the commit pointed to by your source branch and the commit pointed to by your target branch. When you "merge," GitLab essentially creates a new commit on the target branch that incorporates the changes from the source branch.

The levers you control are numerous:

  • git merge --no-ff (or equivalent in MR settings): This ensures that even if a merge could be a fast-forward, a merge commit is created. This preserves the history of the branch, making it clear that a distinct set of changes was introduced as a feature or fix.
  • Code Owners: You can define specific files or directories that must be approved by designated "code owners" before an MR can be merged. This is crucial for maintaining expertise and accountability in larger teams.
  • Approval Rules: You can set rules for the number of approvals required, or require approvals from specific users or groups, before an MR can be merged.
  • Merge Strategy: GitLab offers different merge strategies, including merge commit, squash, and rebase. Squash merges are popular for cleaning up commit history by combining all commits from a feature branch into a single commit on the target branch, making the history cleaner.

When you’re setting up your MR workflow, remember that the "Changes" tab isn’t just for viewing diffs; it’s a canvas for discussion. You can click the + icon next to any line to start a new discussion thread directly tied to that specific piece of code. This context is invaluable for reviewers trying to understand your intent or suggest precise improvements.

Beyond basic approvals, GitLab MRs facilitate the integration of automated quality gates. You can configure merge request pipelines that must pass before an MR can be merged, and even set up "approvals" that are automatically granted if these pipelines succeed, streamlining the review process for well-tested code.

The next step after mastering Merge Requests is to explore GitLab’s CI/CD integration for automated testing and deployment, which often works hand-in-hand with your MR workflow.

Want structured learning?

Take the full Gitlab course →