Merging Branches and Handling Conflicts

Tutorial 2 of 5

Introduction

The purpose of this tutorial is to guide you through the process of merging branches in Git and handling any merge conflicts that may arise. Git is a powerful tool for managing source code in software development projects, and understanding how to effectively merge branches and resolve conflicts is an essential skill for any developer.

By the end of this tutorial, you'll know how to:

  • Merge branches in Git
  • Deal with merge conflicts

To get the most from this tutorial, you should have a basic understanding of Git and its command line interface. If you're completely new to Git, I recommend going through a basic Git tutorial first.

Step-by-Step Guide

Merging Branches

Merging is the way to combine the work of different branches together. This allows you to branch off, develop a new feature, and then integrate it back into the main project.

Step 1: Switch to the receiver branch

The first step is to switch to the branch you want to merge into. This is often your main or master branch. Use the checkout command for this:

git checkout main

Step 2: Merge the branch

Next, you use the merge command to merge the branch you want into the branch you're currently on:

git merge feature-branch

This tells Git to integrate the feature-branch into main.

Handling Merge Conflicts

Sometimes, changes conflict with each other, and Git doesn't know how to combine them. This results in a merge conflict.

Step 1: Identify the conflict

Git will tell you that a conflict has occurred, and if you try to commit, it will show you which files are causing the issue.

git commit

Step 2: Resolve the conflict

Open the problematic file in a text editor. Git marks the problematic area in the file by enclosing it in <<<<<<< HEAD and >>>>>>> branch-name. Make the necessary changes to resolve the conflict, then save and close the file.

Step 3: Commit the resolution

Once you've resolved the conflict, stage the file for commit, and then commit as usual:

git add filename
git commit -m "Resolved merge conflict"

Code Examples

Consider the following scenario: you have a repository with two branches, main and new-feature. You've done some work on both branches, and now you want to merge new-feature into main.

  1. Switch to the main branch:
    bash git checkout main
  2. Merge new-feature into main:
    bash git merge new-feature
    If there are no conflicts, Git will perform the merge and create a new commit in main that includes the changes from new-feature.

If there are conflicts, Git will output a message like this:
bash Auto-merging file.txt CONFLICT (content): Merge conflict in file.txt Automatic merge failed; fix conflicts and then commit the result.
3. Open file.txt in your editor. You'll see the conflicting changes marked like this:
```bash
<<<<<<< HEAD
This is some content from the main branch.
=======
This is some different content from the new-feature branch.

new-feature
4. Resolve the conflict by editing the file until it looks the way you want it to. Then, save and close the file. 5. Stage the file and commit the resolution:bash
git add file.txt
git commit -m "Resolved merge conflict in file.txt"
```

Summary

In this tutorial, we've gone through the process of merging branches in Git and handling merge conflicts. Here are the key points we covered:

  • To merge a branch, switch to the receiver branch and use git merge.
  • Merge conflicts occur when Git can't automatically merge branches due to conflicting changes.
  • To resolve a conflict, edit the conflicted file until it looks the way you want it to, then stage and commit the file.

Next, I recommend learning more about other Git features, such as rebasing and cherry-picking. The official Git documentation is a great resource for this.

Practice Exercises

  1. Create a new Git repository, create a new branch, make some changes on both branches, and then merge the new branch into main.
  2. In the same repository, create a situation where a merge conflict would occur (i.e., change the same line of the same file on two different branches), and then try to merge. Resolve the conflict.
  3. Look up how to abort a merge in Git, and then practice this by starting a merge, causing a conflict, and then aborting the merge.

For each exercise, use the git status and git log commands to understand what's happening at every step. Remember, the key to mastering Git is practice and experimentation. Good luck!