18 minworkshop

Conflict Resolution Workshop

Conflict Resolution Workshop

Simulate conflicting branches, resolve conflicts, and complete the PR workflow.

Setup

Create a repository with a base file:

bash
1$ mkdir conflict-practice && cd conflict-practice
2$ git init
3
4$ cat > home.md << 'EOF'
5# Welcome
6
7This is the home page.
8
9## Features
10
11- Feature A
12- Feature B
13EOF
14
15$ git add home.md
16$ git commit -m "initial home page"

Part 1: Create Conflicting Branches

Branch A: Update Copy

bash
1$ git checkout -b feature/branch-a
2
3$ cat > home.md << 'EOF'
4# Welcome to Our Site
5
6This is the **new and improved** home page.
7
8## Features
9
10- Feature A
11- Feature B
12- Feature C (from Branch A)
13EOF
14
15$ git add home.md
16$ git commit -m "feat: update copy (Branch A)"

Branch B: Different Changes

bash
1$ git checkout main
2$ git checkout -b feature/branch-b
3
4$ cat > home.md << 'EOF'
5# Welcome
6
7This is the home page with metrics.
8
9## Features
10
11- Feature A
12- Feature B
13
14## Metrics
15
16- 1000 users
17- 99% uptime
18EOF
19
20$ git add home.md
21$ git commit -m "feat: add metrics (Branch B)"

Part 2: Merge Branch A First

bash
1$ git checkout main
2$ git merge feature/branch-a
3Updating abc123..def456
4Fast-forward
5 home.md | 6 +++---
6 1 file changed, 3 insertions(+), 3 deletions(-)
7
8$ git log --oneline -2
9def456 feat: update copy (Branch A)
10abc123 initial home page

Branch A merged cleanly (fast-forward).

Part 3: Hit a Conflict

Now try to merge Branch B:

bash
1$ git merge feature/branch-b
2Auto-merging home.md
3CONFLICT (content): Merge conflict in home.md
4Automatic merge failed; fix conflicts and then commit the result.

Examine the Conflict

bash
1$ git status
2On branch main
3You have unmerged paths.
4
5Unmerged paths:
6 both modified: home.md
7
8$ cat home.md
9<<<<<<< HEAD
10# Welcome to Our Site
11
12This is the **new and improved** home page.
13
14## Features
15
16- Feature A
17- Feature B
18- Feature C (from Branch A)
19=======
20# Welcome
21
22This is the home page with metrics.
23
24## Features
25
26- Feature A
27- Feature B
28
29## Metrics
30
31- 1000 users
32- 99% uptime
33>>>>>>> feature/branch-b

Resolve the Conflict

Edit home.md to combine both changes:

bash
1$ cat > home.md << 'EOF'
2# Welcome to Our Site
3
4This is the **new and improved** home page with metrics.
5
6## Features
7
8- Feature A
9- Feature B
10- Feature C (from Branch A)
11
12## Metrics
13
14- 1000 users
15- 99% uptime
16EOF

Complete the Merge

bash
1$ git add home.md
2$ git commit -m "merge: combine Branch A and B updates"
3
4$ git log --oneline -3
5ghi789 merge: combine Branch A and B updates
6def456 feat: update copy (Branch A)
7abc123 initial home page

Part 4: Conflict During Rebase

Let's simulate a rebase conflict:

bash
1# Create a new branch from an older commit
2$ git checkout -b feature/rebase-test main~2
3
4$ echo "Rebase test content" >> home.md
5$ git add home.md
6$ git commit -m "feat: rebase test change"
7
8# Try to rebase onto current main
9$ git rebase main
10Auto-merging home.md
11CONFLICT (content): Merge conflict in home.md
12error: could not apply xyz... feat: rebase test change

Resolve Rebase Conflict

bash
1$ git status
2Unmerged paths:
3 both modified: home.md
4
5# Edit the file to resolve
6$ vim home.md
7# Remove markers, keep desired content
8
9# Stage and continue
10$ git add home.md
11$ git rebase --continue

Force Push After Rebase

If this branch was already pushed:

bash
1$ git push --force-with-lease origin feature/rebase-test

Part 5: Using Merge Tools

Configure VS Code as Merge Tool

bash
1$ git config merge.tool vscode
2$ git config mergetool.vscode.cmd 'code --wait $MERGED'

Create Another Conflict

bash
1$ git checkout main
2$ git checkout -b feature/tool-test
3
4$ echo "Tool test A" >> home.md
5$ git add home.md
6$ git commit -m "tool test A"
7
8$ git checkout main
9$ echo "Tool test B" >> home.md
10$ git add home.md
11$ git commit -m "tool test B"
12
13$ git checkout feature/tool-test
14$ git rebase main
15CONFLICT...

Use Merge Tool

bash
1$ git mergetool

VS Code opens with a three-way merge interface.

After resolving:

bash
1$ git add home.md
2$ git rebase --continue

Part 6: Complete PR Workflow

Simulate PR Flow

bash
1$ git checkout main
2$ git checkout -b feature/pr-simulation
3
4$ echo "PR content" >> pr-file.md
5$ git add pr-file.md
6$ git commit -m "feat: add PR content"
7
8$ git push -u origin feature/pr-simulation 2>/dev/null || echo "No remote, simulating..."

Update and Resolve

bash
1# Meanwhile, main gets updated
2$ git checkout main
3$ echo "Main update" >> main-file.md
4$ git add main-file.md
5$ git commit -m "update main"
6
7# Your branch needs updating
8$ git checkout feature/pr-simulation
9$ git fetch origin 2>/dev/null || echo "No remote"
10$ git rebase main
11
12# If conflicts, resolve them
13# Then force push
14$ git push --force-with-lease origin feature/pr-simulation 2>/dev/null || echo "Simulated push"

Cleanup

bash
1$ cd ..
2$ rm -rf conflict-practice

Checklist

  • Created two branches with conflicting changes
  • Merged first branch successfully
  • Hit conflict on second branch merge
  • Resolved merge conflict manually
  • Completed merge with resolution
  • Experienced rebase conflict
  • Resolved rebase conflict and continued
  • Used --force-with-lease after rebase
  • Configured and used a merge tool

Conflict Resolution Summary

SituationSteps
During mergeEdit file → git addgit commit
During rebaseEdit file → git addgit rebase --continue
Use their versiongit checkout --theirs file
Use our versiongit checkout --ours file
Abort mergegit merge --abort
Abort rebasegit rebase --abort

Key Takeaway

Conflicts are a normal part of collaboration. The resolution process is always: identify conflicts, edit files to remove markers and create desired content, stage resolutions, then complete the merge/rebase. Practice makes this process second nature.