Examples of Merge Conflict in Git

Learn about merge conflicts in Git with practical examples to help you resolve them effectively.
By Jamie

Understanding Merge Conflicts in Git

In the world of collaborative software development, using version control systems like Git is essential. However, when multiple developers are working on the same codebase, conflicts can arise during the merging process. A merge conflict occurs when Git is unable to automatically reconcile differences between two branches. Understanding these conflicts is crucial for maintaining a smooth workflow. Below are three diverse, practical examples of merge conflicts in Git, illustrating different scenarios and their resolutions.

Example 1: Concurrent Changes to the Same Line

Context

Two developers, Alice and Bob, are working on the same file, app.js. Alice changes a specific line in her branch, while Bob, unbeknownst to Alice, modifies the same line in his branch.

When they attempt to merge their changes, a conflict arises because Git cannot determine which change to keep.

Example

Suppose the original line in app.js is:

const greeting = 'Hello, World!';
  • Alice modifies the line in her branch:

    const greeting = 'Hello, Alice!';
    
  • Bob modifies the line in his branch:

    const greeting = 'Hello, Bob!';
    

When they try to merge their branches, Git will report a conflict:

CONFLICT (content): Merge conflict in app.js

Notes

  • To resolve this conflict, both developers must manually edit app.js, deciding on the final output. A possible resolution could be:

    const greeting = 'Hello, Alice and Bob!';
    
  • After resolving the conflict, they need to add and commit the changes to complete the merge.


Example 2: Added Functions in Different Branches

Context

In this scenario, Developer Charlie is working on a new feature in a feature branch, while Developer Dana is also adding another function in the same file but in a different branch. They both include a new function in utils.js, leading to a merge conflict due to the new content being added in the same area of the file.

Example

Charlie adds a new function:

function greetUser(name) {
  return `Hello, ${name}!`;
}

Dana adds her own function:

function farewellUser(name) {
  return `Goodbye, ${name}!`;
}

When they attempt to merge their branches, Git will indicate a conflict:

CONFLICT (content): Merge conflict in utils.js

Notes

  • The resolution will require both developers to integrate their functions in a way that maintains both features:
function greetUser(name) {
  return `Hello, ${name}!`;
}

function farewellUser(name) {
  return `Goodbye, ${name}!`;
}
``` 

- After merging, they should test both functions to ensure they work together seamlessly.

---

## Example 3: Renaming and Modifying a File

### Context
This example involves Developer Evan renaming a file from `config.json` to `settings.json` in one branch, while Developer Fiona modifies `config.json` in another branch. When Evan tries to merge his branch, a conflict occurs because Git detects changes in both the original and renamed files.

### Example
Evan changes the file name:

```bash
git mv config.json settings.json
```

Fiona modifies `config.json` to add a new configuration setting:

```json
{
  "appName": "MyApp",
  "version": "1.0",
  "newSetting": true
}
```

When Evan attempts to merge, Git reports:

```bash
CONFLICT (rename): Renamed 'config.json' to 'settings.json' but the content is different.
``` 

### Notes

- To resolve this conflict, Evan must manually integrate Fiona's changes into `settings.json`:
  ```json
  {
    "appName": "MyApp",
    "version": "1.0",
    "newSetting": true
  }
  • After resolving the conflict, Evan can proceed to stage and commit the changes.