← 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
developtomain, and GitLab blocks it. It says the source branch is behind the target branch. - The root cause
- The last merge into
maincreated a merge commit. That commit exists only onmain, sodevelopdoes not have it. - The solution
- After every merge to
main, mergemainback intodevelopand 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:
- Developer: Pull the latest
develop, create a branch, build the feature, push, and open an MR todevelop - Admin: Approve and merge to
develop. Staging looks good. - Create an MR from
developtomain: Blocked.
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.
- Your work is on
develop: commits 1 and 2.maindoes not have them yet. - You merge
developintomain. Git creates the merge commitM. It exists only onmain. - You add commit 3 and open the next merge request. GitLab blocks it, because
developdoes not haveM. git merge origin/mainondevelopcreates commitS. It bringsMintodevelop.- Now
develophas everything thatmainhas. The merge request is not blocked.
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:
# 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 developWhat 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:
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):
- The merge message is at the top
Ctrl + Oto saveEnterto confirmCtrl + Xto exit
If you see Vim:
Escto enter normal mode:wqthenEnter
To skip the editor:
git merge origin/main --no-editThe Golden Rule#
git checkout develop
git fetch origin
git merge origin/main --no-edit
git push origin developAutomating the Sync-Back#
GitLab CI/CD Pipeline#
Add this job to .gitlab-ci.yml. It syncs develop automatically after each merge to main:
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:
- dockerComplete Workflow Example#
The full flow from feature to production, with the sync-back:
# 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 developWhy 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”:
# 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 developAfter every merge to main:
git checkout develop
git merge origin/main --no-edit
git push origin developIf you’re in Nano editor:
Ctrl + O→Enter→Ctrl + X
If you’re in Vim editor:
Esc→:wq→Enter
Summary#
“Source branch behind target” happens because main is not synced back to develop after merges. The fix:
-
After every merge to
main:bashgit checkout develop git merge origin/main git push origin develop -
Make it a team habit, or automate it in CI/CD
-
Never turn off the merge check. Fix the root cause
Related Posts#
- Git Branching Strategy: A Practical Case - Complete workflow guide
- Fix Accidental Commit to Main - Recover from direct commits
- GitLab CI/CD for Astro & Cloudflare - Automate your deployments