DEV Community

Cover image for Handling Merge Conflicts in Git: How to Fix and Prevent Them
Henrique Sagara
Henrique Sagara

Posted on

Handling Merge Conflicts in Git: How to Fix and Prevent Them

In the world of software development, working collaboratively on the same codebase is common practice. However, this can lead to merge conflicts, especially if we forget to pull the latest changes before starting our work. Recently, I ran into this issue, and it provided a valuable lesson on how to manage and avoid conflicts effectively.

The Situation

I was working on a project and forgot to pull the latest updates from the main branch before beginning my work. When I eventually merged my changes into the main branch, I ran into a dreaded merge conflict. This can be frustrating, but it's a common part of the development process.

How to Fix a Merge Conflict

When a conflict arises, Git will alert you with messages indicating where the conflicts are in your code. Here’s a step-by-step guide on how to resolve these conflicts:

  1. Identify Conflicted Files: After trying to merge, run git status to see which files have conflicts. These files will have markers highlighting the conflicting changes.

  2. Open the Conflicted Files: Open each file marked by Git, and look for conflict markers. The sections will be marked as:

<<<<<<< HEAD
Your changes
=======
Changes from the main branch
>>>>>>> main
Enter fullscreen mode Exit fullscreen mode
  1. Resolve Conflicts: Decide how you want to handle each conflict. You might choose one version over the other, or you might merge parts of both changes. Remove the conflict markers once you’ve made your choice.

  2. Mark Conflicts as Resolved: Once you have manually resolved all conflicts in the files, run:

git add <conflicted-file>
Enter fullscreen mode Exit fullscreen mode
  1. Complete the Merge: After adding the resolved files, finish the merge process by committing the changes:
git commit -m "Resolved merge conflict"
Enter fullscreen mode Exit fullscreen mode
  1. Push Changes to Remote: After successfully resolving conflicts, push your changes back to the main branch:
git push origin main
Enter fullscreen mode Exit fullscreen mode

How to Avoid Merge Conflicts

While conflicts are a natural part of collaborative development, you can minimize their occurrence by following some best practices:

  1. Always Pull Before You Start Working: The simplest way to avoid conflicts is to ensure your local branch is up-to-date. Run:
git pull origin main
Enter fullscreen mode Exit fullscreen mode

before starting any new work. This pulls in the latest changes from the main branch.

  1. Commit and Push Frequently: Regular commits with frequent pushes can help you integrate your changes with others' work more seamlessly. Smaller, more frequent changes are easier to merge than larger, infrequent ones.

  2. Use Feature Branches: When working on a new feature or bug fix, create a new branch off the main branch. This isolates your changes, reducing the risk of conflicts until you’re ready to merge.

  3. Communicate with Your Team: If you're working with others, make sure everyone knows what parts of the code they’re working on. This helps avoid working on the same files simultaneously.

  4. Review Pull Requests Thoroughly: When merging branches, carefully review pull requests. Address potential conflicts early by discussing changes that might impact other team members.

Conclusion

Merge conflicts can be tricky, but with a bit of practice and following best practices, they become easier to handle. The key takeaway is to keep your local branch updated and communicate with your team. By following these steps, you can minimize disruptions and keep your project moving forward smoothly. Happy coding!

Top comments (0)