DEV Community

Deep Fix
Deep Fix

Posted on

How to Resolve Git Merge Conflicts in CI/CD Pipelines – Best Practices & Automated Fixes

How to Resolve Git Merge Conflicts in CI/CD Pipelines – Best Practices & Automated Fixes

In modern CI/CD workflows a stray merge conflict can halt the entire delivery pipeline. This guide shows you how to detect, auto‑resolve, and gracefully fail when conflicts arise, keeping your builds green.

Why Merge Conflicts Break CI

  • The merge step runs on a clean checkout; a conflict aborts git merge and returns a non‑zero exit code.
  • Most CI systems treat any non‑zero exit as a failure, stopping subsequent jobs.

Step‑by‑Step Guide

  1. Detect the conflict early
   - name: Merge feature branch
     run: |
       git fetch origin
       git checkout ${{ github.base_ref }}
       git merge --no-ff ${{ github.head_ref }} || true
Enter fullscreen mode Exit fullscreen mode
  1. Capture conflict files
   conflicted=$(git diff --name-only --diff-filter=U)
   echo "Conflicted files: $conflicted"
Enter fullscreen mode Exit fullscreen mode
  1. Automated resolution with ours/theirs strategy
   for file in $conflicted; do
     git checkout --theirs "$file"   # keep incoming changes
     git add "$file"
   done
   git commit -m "Automated conflict resolution"
Enter fullscreen mode Exit fullscreen mode
  1. Fail fast & notify
   - name: Check for unresolved conflicts
     if: steps.merge.outcome == 'failure'
     run: |
       echo "❌ Merge conflict detected. See logs."
       exit 1
Enter fullscreen mode Exit fullscreen mode
  1. Provide a manual fallback
   git checkout -b conflict-fix
   git push origin conflict-fix
Enter fullscreen mode Exit fullscreen mode

Common Pitfalls

  • Ignoring the exit status of git merge leaves the pipeline in an undefined state.
  • Using git reset --hard without backing up the branch can discard valuable work.

Real‑world Script

You can download a ready‑to‑use Bash helper that wraps the above logic:

Download the pre‑configured script here

Or grab the full patch utility:

Get the complete patch tool

For the entire repository fix, see:

Access the full repository fix

Conclusion

By integrating conflict detection and automated resolution directly into your CI pipeline, you keep the feedback loop short and your deployments reliable. Adopt these patterns today and turn a common blocker into a smooth, repeatable process.

Top comments (0)