Merging Branches and Resolving Conflicts

Let's illustrate a scenario where two branches have conflicting changes

Scenario

Conflicting Changes Let's consider a scenario where you are working on a project with a team, and you have two branches: feature-A and feature-B. Both branches have undergone significant changes, including modifications to the same files.

Step 1: Checking out the Target Branch

To begin the merge process, we need to switch to the branch where we want to merge the changes. In this scenario, let's assume we want to merge feature-B into feature-A. To switch to feature-A, execute the following command in your terminal:

git checkout feature-A

Step 2: Initiating the Merge

Now that we are on the target branch (feature-A), we can initiate the merge with the following command:

git merge feature-B

Step 3: Resolving Conflicts Manually

During the merge process, Git might identify conflicting changes in the files modified on both branches. Conflicts occur when Git is unable to determine which version of the file to keep. Git will mark these conflicts in the affected files.

To resolve conflicts manually, open the conflicted files in your preferred text editor. Within the file, Git will mark the conflicting sections using special markers. Here's an example:

<<<<<<< HEAD
# This is the version of the code from feature-A branch

def some_function():
    # Code implementation
    pass
=======
# This is the version of the code from feature-B branch

def some_function():
    # Updated code implementation
    pass
>>>>>>> feature-B

In the above example, Git has marked the conflicting changes between the HEAD (current branch) and feature-B. Manually review the code and select the desired changes or modify the code to combine the changes from both branches.

After resolving the conflicts, save the file, and proceed to the next step.

Step 4: Completing the Merge

Once you have resolved all conflicts in the affected files, you need to inform Git that the conflicts have been resolved. Use the following command:

git add <file1> <file2> ...

Replace <file1>, <file2>, and so on with the names of the files you have resolved.

Step 5: Committing the Merge

After resolving conflicts, create a new commit to finalise the merge:

git commit -m "Merge feature-B into feature-A"

Congratulations! You have successfully merged the changes from the feature-B branch into the feature-A branch, resolving conflicts along the way.

Using Merge Tools

Git also provides merge tools that assist in resolving conflicts visually. These tools offer a more user-friendly approach to conflict resolution. Popular merge tools include KDiff3, P4Merge, and Beyond Compare.

To configure and use a merge tool, you can update your Git configuration file with the desired tool's settings. Consult the documentation of your chosen merge tool for specific instructions.

Remember to carefully review the conflicting changes, make informed decisions, and create clear commit messages to maintain a clean and organised project history.

Last updated