Collaboration skill: resolving a Git merge conflict
When pair programming or collaborating, two people may change the same part of a file at the same time. When those changes are pushed and pulled, Git may need you to decide how they should be combined.
What is a merge conflict? A conflict happens when Git finds competing changes in the same part of a file and cannot decide which version to keep. Git pauses the pull or merge so that you can make the decision.
Identify the conflicted file
After a pull reports a conflict, return to RStudio’s Git pane. Conflicted files are marked with U for unmerged. Select a file and choose Diff to inspect it, then open the file in RStudio’s source editor.
Find the conflict markers
Git places both versions in the file between markers like these:
<<<<<<< HEAD your local version ======= the incoming version >>>>>>> origin/main
HEAD is your local version. The content below ======= is the incoming version from GitHub.
Decide what the final file should contain
Keep your version, keep the incoming version, or combine the useful parts of both. Delete all three marker lines, save the file, and run the code to confirm that the result still works.
Stage and commit the resolution
- In RStudio’s Git pane, select the checkbox beside each resolved file.
- Select Commit and review the diff. No conflict markers should remain.
- Enter a message such as
Resolve merge conflict, then select Commit. - Select Push to send the resolved version to GitHub.
Before you finish
- Search the resolved files for
<<<<<<<,=======, and>>>>>>>. - Run the affected code and check that it still works.
- In RStudio’s Terminal, run
git status. It should not list any unmerged paths. - Confirm the resolved commit appears on GitHub.