Rebasing with Conflicts: 3 Practical Examples

Explore three detailed examples of rebasing with conflicts in version control systems.
By Jamie

Understanding Rebasing with Conflicts

Rebasing is a powerful feature in version control systems like Git that allows developers to integrate changes from one branch into another. However, when multiple developers are working on the same codebase, conflicts can arise. This article presents three practical examples of rebasing with conflicts, illustrating common scenarios and how to resolve them effectively.

Example 1: Feature Branch Conflict with Main Branch

In a development team, a feature branch is created to add a new login feature. Meanwhile, another developer modifies the same section of code in the main branch.

The development process proceeds as follows:

  • Developer A checks out a feature branch named feature/login and makes changes to the login.js file.
  • Developer B, working on the main branch, also updates the login.js file by fixing a bug.
  • Later, Developer A attempts to rebase feature/login onto the latest main branch.

During the rebase, a conflict occurs because both branches modified the same lines in login.js.

Steps to Resolve the Conflict:

  1. Initiate the rebase: git checkout feature/login and git rebase main
  2. Git alerts Developer A of the conflict in login.js.
  3. Developer A opens login.js, which contains conflict markers:

    // HEAD
    const user = authenticateUser();
    
    // Changes from main
    const user = validateUser();
    
  4. Developer A decides to combine both functionalities into:

    const user = authenticateUser() || validateUser();
    
  5. After editing, Developer A marks the conflict as resolved: git add login.js

  6. Continue the rebase process: git rebase --continue

Notes:

  • Always ensure to test the code after resolving conflicts to confirm functionality.
  • Consider using git mergetool for a visual conflict resolution experience.

Example 2: Parallel Development on Configuration Files

In a scenario where multiple developers are updating configuration files simultaneously, conflicts can arise during rebasing.

  • Developer C is working on the feature/analytics branch, where they modify config.json to add analytics settings.
  • Developer D is also working on the feature/performance branch and makes changes to the same config.json file to enhance performance settings.
  • When Developer C tries to rebase feature/analytics onto the main branch, conflicts occur in config.json.

Steps to Resolve the Conflict:

  1. Initiate the rebase: git checkout feature/analytics and git rebase main
  2. Git reports a conflict in config.json.
  3. Developer C opens the file and sees:

    // HEAD
    {
        "performance": {
            "enabled": true
        }
    }
    
    // Changes from main
    {
        "analytics": {
            "enabled": true
        }
    }
    
  4. Developer C merges both changes:

    {
        "performance": {
            "enabled": true
        },
        "analytics": {
            "enabled": true
        }
    }
    
  5. After saving, Developer C marks the conflict as resolved with git add config.json.

  6. Continue the rebase: git rebase --continue

Notes:

  • Keeping configuration files modular can help in reducing conflicts.
  • Consider utilizing JSON schema validation to ensure file integrity after merges.

Example 3: Documentation Updates with Concurrent Edits

When updating documentation, conflicts may arise if two developers modify the same document simultaneously.

  • Developer E is updating the README.md file in the feature/docs branch to add new installation instructions.
  • Developer F is also editing the README.md file in the feature/updates branch to revise existing usage examples.
  • Developer E attempts to rebase feature/docs onto the main branch and encounters a conflict.

Steps to Resolve the Conflict:

  1. Start the rebase: git checkout feature/docs and git rebase main
  2. Git identifies a conflict in README.md.
  3. Developer E opens the file and sees:

    # Installation
    
    - Install via NPM
    
    # Usage
    
    - Example usage here...
    
    // Changes from main
    # Installation
    
    - Install globally using NPM
    
  4. Developer E edits to integrate both changes:

    # Installation
    
    - Install globally using NPM
    - Install via NPM
    
    # Usage
    
    - Example usage here...
    
  5. After resolving, Developer E stages the changes with git add README.md.

  6. Proceed with the rebase: git rebase --continue

Notes:

  • Regular communication among team members can minimize documentation conflicts.
  • Utilizing pull requests can help in reviewing changes before merging, reducing the chances of conflicts.

By understanding these practical examples of rebasing with conflicts, developers can better navigate version control challenges and maintain a smoother workflow.