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
- 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
- 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
Run it in a job:
resolve_conflict:
stage: resolve
script:
- chmod +x resolve-conflict.sh
- ./resolve-conflict.sh
-
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"
Add the driver to .gitattributes:
package.json merge=packagejson
- 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
Using the Pre‑Configured Script
Download the pre‑configured script here
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)