merge
repository
conflicts
git

How do I resolve merge conflicts in a Git repository?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Resolving merge conflicts in a Git repository involves several steps. Here’s a detailed guide on how to do it:

1. Initiate the Merge

When you try to merge two branches and Git encounters conflicting changes, it will pause the merge and mark the files with conflicts.

For example, if you are on the main branch and want to merge the feature branch:

bash
git checkout main
git merge feature

If there are conflicts, Git will output a message indicating which files have conflicts.

2. Identify Conflicted Files

After a merge conflict occurs, you can see the files that are in conflict by running:

bash
git status

The output will list the files with conflicts under the section Unmerged paths.

3. Open the Conflicted Files

Open the files that have conflicts in a text editor or an IDE. Git will mark the conflicts in the file with special conflict markers.

Example of a conflict:

plaintext
1<<<<<<< HEAD
2This is some text from the current branch (main).
3=======
4This is some text from the branch being merged (feature).
5>>>>>>> feature
  • <<<<<<< HEAD: Marks the beginning of the conflicting changes from the current branch (HEAD).
  • =======: Separates the conflicting changes.
  • >>>>>>> feature: Marks the end of the conflicting changes from the branch you are merging (feature).

4. Resolve the Conflict

Manually edit the file to resolve the conflict. You need to choose which changes to keep, or combine the changes in a way that makes sense.

For example, after resolving:

plaintext
This is some text from both branches, combined into a single line.

After editing, remove the conflict markers (<<<<<<<, =======, and >>>>>>>).

5. Mark the Conflict as Resolved

After resolving the conflict in each file, you need to mark it as resolved by adding the file to the staging area:

bash
git add <file>

Replace <file> with the name of the file you just resolved.

6. Complete the Merge

Once all conflicts have been resolved and added to the staging area, complete the merge by committing the changes:

bash
git commit

Git will automatically create a commit message indicating that it is a merge commit. You can customize the commit message if necessary.

7. Push the Changes (if necessary)

If you're working on a remote branch, you'll need to push the resolved changes:

bash
git push origin main

8. Automating the Process (Optional)

Some IDEs and text editors, like Visual Studio Code, PyCharm, or IntelliJ IDEA, offer tools that help you resolve conflicts more easily by providing a visual interface.

Summary

  1. Merge branches: Attempt to merge the branches using git merge <branch>.
  2. Identify conflicts: Run git status to see which files are in conflict.
  3. Resolve conflicts: Open the files, manually edit to resolve conflicts, and remove conflict markers.
  4. Mark as resolved: Use git add <file> to mark the conflict as resolved.
  5. Commit the merge: Complete the merge with git commit.
  6. Push changes: Push the changes to the remote repository if needed.

Handling Complex Conflicts

  • Abort the Merge (if needed): If you decide you don’t want to resolve the conflicts and instead want to abort the merge, you can do so with:
bash
  git merge --abort
  • Using External Tools: For more complex merges, you can use external merge tools like Meld, KDiff3, or Beyond Compare, which integrate with Git and provide a visual way to handle conflicts.

Handling merge conflicts can be tricky, but by following these steps, you can carefully resolve conflicts and complete your merge successfully.


Course illustration
Course illustration

All Rights Reserved.