Troubleshooting

How to Resolve a Merge Conflict in Git 🚀

A merge conflict happens when Git cannot automatically merge changes due to conflicting modifications in the same file

chevron-rightMerge Conflicthashtag

If one branch deletes a file while another branch edits it, Git cannot decide whether to keep or remove the file.

  • In the main branch, file A is deleted.

  • In the dev branch, file A is modified.

  • Now, when you try to merge dev into main, Git cannot decide:

    • Should it keep the deletion from main?

    • Or should it keep the modified version from dev? This leads Merge Conflict

Commands to resolve merge conflict

  • git checkout --theirs <file>

    Uses the version from the incoming branch (dev)

    git checkout --ours <file>

    Uses the version from the current branch (main)

Scenario

  • Pavan (on dev) is modifying config.yml.

  • Ajay (on main) has deleted config.yml.

  • When Pavan tries to merge dev into main, Git doesn't know whether to keep the deleted file (from main) or keep the modified file (from dev).

  • This causes a merge conflict.


Who Resolves the Merge Conflict?

Since Pavan is merging dev into main, he must resolve the conflict. Ajay will not see the conflict unless Pavan pushes the merged changes.


Step-by-Step Resolution

Step 1: Pavan Tries to Merge dev into main

Pavan runs:

git checkout main
git merge dev

Git detects a conflict and shows:

CONFLICT (modify/delete): config.yml deleted in main and modified in dev.

Running git status will show:

both deleted:    config.yml

Step 2: Pavan Resolves the Conflict

Now, Pavan has two options:

Option 1: Keep the File from dev (Keep Pavan’s Changes)

If Pavan wants to keep the modified config.yml from dev, he runs:

Note: current branch is main

git checkout --theirs config.yml  # Restore config.yml from dev
git add config.yml
git commit -m "Resolved conflict: Kept modified config.yml from dev"

What Happens?

  • This restores config.yml from dev.

  • The file will be committed, meaning it remains in the repository.


Option 2: Keep the Deletion from main (Ajay’s Changes)

If Pavan wants to keep Ajay’s deletion, he runs:

git rm config.yml
git commit -m "Resolved conflict: Kept deletion of config.yml from main"

What Happens?

  • This permanently removes config.yml from the repository.


Step 3: Pavan Completes the Merge and Pushes the Changes

After resolving the conflict, Pavan pushes the changes:

git push origin main

Now, the conflict is resolved, and main is updated.


Summary of Conflict Resolution

Scenario
Command to Use
Result

Keep modified file from dev

git checkout --theirs config.yml && git add config.yml && git commit -m "Kept modified config.yml"

The file remains

Keep deletion from main

git rm config.yml && git commit -m "Kept deletion"

The file is deleted


Final Thoughts

  • Pavan resolves the conflict because he is merging dev into main.

  • He can either keep his changes (dev) or accept the deletion (main).

  • After resolving, he must commit and push the changes.

Let me know if you need further clarification! 🚀

What Happens When You Delete Multiple Files and Folders and Merge?

If multiple files and folders were deleted in main and modified in dev, then merging dev into main will cause multiple merge conflicts.


Example: Deleting Multiple Files and Folders

Scenario

  • Ajay (on main) deletes an entire folder and some files:

    rm -rf config/ settings.yml logs/
    git commit -m "Deleted config folder, settings.yml, and logs directory"
  • Pavan (on dev) modifies those same files and folders:

    echo "New settings" >> settings.yml
    echo "Updated logs" >> logs/error.log
  • Pavan switches to main and tries to merge dev:

    git checkout main
    git merge dev

🔴 Merge Conflict Occurs:

CONFLICT (modify/delete): settings.yml deleted in main and modified in dev.
CONFLICT (modify/delete): logs/error.log deleted in main and modified in dev.
CONFLICT (modify/delete): config/app.conf deleted in main and modified in dev.

How to Resolve the Conflict for Multiple Files

Option 1: Keep All Files and Folders from dev (Use --theirs)

If Pavan wants to restore all deleted files from dev, he runs:

git checkout --theirs -- settings.yml logs/error.log config/*
git add .
git commit -m "Resolved conflict: Restored files from dev"

Effect:

  • This brings back all deleted files and folders from dev (overwriting main's deletions).


Option 2: Keep Deletions from main (Use --ours)

If Pavan wants to keep the deletions from main, he removes the conflicted files:

git rm -r settings.yml logs config
git commit -m "Resolved conflict: Kept deletions from main"

Effect:

  • The files and folders remain deleted.


Option 3: Keep Some Files and Delete Others (Manual Fix)

If Pavan wants to restore some files but keep others deleted, he manually picks files:

git checkout --theirs -- settings.yml  # Restore settings.yml from dev
git rm -r logs config  # Keep logs and config deleted
git add .
git commit -m "Resolved conflict: Restored settings.yml but kept logs and config deleted"

Effect:

  • settings.yml is restored.

  • logs/ and config/ stay deleted.


Final Summary

Scenario
Command

Restore all files from dev (ignore main's deletions)

git checkout --theirs -- <files>

Keep all deletions from main

git rm -r <files>

Manually restore some files and delete others

Use git checkout --theirs on selected files and git rm on others

Let me know if you need more clarity! 🚀

.


🛠 Steps to Resolve a Merge Conflict

1️⃣ Check Merge Conflict

When you merge branches and a conflict occurs, Git shows an error:

git merge <branch-name>

Output:

Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.

2️⃣ Identify Conflicted Files

Run:

This shows:

This means file.txt has conflicting changes.


3️⃣ Open the Conflicted File

Open the file in an editor (VS Code, Nano, Vim, etc.), and you'll see:

  • Between <<<<<<< HEAD and ======= → Changes from your current branch

  • Between ======= and >>>>>>> feature-branch → Changes from the merging branch


4️⃣ Manually Resolve the Conflict

Edit the file to keep the correct changes. Example:

Save the file.


5️⃣ Mark Conflict as Resolved

Run:


6️⃣ Complete the Merge

Run:


🛠 Alternative: Abort the Merge

If you want to cancel the merge:


🎯 Key Takeaways

✅ Use git status to check conflicted files ✅ Edit files manually to resolve conflicts ✅ Use git add and git commit to finalize the merge ✅ If needed, abort the merge with git merge --abort

Need help with a specific merge scenario? 🚀

Last updated