Branch Conflict Examples in Git

Explore practical examples of branch conflicts in Git to enhance your version control skills.
By Jamie

Understanding Branch Conflicts in Git

Branch conflicts in Git occur when changes made in two different branches overlap in a way that Git cannot automatically merge them. These conflicts often arise during collaborative development, particularly when multiple developers are working on the same files. Understanding how to handle these conflicts is crucial for maintaining an efficient workflow. Here are three practical examples of branch conflicts in Git.

Example 1: Simultaneous Edits to the Same Line

Context

In a team project, two developers, Alice and Bob, are working on a feature that involves modifying a shared configuration file. Both developers make changes to the same line in the file, leading to a merge conflict.

Example

  1. Alice creates a new branch from main and updates the configuration file:

    git checkout -b feature/alice
    echo "MaxUsers=100" >> config.txt
    git add config.txt
    git commit -m "Updated MaxUsers"
    
  2. Meanwhile, Bob also creates a new branch and modifies the same line in config.txt:

    git checkout -b feature/bob
    echo "MaxUsers=150" >> config.txt
    git add config.txt
    git commit -m "Changed MaxUsers"
    
  3. After both commits, they both try to merge their branches back into main:

    git checkout main
    git merge feature/alice
    git merge feature/bob
    
  4. This results in a merge conflict due to the simultaneous edits.

Notes

  • Resolution involves manually editing config.txt to decide which value to keep or how to combine them.
  • Use git status to identify the conflict and git diff to see the conflicting changes.

Example 2: Different Branches Changing File Structure

Context

Developers Alice and Charlie are refactoring a project. Alice moves a function to a new file while Charlie renames the original file. When they try to merge their branches, a conflict arises due to the file structure changes.

Example

  1. Alice creates a new branch and moves functionA from utils.js to newUtils.js:

    git checkout -b feature/alice
    mv utils.js newUtils.js
    git add newUtils.js
    git commit -m "Moved functionA to newUtils.js"
    
  2. At the same time, Charlie creates his branch and renames utils.js to utilities.js:

    git checkout -b feature/charlie
    mv utils.js utilities.js
    git add utilities.js
    git commit -m "Renamed utils.js to utilities.js"
    
  3. When both developers try to merge back into main, they encounter a conflict:

    git checkout main
    git merge feature/alice
    git merge feature/charlie
    
  4. Git reports a conflict due to the changes in file names and locations.

Notes

  • To resolve, one developer must choose to keep one structure over the other or manually integrate the necessary changes.
  • Use git log to review history and understand the context of changes.

Example 3: Changes in the Same Function

Context

Developers Bob and Diana are both working on the same function in the same file but with different logic. When they try to merge their branches, a conflict arises because Git cannot automatically reconcile the differences.

Example

  1. Bob creates a branch and modifies calculateSum function to add two integers:

    git checkout -b feature/bob
    echo "function calculateSum(a, b) { return a + b; }" > math.js
    git add math.js
    git commit -m "Updated calculateSum to add integers"
    
  2. Meanwhile, Diana creates her branch and changes calculateSum to concatenate two strings:

    git checkout -b feature/diana
    echo "function calculateSum(a, b) { return a.toString() + b.toString(); }" > math.js
    git add math.js
    git commit -m "Changed calculateSum to concatenate strings"
    
  3. Upon trying to merge both branches into main:

    git checkout main
    git merge feature/bob
    git merge feature/diana
    
  4. Git raises a conflict for math.js because the same function has been modified differently.

Notes

  • Resolution requires deciding on the intended functionality and modifying the code accordingly.
  • It’s helpful to discuss with team members to align on the intended design before resolving the conflict.

By understanding these examples of branch conflicts in Git, you can better navigate version control challenges in collaborative projects.