GitlabIntermediate

Automating GitLab Merge Requests: From Release to Development

DeveloperHat Team
9 min read

TL;DR

Learn how to automate GitLab merge requests and set up auto-merging from release to development branches, improving your team's workflow efficiency.

Automating GitLab Merge Requests: From Release to Development

Executive Summary

Hotfixes land on a release branch under pressure. If those changes are not carried back into the development branch promptly, the next release silently reintroduces the bug that was just fixed. This guide shows how to automate the release-to-development synchronisation in GitLab: create the back-merge request automatically, let it merge itself when the pipeline is green and approvals are satisfied, and route conflicts to a human instead of letting branches drift. After reading it you will be able to decide which parts of that flow are safe to automate, which guardrails must stay, and when a long-lived release branch should not exist at all.

Why This Matters

Branch drift is not an inconvenience; it is a defect factory. A fix merged only to release will be overwritten by the next merge from development if nobody notices. A development branch that lags the release branch produces conflicts that grow larger and riskier the longer they wait. Manual back-merges also depend on the person who made the hotfix remembering, documenting, and completing a second merge while the incident that caused the hotfix is still unfolding. Automating the creation of the merge request removes the forgetting failure. Automating the merge itself, under pipeline and approval controls, removes the waiting failure. What must never be automated away is conflict resolution: a conflict is information that two changes disagree, and resolving it is engineering judgement, not plumbing.

Core Concepts

The Back-Merge Pattern

The pattern applies to branching models with a long-lived release branch and a long-lived development branch, such as Gitflow-style workflows. Work normally flows development to release. Hotfixes reverse the direction for a single change. The automation therefore has three parts: detect a merge into release, open a merge request from release to development if one is not already open, and merge it when it is safe. Keep at most one open synchronisation request at a time; a queue of stale sync requests is worse than none, because each one conflicts with the next.

Branch Protection Is the Foundation

Automation is only safe on top of protected branches. Direct pushes to release and development should be restricted, merges should require a passing pipeline, and the token or account the automation uses needs merge permission and nothing beyond it.

# .gitlab/branch-protection.yml
release:
  protect: true
  allowed_to_push:
    - maintainers
  allowed_to_merge:
    - maintainers

development:
  protect: true
  allowed_to_push:
    - developers
  allowed_to_merge:
    - maintainers

In current GitLab these rules are configured in project settings or through the protected branches API rather than a committed file; the file above is a useful record of intent for review. The point stands either way: decide who can push, who can merge, and whether approvals are required, before you automate anything.

Merge Request Templates Set the Contract

A synchronisation merge request should look the same every time so reviewers can process it quickly and automation can rely on its checklist.

# .gitlab/merge_request_templates/release-to-dev.md
## Release to Development Merge

**Branch:** Release ${CI_COMMIT_REF_NAME} → Development

### Changes Included
- [ ] Feature updates
- [ ] Bug fixes
- [ ] Documentation updates

### Pre-merge Checklist
- [ ] Pipeline passes
- [ ] No conflicts
- [ ] Required approvals obtained

Use the template to state what the request is, link the original hotfix, and name anything that must not be carried back, such as a release-only version bump or a temporary workaround that development should implement properly instead.

Creating the Merge Request from CI

A scheduled or branch-triggered pipeline job can call the GitLab API to open the sync request. Prefer the API with a scoped project access token over any unofficial helper command, and make the job idempotent: query for an existing open release-to-development request first, and exit successfully if one exists.

create_merge_request:
  stage: sync
  script:
    - |
      if [[ $CI_COMMIT_BRANCH == "release" ]]; then
        gitlab-api create_merge_request \
          --source-branch release \
          --target-branch development \
          --title "Sync: Release to Development" \
          --description "Automated sync from release branch" \
          --remove-source-branch false \
          --squash false
      fi
  rules:
    - if: $CI_COMMIT_BRANCH == "release"
      changes:
        - "**/*"

Two details matter. First, do not squash a synchronisation merge and do not remove the source branch; you want history preserved so development reflects what release actually contains. Second, decide the trigger deliberately. Creating the request on every push to release is responsive but noisy during an incident with many pushes. A pipeline schedule that checks a few times a day is calmer and usually sufficient.

Merge When Pipeline Succeeds

GitLab's auto-merge, historically labelled merge when pipeline succeeds, is the control that turns an open request into a hands-off merge without removing the safety gate. Enable it on the synchronisation request once it is created, provided the project requires a green pipeline, required approvals are configured, and the request is not in a conflicted or draft state. Set the project's merge method deliberately: a merge commit preserves the release history most faithfully for back-merges, while fast-forward and semi-linear options impose rebase discipline that a long-running sync request may not survive.

Conflicts Need a Human, Promptly

Detect conflicts and escalate them as work, not as a failed automation statistic. The merge request itself shows the conflict; pair that with a notification to the team channel and an assignee, normally the author of the change that went into release.

#!/bin/bash
# scripts/resolve-conflicts.sh

# Check for conflicts
if git merge-tree $(git merge-base HEAD release) HEAD release | grep -e "^<<<<<<<<"; then
  echo "Conflicts detected. Creating resolution branch..."
  git checkout -b conflict-resolution/release-to-dev
  git merge release --no-commit
  # Notify team about conflicts
  curl -X POST "${GITLAB_API_URL}/projects/${CI_PROJECT_ID}/merge_requests" \
    --header "PRIVATE-TOKEN: ${GITLAB_API_TOKEN}" \
    --data "title=Resolve conflicts: Release to Development" \
    --data "description=Manual conflict resolution required"
fi

Resolve the conflict on a short-lived branch, review it like any other change, and merge it into development. Never resolve a sync conflict by choosing one side wholesale without reading both; the conflict usually marks the exact place where the hotfix and a newer feature disagree about behaviour.

Failed Pipelines and Retries

A red pipeline on the sync request means one of three things: the back-merge genuinely breaks development, the development branch is already broken, or the job infrastructure failed. Retry only the third category automatically.

sync_branches:
  stage: sync
  retry:
    max: 2
    when:
      - runner_system_failure
      - stuck_or_timeout_failure
  script:
    - ./scripts/sync-branches.sh

Test failures must block the merge and be investigated. Automatically retrying a failing test until it passes teaches the team to ignore the signal the pipeline exists to provide.

Notifications

Notify on creation and on conflict, not on every successful sync. A short message with the request link and status is enough for the team to know the loop is closed.

notify_team:
  stage: .post
  script:
    - |
      curl -X POST "${SLACK_WEBHOOK_URL}" \
        -H "Content-Type: application/json" \
        -d "{
          \"text\": \"Merge request created: Release → Development\n
          Status: ${CI_JOB_STATUS}\n
          URL: ${CI_MERGE_REQUEST_URL}\"
        }"

Decision Framework

SituationRecommendation
Hotfix merged to release, development is activeOpen the back-merge request immediately and enable auto-merge
Sync request has conflictsStop automation, assign the hotfix author, resolve on a dedicated branch
Release branch receives many pushes during an incidentCreate the sync request from a schedule instead of on every push
Change is release-only, such as a version bump or temporary workaroundExclude it deliberately and record the exclusion in the request description
Release branch is short-lived and cut per releasePrefer cherry-picking individual fixes over a standing sync request
Team works trunk-based with feature flagsDo not build this automation; there is no long-lived branch pair to synchronise

Implementation Checklist

  • [ ] Release and development branches are protected, with direct pushes restricted and pipelines required
  • [ ] The automation token is a scoped project token with merge rights and no broader access
  • [ ] The creation job checks for an existing open sync request before creating a new one
  • [ ] Sync merges use a merge commit, are not squashed, and do not delete the release branch
  • [ ] Auto-merge is enabled only when the pipeline is green and required approvals are met
  • [ ] Conflicts assign a named owner and notify the team channel
  • [ ] Automatic retries are limited to runner and infrastructure failures, never test failures
  • [ ] Release-only changes have an explicit, documented exclusion path
  • Trade-offs

    Automation keeps branches close, but it also merges code into development at machine speed, which means a bad hotfix propagates faster if the pipeline does not actually exercise the changed behaviour. The sync request can become a large, hard-to-review bundle if hotfixes accumulate, trading many small reviews for one risky one. Maintaining the automation itself, tokens, templates, and rules, is ongoing work that only pays off while the two-branch model exists. There is also a subtle cultural cost: if back-merges are invisible, developers stop thinking about whether a change belongs on release at all.

    When Not to Use

    Do not build release-to-development automation if your team can remove the problem instead. Trunk-based development with feature flags eliminates the branch pair and the sync entirely, and is the better answer for teams deploying continuously. If releases are cut as short-lived branches from the main branch and discarded, cherry-pick the rare fix rather than maintaining standing automation. If your regulator or change process requires a named human to approve every movement into a controlled branch, auto-merge is the wrong choice; automate the request creation and the reminders, and keep the merge manual. Finally, if conflicts occur on most syncs, the branches have diverged beyond what automation can repair, and the branching model itself needs to change.

    Conclusion

    Automate the bookkeeping, not the judgement. Let GitLab open the back-merge request, watch the pipeline, and merge when the agreed conditions are met. Keep conflicts, exclusions, and release-only changes in front of a human. Done that way, a hotfix stops being a future regression waiting to happen.

    References

  • GitLab Merge Requests
  • GitLab Auto-Merge
  • GitLab Protected Branches
  • GitLab Merge Methods
  • Why This Matters

    Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.

    Trade-offs to Consider

    Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.

    When NOT to Use This

    Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.

    Decision Framework

    Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.

    Share this articleShare on XShare on LinkedIn

    Keep the decision frameworks coming

    One practical architecture guide a week — patterns, trade-offs, and when not to use them. No spam, unsubscribe anytime.