Skip to content
subuilds.dev

← logbuild #12 passede56bf2b

GitLab Merge Blocked: Source Branch Behind Target

Learn why GitLab shows "source branch is X commits behind target branch" and how to fix it. Includes the sync-back rule that stops this problem from coming back.

· 7 min read

You merged a feature to develop and tested it. Then you created a merge request (MR) from develop to main, and got this:

The merge is blocked The source branch is 2 commits behind the target branch.

If this keeps happening, you are skipping one important step.

In Simple Words#

The problem
You open a merge request from develop to main, and GitLab blocks it. It says the source branch is behind the target branch.
The root cause
The last merge into main created a merge commit. That commit exists only on main, so develop does not have it.
The solution
After every merge to main, merge main back into develop and push. Then both branches have the same history.
Think of it like this
Two friends each keep a copy of the same notebook. One friend adds a page and does not share it, so the copies no longer match. If every new page is copied to the other notebook at once, the two copies stay the same.

The Problem#

A common case:

  1. Developer: Pull the latest develop, create a branch, build the feature, push, and open an MR to develop
  2. Admin: Approve and merge to develop. Staging looks good.
  3. Create an MR from develop to main: Blocked.
bash
The merge is blocked
The source branch is 2 commits behind the target branch.
1 commit and 1 merge commit will be added to main (squashes 34 commits).
Source branch will not be deleted.

Why?

The Root Cause#

When you merge develop into main, Git creates a merge commit on main. A merge commit is a commit that joins two branches. That commit exists only on main. Now main has history that develop does not have.

  1. Your work is on develop: commits 1 and 2. main does not have them yet.
  2. You merge develop into main. Git creates the merge commit M. It exists only on main.
  3. You add commit 3 and open the next merge request. GitLab blocks it, because develop does not have M.
  4. git merge origin/main on develop creates commit S. It brings M into develop.
  5. Now develop has everything that main has. The merge request is not blocked.
5 / 5

After the merge, main has a merge commit that develop does not have. The next time you try develop to main, Git sees that develop is “behind”, because that commit is missing.

The Fix: Sync Back After Every Merge to Main#

After every merge from develop to main, sync main back. This means you merge main into develop:

bash
# Step 1: Switch to develop
git checkout develop

# Step 2: Get the latest from remote
git fetch origin

# Step 3: Merge main into develop
git merge origin/main

# Step 4: Push the synced develop
git push origin develop

What Happens When You Run git merge#

git merge origin/main may open an editor (Nano or Vim). Git wants a message for the merge commit:

bash
Merge remote-tracking branch 'origin/main' into develop

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

If you see Nano (the default editor):

  1. The merge message is at the top
  2. Ctrl + O to save
  3. Enter to confirm
  4. Ctrl + X to exit

If you see Vim:

  1. Esc to enter normal mode
  2. :wq then Enter

To skip the editor:

bash
git merge origin/main --no-edit

The Golden Rule#

bash
git checkout develop
git fetch origin
git merge origin/main --no-edit
git push origin develop

Automating the Sync-Back#

GitLab CI/CD Pipeline#

Add this job to .gitlab-ci.yml. It syncs develop automatically after each merge to main:

yaml
sync-develop:
  stage: post-deploy
  rules:
    - if: $CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "push"
  script:
    - git checkout develop
    - git pull origin develop
    - git merge origin/main --no-edit
    - git push origin develop
  tags:
    - docker

Complete Workflow Example#

The full flow from feature to production, with the sync-back:

bash
# 1. Developer creates feature
git checkout develop
git pull origin develop
git checkout -b feature/awesome-feature

# Work on feature...
git add .
git commit -m "feat: add awesome feature"
git push origin feature/awesome-feature

# 2. Create MR: feature → develop (in GitLab UI)
# Get it reviewed and merged

# 3. After feature merged to develop, create MR: develop → main
git checkout develop
git pull origin develop

# Before creating MR, ensure develop is synced with main
git merge origin/main --no-edit
git push origin develop

# Now create MR in GitLab: develop → main

# 4. After MR merged to main, SYNC BACK!
git checkout develop
git fetch origin
git merge origin/main --no-edit
git push origin develop

Why GitLab Blocks the Merge#

GitLab’s merge checks stop a merge when the source is behind the target. This is a safety feature:

  • It stops you from overwriting commits on main
  • It makes sure you tested against the latest production code
  • It makes you fix conflicts before you merge

You find these checks in Settings, Merge Requests, Merge checks:

  • Pipelines must succeed: CI must pass
  • All discussions must be resolved: Every comment is answered
  • Source branch must be up to date: Blocks the merge if the source is behind the target

How to Prevent It#

1. Make Sync-Back a Team Rule#

Team rule: After every merge to main, sync main back to develop.

2. Use Protected Branches#

Set this up in GitLab:

  • main: Fully protected, needs approval
  • develop: Protected, allows merges from main

Quick Reference#

When you see “source branch is X commits behind target”:

bash
# Sync the source branch with target
git checkout develop          # or your source branch
git fetch origin
git merge origin/main         # or your target branch
git push origin develop

After every merge to main:

bash
git checkout develop
git merge origin/main --no-edit
git push origin develop

If you’re in Nano editor:

  1. Ctrl + O → Enter → Ctrl + X

If you’re in Vim editor:

  1. Esc → :wq → Enter

Summary#

“Source branch behind target” happens because main is not synced back to develop after merges. The fix:

  1. After every merge to main:

    bash
    git checkout develop
    git merge origin/main
    git push origin develop
  2. Make it a team habit, or automate it in CI/CD

  3. Never turn off the merge check. Fix the root cause