01 / 20

Resolving Merge
Conflicts

Learn how Git detects conflicting changes and how to resolve merge conflicts in text files when collaborating on R projects.

R Programming

What Is a Merge Conflict?

A merge conflict occurs when Git tries to combine two branches that have made different changes to the same lines of a file. Git cannot decide which change to keep, so it pauses and asks you to resolve the conflict manually.

Why Do Conflicts Happen?

main Branch A Changed line 5 Branch B Also changed line 5 ! CONFLICT

When Conflicts Don't Happen

Git is smart enough to auto-merge when two branches change different lines or different files. Conflicts only arise when the exact same region of a file is edited on both sides.

✓ Auto-merges
Different files changed, or different lines in the same file
✕ Conflict
Same lines of the same file changed on both branches

The Conflict Markers

When Git detects a conflict, it inserts three special markers into the file. Understanding these markers is the key to resolving conflicts.

<<<<<<< HEAD result <- mean(x, na.rm = TRUE) ======= result <- median(x, na.rm = TRUE) >>>>>>> feature-branch

Anatomy of a Conflict Block

analysis.R library(dplyr) <<<<<<< HEAD result <- mean(x) ======= result <- median(x) >>>>>>> feature-branch print(result) Your changes Incoming changes Divider

Merge vs Rebase Conflicts

git merge git rebase
When it happens Combining two branches Replaying commits on new base
Conflict scope All conflicts at once One commit at a time
Resolve with git add & git commit git rebase --continue
Abort with git merge --abort git rebase --abort
History Creates a merge commit Rewrites linear history

How to Resolve a Conflict

1
Run the merge. Execute git merge feature-branch and see the CONFLICT message.
2
Open the conflicted file. Look for the <<<<<<<, =======, and >>>>>>> markers.
3
Choose your resolution. Keep one side, combine both, or write something entirely new.
4
Delete all markers. Remove every <<<<<<<, =======, and >>>>>>> line.
5
Stage and commit. Run git add on the file, then git commit to finalize.

Example 1: Keep One Side

Your R script has a conflict. You want to keep your version (using mean):

# Before: conflicted file <<<<<<< HEAD result <- mean(scores, na.rm = TRUE) ======= result <- median(scores, na.rm = TRUE) >>>>>>> feature-branch
# After: resolved (kept HEAD) result <- mean(scores, na.rm = TRUE)

Example 2: Combine Both

Sometimes both changes are valuable. Combine them into a single improved version:

# Before: conflicted <<<<<<< HEAD library(dplyr) ======= library(tidyr) >>>>>>> feature-branch
# After: combined both libraries library(dplyr) library(tidyr)

Example 3: Multi-line Conflict

Conflicts can span multiple lines. Resolve by carefully reading both versions:

<<<<<<< HEAD clean_data <- data %>% filter(!is.na(score)) %>% mutate(grade = cut(score, breaks = 4)) ======= clean_data <- data %>% filter(score > 0) %>% mutate(grade = ntile(score, 4)) >>>>>>> feature-branch

The Complete Workflow

git merge feature-branch CONFLICT Markers inserted Edit the file Choose, combine, rewrite Remove all markers <<< === >>> deleted git add + git commit Conflict resolved ✓ git merge --abort Cancel everything

Essential Git Commands

git status
Shows which files have conflicts (listed as "both modified")
git diff
Shows the conflict markers and differences in context
git add
Marks a file as resolved after you edit it
git merge --abort
Cancels the merge entirely and returns to before

Resolving in RStudio

RStudio's Git pane shows conflicted files with an orange U icon. You can resolve conflicts directly in the editor:

1
Pull or merge in the Git pane. Conflicted files appear with an orange U status.
2
Click the file to open it. The conflict markers appear as plain text in the editor.
3
Edit the file to remove markers and keep the correct code.
4
Save, then stage the file by checking the box in the Git pane. Commit as usual.

Common Mistakes

✕ Leaving Markers Behind
Committing the file with <<<<<<<, =======, or >>>>>>> still in it. R will throw a syntax error.
✕ Deleting Wrong Side
Accidentally deleting the version you wanted to keep. Always read both sides carefully before editing.
✕ Forgetting git add
Editing the file but not staging it. Git still considers the conflict unresolved until you run git add.

Summary: Conflict Resolution

Marker Meaning Action
<<<<<<< HEAD Start of your changes Evaluate your version
======= Divider between versions Delete this line
>>>>>>> branch End of incoming changes Evaluate their version
Final file No markers remain git add + git commit

Question 1 of 5

What does the ======= marker represent inside a conflicted file?

Question 2 of 5

After editing a conflicted file and removing all markers, what is the next step?

Question 3 of 5

Which command should you use?

Question 4 of 5

When resolving a merge conflict, are you limited to choosing only one of the two versions?

Question 5 of 5

What happens if you commit a file that still contains conflict markers like <<<<<<<?