Site icon Student Projects Live

Understanding and Resolving Merge Conflicts in GIT

Understanding and Resolving Merge Conflicts in GIT

Git Tutorial – Part 6: Understanding and Resolving Merge Conflicts

Objective

In this tutorial, you will learn how to:


Why Am I Seeing This Screen?

If you ran:

git merge feature/homepage

and Git opened a screen that looks like this:

Merge branch 'feature/homepage'
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
# 
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
~
~
~
.git/MERGE_MSG [unix]

this is usually not an error.

Git is opening its default text editor so you can confirm or edit the merge commit message.

What this means

What you should do right now

If you want to keep the default merge message:

  1. Press Esc
  2. Type:
:wq
  1. Press Enter

This means:

After that, Git will finish the merge.


If You Want to Use Visual Studio Code Instead of Vim

If you do not want Git to open Vim in the future, you can set Visual Studio Code as your Git editor:

git config --global core.editor "code --wait"

After this, Git will open VS Code when it needs you to enter a commit message.


What Is a Merge Conflict?

A merge conflict occurs when Git cannot automatically combine changes from two branches because the same part of a file has been modified differently.

Git pauses the merge process and asks you to resolve the conflict manually.


When Do Merge Conflicts Occur?

Common situations include:


Example Scenario

Assume two developers are working on the same file:

Developer A

Welcome to Git Tutorial

Changes it to:

Welcome to Git Tutorial Version 2

Developer B

Changes the same line to:

Welcome to Git Training Guide

Both developers commit their changes.

When Git attempts to merge the branches, it cannot determine which version should be kept.


Repository Before Merge

                 main
                  │
                  ●
                  │
        -------------------
        │                 │
        ▼                 ▼
 feature/login     feature/homepage

Both branches contain different changes.


Step 1: Create Two Branches

Start from the main branch.

Create the first branch:

git switch -c feature/login

Modify a file (for example, README.md) and commit the changes.

git add README.md
git commit -m "Update README from login branch"

Switch back to main.

git switch main

Create another branch.

git switch -c feature/homepage

Modify the same line in README.md.

Commit the changes.

git add README.md
git commit -m "Update README from homepage branch"

Step 2: Merge the First Branch

Switch to the main branch.

git switch main

Merge the first feature branch.

git merge feature/login

The merge should complete successfully.

If you see the .git/MERGE_MSG screen after running git merge feature/homepage, Git is usually waiting for you to confirm the merge commit message. In most cases, you should press Esc, type :wq, and press Enter.


Step 3: Merge the Second Branch

Now merge the second branch.

git merge feature/homepage

Git displays a conflict.

Example:

Auto-merging README.md

CONFLICT (content): Merge conflict in README.md

Automatic merge failed; fix conflicts and then commit the result.

Git has stopped the merge until the conflict is resolved.


Step 4: Check Repository Status

Run:

git status

Example output:

On branch main

You have unmerged paths.

Fix conflicts and run "git commit".

Git identifies the files that require attention.


Step 5: Open the Conflicted File

Open README.md in Visual Studio Code.

You will see conflict markers similar to:

<<<<<<< HEAD
Welcome to Git Tutorial Version 2
=======
Welcome to Git Training Guide
>>>>>>> feature/homepage

Understanding the Conflict Markers

<<<<<<< HEAD

Represents the current branch (main).

=======

Separates the two versions.

>>>>>>> feature/homepage

Represents the incoming changes from the branch being merged.


Step 6: Resolve the Conflict

Choose one of the following options:

Option 1: Keep the Current Change

Welcome to Git Tutorial Version 2

Option 2: Keep the Incoming Change

Welcome to Git Training Guide

Option 3: Combine Both Changes

Welcome to Git Tutorial Version 2

Welcome to Git Training Guide

Remove all conflict markers after deciding on the final content.

Save the file.


Step 7: Stage the Resolved File

After resolving the conflict, stage the file.

git add README.md

Git now recognizes that the conflict has been resolved.


Step 8: Complete the Merge

Complete the merge by creating a merge commit.

git commit -m "Resolve merge conflict in README"

Git records the successful merge.


Step 9: Verify the Merge

View the commit history.

git log --oneline --graph

Example output:

*   a45d2b1 Resolve merge conflict in README
|\
| * 9a76d21 Update README from homepage branch
| * 8d13ef4 Update README from login branch
|/
* 3b72ef5 Previous commit

The graph shows both branches merging into a single commit.


Visual Studio Code Merge Editor

Visual Studio Code provides a graphical Merge Editor.

When a conflict occurs, options such as the following appear above the conflicted section:

These options simplify conflict resolution without manually editing conflict markers.


Aborting a Merge

If you decide not to continue the merge:

git merge --abort

Git restores the repository to its state before the merge began.


Common Commands

CommandDescription
git merge <branch>Merge a branch into the current branch.
git statusShow files with merge conflicts.
git add <file>Mark a conflict as resolved.
git commitComplete the merge after resolving conflicts.
git merge --abortCancel the merge process.
git log --oneline --graphDisplay branch and merge history.

Best Practices


Summary

In this chapter, you learned how to:


Tutorial Index:

Exit mobile version