Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Git & GitHub - Resolving Conflicts

How to resolve merge conflicts in Git

Merge conflicts occur when changes in different branches overlap and Git cannot automatically merge them. This guide explains how to resolve merge conflicts in Git, ensuring a smooth integration of changes.

Key Points:

  • Merge conflicts happen when changes in branches overlap.
  • Git marks the conflicted areas in the files, requiring manual resolution.
  • Using Git's tools and commands helps resolve conflicts efficiently.

Understanding Merge Conflicts

Merge conflicts occur when Git encounters changes in multiple branches that overlap and cannot be merged automatically. Conflicts need to be resolved manually by editing the conflicting files.

Identifying Conflicts

When a conflict occurs during a merge, Git will stop the merge process and mark the conflicted areas in the affected files. Use the git status command to see which files have conflicts.


# Check the status of your repository
$ git status

# Example output showing conflicted files
# On branch main
# You have unmerged paths.
#   (fix conflicts and run "git commit")
#
# Unmerged paths:
#   (use "git add ..." to mark resolution)
#       both modified:   conflicted-file.txt
                

Resolving Conflicts Manually

To resolve conflicts, open the conflicted file in your text editor. Git marks the conflicts with special markers:


<<<<<<< HEAD
This is the main branch content.
=======
This is the feature branch content.
>>>>>>> feature-branch
                
  • Edit the file to resolve the conflict by keeping the desired changes and removing the conflict markers.
  • Stage the resolved file using git add.
  • Complete the merge by committing the changes.

# Stage the resolved file
$ git add conflicted-file.txt

# Commit the merge
$ git commit -m "Resolve merge conflict in conflicted-file.txt"
                

Using Git Mergetool

Git supports external merge tools to help resolve conflicts visually. You can configure and use a merge tool with the following commands:


# Configure a merge tool (example with Meld)
$ git config --global merge.tool meld

# Use the merge tool to resolve conflicts
$ git mergetool
                

This command opens the configured merge tool, allowing you to resolve conflicts using a graphical interface.

Aborting a Merge

If you want to abort the merge process and revert to the previous state, you can use the git merge --abort command.


# Abort the merge
$ git merge --abort
                

This command stops the merge and restores the state of your repository to what it was before the merge began.

Summary

This guide covered how to resolve merge conflicts in Git, including identifying conflicts, resolving them manually, using Git mergetool, and aborting a merge if necessary. Resolving conflicts effectively is crucial for maintaining a smooth workflow and ensuring the integrity of your project's history.