How to resolve common merge conflicts?

QuestionsQuestions4 SkillsProDec, 10 2025
0141

Hello! I'm Labby.

Resolving merge conflicts is a common part of working with Git, especially in collaborative environments. Don't worry, it's a skill that gets easier with practice!

Here's a breakdown of how to resolve common merge conflicts:

When do merge conflicts happen?

A merge conflict occurs when Git cannot automatically figure out how to combine changes from two separate branches. This typically happens when:

  • The same line of code is modified differently in both branches.
  • A file is deleted in one branch but modified in another.
  • A file is modified in one branch but moved/renamed in another.

Steps to Resolve a Merge Conflict:

When you encounter a conflict during a git merge (or git pull, which often performs a merge), Git will pause the merge process and tell you which files have conflicts.

  1. Identify Conflicting Files: Git will print messages like:

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

    You can also use git status to see a clear list of "unmerged paths" (files with conflicts).

    git status
    
  2. Open and Edit Conflicting Files: Open each conflicted file in your text editor. Git marks the conflicting sections with special markers:

    <<<<<<< HEAD
    // This is the code from your CURRENT branch (HEAD, usually the branch you merged INTO)
    // For example, if you were on 'master' and merging 'feature-branch',
    // this section shows 'master's version.
    =======
    // This is the code from the OTHER branch (the one you are merging FROM)
    // For example, if you were merging 'feature-branch' into 'master',
    // this section shows 'feature-branch's version.
    >>>>>>> feature-branch
    

    Your task is to manually edit these sections to combine the desired changes. You'll:

    • Delete the <<<<<<< HEAD, =======, and >>>>>>> feature-branch markers.
    • Keep the code you want, or combine parts of both versions to form the correct final code.
    • Remove the code you don't want.

    Example: Let's say master had: javascript function greet() { console.log("Hello!"); } And feature-dimension changed it to: javascript function greet() { console.log("Hi there!"); } The conflict in your file would look like: ```javascript <<<<<<< HEAD function greet() { console.log("Hello!"); }

    function greet() { console.log("Hi there!"); }

    feature-dimension

    You need to decide which version you want, or combine them. If you want `Hi there!`, you'd edit it to:
    ```javascript
    function greet() {
        console.log("Hi there!");
    }
    
  3. Stage the Resolved Files: After you've manually resolved all conflicts in a file, you need to tell Git that you're done with it.

    git add <conflicted-filename>
    

    Do this for every file that had a conflict.

  4. Commit the Merge: Once all conflicts are resolved and all conflicted files are staged, you can finalize the merge. Git will usually pre-populate a commit message for you.

    git commit
    

    Save and close the commit message editor.

Tips for smoother conflict resolution:

  • Communicate: If you're working in a team, talk to your teammates to understand why certain changes were made, which can help in resolving conflicts.
  • Pull/Rebase Frequently: Regularly pulling changes from the main branch or rebasing your feature branch can help you encounter smaller, more manageable conflicts more often, rather than one huge conflict at the very end.
  • Use a Merge Tool: Many IDEs (like VS Code, IntelliJ) and dedicated Git tools (like Meld, KDiff3) have excellent visual merge tools that make conflict resolution much easier. You can configure Git to use an external merge tool (git config --global mergetool <tool-name>). Then, when you have a conflict, you can simply run git mergetool.

Does this give you a good starting point for tackling merge conflicts?

0 Comments

no data
Be the first to share your comment!