0% found this document useful (0 votes)
13 views10 pages

Git Merge Conflict

This document is about git merge conflict lab report.

Uploaded by

singha.stu2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

Git Merge Conflict

This document is about git merge conflict lab report.

Uploaded by

singha.stu2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Git Merge Conflict

Course Code: CSE-404


Course Name: Software Engineering Lab

Submitted By:
Sudipta Singha(408)

Submitted To:
Dr. Md Musfique Anwar
Professor
Dr. Md. Humayun Kabir
Professor

Date: October 25, 2024

Department of Computer Science & Engineering


Jahangirnagar University
Contents
1 What is git conflict? 2

2 Main reason of git conflict 3

3 Resolve merge conflict 6

4 How to avoid merge conflicts 8

1
1 What is git conflict?
When Git cannot automatically reconcile differences between two commits during a merge
procedure, a merge conflict occurs. It happens when several authors in various branches
make modifications to the same section of a file. Git identifies the areas of disagreement
and stops the merging process, needing human intervention to determine how to merge the
conflicting changes because it is unable to determine which changes should be prioritized.
Git inserts conflict markers to show the portions of the involved files that are at odds
during a merge conflict. After that, the user has to examine the regions that are at odds
and choose which version to retain—possibly combining the two. Although lines of code
that overlap are the most common cause of merge conflicts, they can also happen when
a file has been changed in an incompatible fashion, such as being renamed or deleted in
one branch but modified in another. Only after the conflicts are resolved and committed
does Git allow the merge to complete.

2
2 Main reason of git conflict
A merge conflict occurs when Git cannot automatically merge changes from different
branches because of conflicting modifications.To demonstrate the main reasons for merge
conflict, we will create a git repository and try to make a merge conflict.
# Creating empty git repository

mkdir demo
cd demo
git init
echo " Master " > A . txt
git add A . txt
git commit -m " from master "

# adding branches

git branch Sudipta


git branch Singha
git checkout Sudipta
echo " Sudipta " >> A . txt
git add A . txt
git commit -m " Sudipta "

# Update from branch Singha

git checkout Singha


echo " Singha " > A . txt
git commit -m " Singha "
git merge Sudipta

3
Screenshots

Figure 1: Create git repository

Figure 2: Branch Creation

4
Figure 3: Merge Conflict

5
3 Resolve merge conflict
Git merge is done manually by Sudpita or Singha to resolve conflict. Here are the changes
to resolve the conflict.The steps taken to resolve the conflict are.

Figure 4: Highlighted the conflict

Figure 5: Resolved conflicts

6
Figure 6: Merge Conflict resolved

7
4 How to avoid merge conflicts
To minimize the likelihood of merge conflicts, consider the following best practices:

1. Communicate with Team

• Coordinate Changes: Ensure that team members are aware of each other’s
work. Regularly communicate about changes that might affect shared files.
• Use Issue Tracking: Track tasks and issues in a centralized system to avoid
overlapping work.

2. Frequent Pulls and Updates

• Regularly Pull Changes: Frequently pull updates from the main branch
into your feature branch to keep it up-to-date. This reduces the risk of large
conflicts when merging back.
git pull origin main

• Rebase Regularly: Use git rebase to keep your branch updated with the
latest changes from the main branch.
git fetch origin
git rebase origin / main

3. Small, Focused Commits

• Make Smaller Commits: Break down your changes into smaller, manage-
able commits. Smaller commits make it easier to resolve conflicts and under-
stand changes.
• Single Purpose Commits: Each commit should focus on a single change or
purpose to make it clearer what has changed.

4. Avoid Long-Lived Branches

• Keep Branches Short-Lived: Avoid keeping feature branches open for ex-
tended periods. Long-lived branches are more likely to encounter conflicts.
• Merge or Rebase Often: Integrate changes from the main branch into your
feature branch regularly to reduce the divergence.

5. Use Clear Branch Naming

• Descriptive Branch Names: Use meaningful names for branches to make


it easier to understand the purpose and scope of the changes being made.

6. Minimize Direct Modifications

• Avoid Direct Changes to Shared Files: When possible, avoid making


changes to files that are likely to be edited by multiple people, especially if
they are critical or widely used.

7. Run Tests Before Merging

8
• Test Changes: Ensure that all tests pass before merging your branch into
the main branch. This helps catch issues early and reduces the risk of conflicts
caused by broken code.

8. Use Merge Tools and Strategies

• Merge Tools: Utilize graphical merge tools to handle complex conflicts more
effectively.
• Conflict Resolution Strategies: Be prepared with strategies for conflict
resolution, such as discussing changes with team members or reviewing version
history.

By following these practices, anyone can significantly reduce the chances of encountering
merge conflicts and handle them more efficiently when they do arise.

You might also like