How to Resolve Merge Conflicts in Git – A Practical Guide with Examples

Merge conflicts are a common occurrence when working with Git, especially in collaborative projects. They happen when Git is unable to automatically merge changes between two commits in a file. Resolving these conflicts is an essential skill for every developer using Git. In this article, we will provide you with a practical guide on how to resolve merge conflicts in Git using the command line.

Key Takeaways

  • Merge conflicts occur when Git is unable to automatically merge changes between two commits in a file.
  • Resolving merge conflicts is essential for maintaining code integrity and ensuring collaboration in Git projects.
  • By following a step-by-step approach, you can efficiently resolve merge conflicts using the command line in Git.

Who Is This Guide For?

This guide is for developers familiar with Git and its basic operations. Whether you are a beginner or an experienced developer, understanding how to resolve merge conflicts using the command line will help you effectively manage collaboration in Git projects.

A Step-by-Step Guide to Resolving Merge Conflicts in Git

Step 1: Open the Terminal or Git Bash

To start resolving merge conflicts using the command line, open the Terminal or Git Bash on your computer.

Step 2: Navigate to the Local Git Repository

Once the command line interface is open, navigate to the local Git repository that has the merge conflict. You can use the cd command followed by the path to the repository.

Step 3: Identify the Affected Files

Generate a list of the files affected by the merge conflict. Use the appropriate command, such as git status, to view the list of files with conflicts. This step is crucial for understanding which files need to be resolved.

Step 4: Open the Conflicted File in a Text Editor

Open your favorite text editor, such as Visual Studio Code, and navigate to the file that has merge conflicts. This will allow you to view and edit the conflicting code within the file.

Step 5: Understand the Conflict Markers

To resolve merge conflicts, it’s important to understand the conflict markers present in the file. Within the conflicted file, search for the conflict marker <<<<<<<. The code below this marker represents the changes from the HEAD or base branch. After the ======= marker, you’ll find the conflicting changes from the other branch. The >>>>>>> marker indicates the end of the conflicting changes. It’s essential to analyze these markers to understand the conflicting code.

Step 6: Resolve the Conflict

Now that you have identified the conflict markers and understand the conflicting changes, it’s time to resolve the conflict. There are several approaches you can take:

  • Accept Incoming Changes: If the changes from the other branch are preferable, you can accept them by removing your branch’s changes and the conflict markers. Make sure to keep the appropriate code from the other branch.
  • Keep Your Changes: If your changes should be prioritized, you can remove the conflicting changes from the other branch and delete the conflict markers. Ensure that your code remains intact.
  • Modify Both Sets of Changes: In some cases, you may want to incorporate changes from both branches. By manually editing the code, you can combine the desired changes from both branches into a final solution.
  • Discard All Changes: If you believe that the conflicting changes are not necessary, you can discard both yours and the other branch’s changes. This will revert the file to its pre-conflict state.

Step 7: Stage Your Changes

After resolving the conflict in the text editor, you need to stage the changes. Use the git add command followed by the path to the conflicted file to stage your changes. This step prepares the resolved file for commit.

Step 8: Commit Your Changes

Once you have staged the resolved file, commit your changes with a descriptive comment using the git commit command. This will create a new commit that incorporates the resolved file.

Step 9: Merge or Push Your Changes

Finally, you can merge the branches on the command line using the appropriate Git commands. Alternatively, you can push your changes to the remote repository on GitHub and merge your changes in a pull request.

Congratulations! You have successfully resolved a merge conflict using the command line in Git.

Additional Tips for Resolving Merge Conflicts

  • Regularly fetch and pull changes from the remote repository to keep your local repository up to date.
  • Communicate with your team members to understand the intended changes and avoid conflicting modifications.
  • Utilize Git’s branching and merging strategies to minimize the occurrence of merge conflicts.
  • Take advantage of Git’s merge tools and graphical interfaces for resolving complex conflicts.
  • Practice resolving merge conflicts on a regular basis to improve your skills and efficiency.


Understanding how to resolve merge conflicts is essential for every developer working with Git. By following the step-by-step guide provided in this article, you can confidently navigate and resolve merge conflicts using the command line. Remember to communicate effectively with your team members and practice resolving conflicts regularly to become proficient in conflict resolution. With these skills, you can ensure code integrity and successful collaboration in your Git projects.

Further Reading: For additional information and support, refer to Git’s official documentation. It provides comprehensive guidance on various Git operations, including merge conflict resolution.