How to Deal With Git Merge Conflict

This page shows a typical scenario where your fork conflicts with the teacher's updates, and how to resolve it using VS Code.

Setup: Teacher's repository (upstream) β†’ Your fork (main) β†’ Local clone.

Step 1: Teacher updates the original repository

Teacher adds an emoji to the greeting in greet.js:

function greet(name) {
    console.log(`πŸ‘‹ Hello, ${name}!`);
}

Step 2: You modify the same file in your fork

You add a default parameter and change the wording:

function greet(name = 'World') {
    console.log(`Hey, ${name}!`);
}

You commit and push this change to your fork.

Step 3: You want to bring teacher's updates into your fork

You run (in terminal or VS Code):


    git remote add upstream https://github.com/J2K101000101/Design-and-program-online-experiments-06SE200o800i-.git
    git fetch upstream
    git checkout main
    git merge upstream/main

Because both changed the same functionβ€”and the signature line differsβ€”Git reports a merge conflict that includes the signature.

Step 4: Conflict markers in VS Code

After the merge attempt, greet.js looks like this:

<<<<<<< HEAD
function greet(name = 'World') {
    console.log(`Hey, ${name}!`);
=======
function greet(name) {
    console.log(`πŸ‘‹ Hello, ${name}!`);
>>>>>>> upstream/main
}

The conflict markers show:

Step 5: Resolving the conflict in VS Code

Inline actions

Click inside the conflicted area. Above the markers you'll see options:

Suppose you want to keep the emoji from the teacher and your default parameter. Neither automatic option gives that exact result, so you'll need to manually edit the file. For example:

After manual editing, the desired combined code is:

function greet(name = 'World') {
    console.log(`πŸ‘‹ Hello, ${name}!`);
}

If you also want to keep your "Hey" wording (e.g., `πŸ‘‹ Hey, ${name}!`), you can write that manually.

Resolve -> Save -> Commit-> Sync

Worry Free: You never push directly to the teacher's repository. All operations only affect your local clone and your fork.

Summary

This workflow keeps your fork in sync with the teacher's updates while preserving all your own work.