DEV Community

Deep Fix
Deep Fix

Posted on

Fix Git Merge Conflicts in CI/CD Pipelines – Proven Strategies for DevOps

Introduction

In fast‑moving CI/CD environments, merge conflicts can stall deployments and waste valuable time. This guide walks you through detecting, diagnosing, and automatically resolving Git merge conflicts directly in your pipeline.

Common Causes of Merge Conflicts in CI/CD

  • Parallel feature branches that touch the same files.
  • Infrastructure‑as‑Code changes (e.g., Terraform, Helm) applied simultaneously.
  • Automated version bumps performed by bots.

Step‑by‑Step Resolution

  1. Detect the conflict early Add a pre‑merge check to your pipeline so the build fails before it reaches production:
   # .gitlab-ci.yml
   stages:
     - check
   merge_check:
     stage: check
     script:
       - git fetch origin $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
       - git merge --no-commit --no-ff origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME || exit 1
Enter fullscreen mode Exit fullscreen mode
  1. Automate conflict markers cleanup A small helper script can abort a failing merge and force a safe strategy:
   # resolve-conflict.sh
   #!/usr/bin/env bash
   set -e
   git merge --abort || true
   git checkout $TARGET_BRANCH
   git pull
   git merge $SOURCE_BRANCH --strategy-option=ours
Enter fullscreen mode Exit fullscreen mode

Run it in a job:

   resolve_conflict:
     stage: resolve
     script:
       - chmod +x resolve-conflict.sh
       - ./resolve-conflict.sh
Enter fullscreen mode Exit fullscreen mode
  1. Configure a custom merge driver For files that are frequently edited (e.g., package.json), tell Git how to merge them automatically:
   git config --global merge.packagejson.driver "node merge-package-json.js %A %O %B %L"
Enter fullscreen mode Exit fullscreen mode

Add the driver to .gitattributes:

   package.json merge=packagejson
Enter fullscreen mode Exit fullscreen mode
  1. Commit the resolved merge Once the automated steps succeed, finalize the merge:
   git add .
   git commit -m "Automated conflict resolution for $CI_MERGE_REQUEST_IID"
   git push origin $TARGET_BRANCH
Enter fullscreen mode Exit fullscreen mode

Using the Pre‑Configured Script

Download the pre‑configured script here

Get the complete patch tool

Access the full repository fix

Conclusion

By integrating conflict detection and automated resolution into your CI/CD pipeline, you keep the delivery flow smooth and reduce manual overhead. Adopt the snippets above, tailor the merge driver to your codebase, and let the pipeline handle the heavy lifting.

Top comments (0)