Handling Merge Conflicts in Git: A Beginner-Friendly Guide

 

Handling Merge Conflicts in Git: A Beginner-Friendly Guide

Merge conflicts are a common scenario when working in Git, especially when multiple developers work on the same file or feature simultaneously. Understanding how to handle these conflicts efficiently is a crucial skill for any developer or DevOps engineer.

What is a Merge Conflict?

A merge conflict occurs when Git cannot automatically combine changes from two branches into a single file. This typically happens when:

  • Two developers modify the same part of a file in different branches.
  • Git cannot determine which changes should take precedence.

Options to Resolve a Merge Conflict

When a merge conflict arises, you’ll have three resolution options:

  1. Keep the changes from the master branch only.
  2. Keep the changes from the dev branch only.
  3. Merge both changes into the file.

Scenario: Merge Conflict in Action

Imagine you’re working on a file called feature-1.txt in two branches:

  • Master branch: Contains some changes to feature-1.txt.
  • Dev branch: Contains other changes to the same file.

When merging the dev branch into the master branch, a conflict arises. Here’s how you can resolve it.

Step-by-Step Guide

1. Setup and Simulate the Conflict

On the dev branch:

# Switch to the dev branch
git checkout dev
# Create and modify the file
vim feature-1.txt
# Stage and commit changes
git add feature-1.txt
git commit -m "File created & changes added in dev branch"

On the master branch:

# Switch to the master branch
git checkout master
# Create and modify the same file
vim feature-1.txt
# Stage and commit changes
git add feature-1.txt
git commit -m "File created & changes added in master branch"

2. Merge the Branches and Trigger the Conflict

Now, attempt to merge the dev branch into master:

git merge dev

At this point, Git will detect conflicting changes in feature-1.txt and notify you of a merge conflict.

3. Check the Conflict Status

Run the following command to see the conflict details:

git status

Git will indicate which file(s) have conflicts.

4. Resolve the Merge Conflict

Open the conflicting file to review the changes:

vim feature-1.txt

You will see markers like these:

<<<<<<< HEAD
Changes from the master branch
=======

Changes from the dev branch
>>>>>>> dev
  • Option 1: Keep only the changes from master (remove dev changes).
  • Option 2: Keep only the changes from dev (remove master changes).
  • Option 3: Combine the changes manually.

Make your edits, save the file, and close the editor.

5. Stage and Finalize the Resolution

After resolving the conflict:

git add feature-1.txt  
git commit -m "Resolved merge conflict in feature-1.txt"

6. Verify the Merge

To ensure everything is merged correctly, check the log or file:

git log  
cat feature-1.txt

Why Is Resolving Merge Conflicts Important?

Merge conflicts are unavoidable in collaborative projects. Resolving them efficiently ensures:

  • Your project remains stable.
  • All important changes are preserved.
  • The team maintains a clean workflow.

Here is the YouTube Link: https://youtube.com/@raeesq.?si=v_QK6Q2XXMf9mKep

Try It Yourself!

Merge conflicts may seem intimidating at first, but with practice, they become straightforward. Follow the steps above and experiment with your Git repository to build confidence.

Happy coding! 🎉

Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator