Git Merge Conflict
Git Merge Conflict
Submitted By:
Sudipta Singha(408)
Submitted To:
Dr. Md Musfique Anwar
Professor
Dr. Md. Humayun Kabir
Professor
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
3
Screenshots
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.
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:
• 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.
• 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
• 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.
• 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.
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.
• 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.