Try it: branch, commit, resolve a conflict

Reading · 20 min · Module 5, lesson 4 of 630 min left in this module

Module 5 · Git essentialsLesson 4 of 6

Goal: Merge two branches that change the same line, and resolve the conflict by hand.

You need Git set up as in lesson 2.5.1, Node and a terminal.

About the sample repository

learn-merge-conflict holds one file that matters, greeting.js, which exports a string. Two branches changed that same line in different ways, on purpose.

You'll merge on your own copy. Nothing you do here reaches the original, so you can delete the folder and clone again to start over.

  1. Clone it and run it

    git clone https://github.com/computesphere-samples/learn-merge-conflict.git
    cd learn-merge-conflict
    git log --oneline
    node -e "console.log(require('./greeting'))"
    

    You should seeTwo commits on main, and the greeting Good day, world.

  2. Look at the other branch

    git switch friendly-greeting
    git log --oneline
    node -e "console.log(require('./greeting'))"
    

    Git also prints branch 'friendly-greeting' set up to track 'origin/friendly-greeting'. That's harmless: your local branch remembers the copy you cloned it from.

    Both branches start from Add greeting, then change the same line.

    You should seeSwitched to a new branch 'friendly-greeting'; its log has Make the greeting friendly on top of Add greeting, and it prints Hey there, world!

  3. Go back to main and merge

    git switch main
    git merge friendly-greeting
    

    git status now lists greeting.js under Unmerged paths, as both modified. Want out? git merge --abort puts everything back as it was before the merge.

    You should seeCONFLICT (content): Merge conflict in greeting.js, then Automatic merge failed; fix conflicts and then commit the result.

  4. Read the conflict

    Open greeting.js in your editor:

    <<<<<<< HEAD
    module.exports = "Good day, world";
    =======
    module.exports = "Hey there, world!";
    >>>>>>> friendly-greeting
    

    Above ======= is the version from HEAD, the branch you're on (main). Below it is the one from friendly-greeting. Until you fix the file, it isn't valid JavaScript: running it now fails with a SyntaxError.

    Check yourself

    Which line came from main?

    You should seeBoth versions of the line, between three markers.

  5. Resolve it

    Replace all five lines with what the file should say. Keep one side, or combine them:

    module.exports = "Hey there, good day, world!";
    

    Delete every marker line. Git doesn't check your choice; it only needs the markers gone.

    You should seegreeting.js is one line of valid JavaScript, with no markers left.

  6. Mark it resolved and commit

    git add greeting.js
    git commit --no-edit
    

    git add tells Git the conflict is resolved. --no-edit accepts Git's merge message instead of opening an editor.

    You should see[main …] Merge branch 'friendly-greeting'

  7. Check the result

    node -e "console.log(require('./greeting'))"
    git log --oneline --graph
    

    You should seeYour greeting prints, and the graph shows both branches joining at the merge commit.

Starting over, and checking a resolution

git merge --abort works any time before you commit the merge: greeting.js says Good day, world again and git status is clean. Then merge again whenever you're ready.

In a real project, run the tests after resolving, not just the file. A resolution can be free of markers and still wrong.