Skip to content

Latest commit

 

History

History
164 lines (122 loc) · 4.24 KB

3. Resolving Merge Conflicts.md

File metadata and controls

164 lines (122 loc) · 4.24 KB

Resolving Merge Conflicts

Merge conflicts occur in Git when changes from two branches affect the same part of a file and Git cannot automatically merge them. This guide explains how to identify, resolve, and finalize merge conflicts effectively.


1. What Causes Merge Conflicts?

A merge conflict can happen when:

  • Multiple contributors make changes to the same line in a file.
  • One branch modifies a file while another branch deletes it.
  • Different branches modify overlapping parts of a file.

2. Identifying Merge Conflicts

When Conflicts Occur

During a merge, Git will notify you of conflicts:

Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.

Check the Status

To see which files have conflicts:

git status

Conflicted files will appear under the section "Unmerged paths".


3. Resolving Merge Conflicts

Step 1: Open the Conflicting Files

Conflicting files will contain conflict markers like these:

<<<<<<< HEAD
Changes from the current branch.
=======
Changes from the branch being merged.
>>>>>>> feature_branch
  • HEAD: Represents changes from the branch you are currently on.
  • Below =======: Represents changes from the branch you are merging.

Step 2: Resolve the Conflict

Manually edit the file to integrate or choose the changes you want to keep. Remove the conflict markers once resolved.

Example Before Resolving:

<<<<<<< HEAD
This is line 1 from the main branch.
=======
This is line 1 from the feature branch.
>>>>>>> feature_branch

Example After Resolving:

This is the integrated line 1 from both branches.

Step 3: Mark the Conflict as Resolved

After editing, stage the resolved file:

git add file.txt

Step 4: Complete the Merge

Finalize the merge by committing:

git commit -m "Resolve merge conflicts"

4. Using Tools for Conflict Resolution

Git's Built-In Merge Tool

Invoke Git's merge tool for easier conflict resolution:

git mergetool

Visual Merge Tools

You can use external GUI-based merge tools like:

  • VS Code (built-in Git support).
  • Sourcetree.
  • Meld.
  • KDiff3.

To configure a tool globally:

git config --global merge.tool tool_name

5. Aborting a Merge

If resolving conflicts becomes too complex or you want to abandon the merge:

git merge --abort

This resets the branch to its state before the merge started.


6. Best Practices for Resolving Conflicts

  • Communicate with Your Team: Discuss conflicts, especially when collaborating.
  • Resolve Small Changes Often: Regularly merge branches to minimize conflicts.
  • Use Descriptive Commit Messages: Clearly describe how conflicts were resolved.

Common Commands for Conflict Resolution

Command Description
git status List conflicting files.
git diff View differences in conflicting files.
git mergetool Launch the configured merge tool.
git add file.txt Mark the conflict as resolved.
git merge --abort Abort the merge process.

Example Workflow: Resolving a Conflict

Step 1: Merge Branches

Attempt to merge:

git merge feature_branch

Step 2: Identify Conflicts

Check the conflicted files:

git status

Step 3: Edit and Resolve

Open the conflicting file in a text editor, resolve the conflicts, and save.

Step 4: Stage and Commit

git add resolved_file.txt
git commit -m "Resolve merge conflict in resolved_file.txt"

Conclusion

Merge conflicts are a natural part of collaborative workflows. By understanding how to identify and resolve conflicts, you can ensure smooth integration of changes and maintain project stability.


Next Steps: Remote Repositories