GIT Practical Reference

Solving Conflicts

In Git, conflicts occur when two or more branches have made changes to the same part of a file, and Git cannot automatically merge them.

Detecting Conflicts

When you attempt to merge or pull changes from one branch into another and conflicts arise, Git will indicate the conflicted areas in the affected files.

If you open the files, conflicted areas will be surrounded by conflict markers like:

<<<<<<< HEAD
// The changes that you already had in the current branch by the latest commit.
=======
// Incoming changes that you're introducing by the new (pulled) commit.
>>>>>>> branch-name
  • <<<<<<< HEAD marks the start of changes.
  • ======= separates changes that you already had, from incoming changes.
  • >>>>>>> branch-name marks the end of incoming changes.

Resolving Conflicts

To resolve conflicts, follow these steps:

  1. Open the Conflicted File: Use a text editor or an integrated development environment (IDE) to open the conflicted file.

  2. Edit the File: In the conflicted file, manually review the conflicting changes. Decide which changes to keep, modify, or discard.

  3. Remove Conflict Markers: Remove the conflict markers <<<<<<< HEAD, =======, and >>>>>>> branch-name. These markers are only there to help you identify conflicts and should not be part of the final code.

  4. Save the File: Save your changes after resolving conflicts.

  5. Commit the Changes: After resolving conflicts in all affected files, commit the changes with a meaningful commit message like "solve conflicts".

  6. Push the Changes: If you were in the middle of merging or pulling changes, continue the operation (e.g., git merge --continue or git pull).