Merge conflicts occur when Git is unable to automatically resolve differences in code between branches. This typically happens when changes are made to the same lines of code in different branches. Understanding how to resolve these conflicts is crucial for maintaining a smooth workflow.

Key Concepts

  1. Merge Conflict: A situation where Git cannot automatically merge changes due to conflicting modifications in the same part of the code.
  2. Conflict Markers: Special markers added by Git to indicate the conflicting sections in the files.
  3. Conflict Resolution: The process of manually editing the conflicting files to resolve the differences.

Steps to Resolve Merge Conflicts

  1. Identify the Conflict

When a merge conflict occurs, Git will notify you and mark the conflicting files. You can use the following command to see the status:

git status

This will show you which files have conflicts.

  1. Open the Conflicting Files

Open the files listed in the git status output. You will see conflict markers like this:

<<<<<<< HEAD
// Changes from the current branch
=======
# Changes from the branch being merged
>>>>>>> branch-name

  1. Edit the Conflicting Sections

Manually edit the conflicting sections to resolve the differences. Remove the conflict markers and make sure the code is correct. For example:

// Before resolving
<<<<<<< HEAD
int value = 10;
=======
int value = 20;
>>>>>>> feature-branch

// After resolving
int value = 15; // Resolved value

  1. Mark the Conflict as Resolved

After editing the files, you need to mark the conflicts as resolved. Use the following command:

git add <file>

  1. Commit the Changes

Finally, commit the resolved changes:

git commit -m "Resolved merge conflict in <file>"

Practical Example

Let's walk through a practical example of resolving a merge conflict.

Scenario

You have two branches: main and feature. Both branches have changes in the same file, example.txt.

Step-by-Step Resolution

  1. Merge the feature branch into main:

    git checkout main
    git merge feature
    

    Git will notify you of a conflict in example.txt.

  2. Check the status:

    git status
    

    Output:

    Unmerged paths:
      (use "git add <file>..." to mark resolution)
        both modified:   example.txt
    
  3. Open example.txt and resolve the conflict:

    <<<<<<< HEAD
    Line from main branch
    =======
    Line from feature branch
    >>>>>>> feature
    

    Edit the file to resolve the conflict:

    Resolved line combining both changes
    
  4. Mark the conflict as resolved:

    git add example.txt
    
  5. Commit the changes:

    git commit -m "Resolved merge conflict in example.txt"
    

Common Mistakes and Tips

  • Mistake: Forgetting to remove conflict markers.

    • Tip: Always double-check the file to ensure all conflict markers are removed.
  • Mistake: Overwriting changes from one branch.

    • Tip: Carefully review changes from both branches to ensure important modifications are not lost.
  • Mistake: Not testing the code after resolving conflicts.

    • Tip: Always test your code after resolving conflicts to ensure it works as expected.

Exercise

Task

You have two branches, dev and feature, both modifying the same line in config.txt. Resolve the merge conflict.

  1. Merge feature into dev.
  2. Resolve the conflict in config.txt.
  3. Commit the resolved changes.

Solution

  1. Merge feature into dev:

    git checkout dev
    git merge feature
    
  2. Open config.txt and resolve the conflict:

    <<<<<<< HEAD
    setting = true
    =======
    setting = false
    >>>>>>> feature
    

    Edit the file:

    setting = true // Decided to keep the setting as true
    
  3. Mark the conflict as resolved:

    git add config.txt
    
  4. Commit the changes:

    git commit -m "Resolved merge conflict in config.txt"
    

Conclusion

Resolving merge conflicts is an essential skill for any developer working with Git. By understanding the process and practicing with real-world scenarios, you can ensure smooth collaboration and maintain a clean codebase. In the next module, we will explore branch management techniques to further enhance your Git workflow.

© Copyright 2024. All rights reserved