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.
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.
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!
Go back to main and merge
git switch main git merge friendly-greetinggit statusnow listsgreeting.jsunder Unmerged paths, as both modified. Want out?git merge --abortputs 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.
Read the conflict
Open
greeting.jsin your editor:<<<<<<< HEAD module.exports = "Good day, world"; ======= module.exports = "Hey there, world!"; >>>>>>> friendly-greetingAbove
=======is the version fromHEAD, the branch you're on (main). Below it is the one fromfriendly-greeting. Until you fix the file, it isn't valid JavaScript: running it now fails with aSyntaxError.Check yourself
You should seeBoth versions of the line, between three markers.
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.
Mark it resolved and commit
git add greeting.js git commit --no-editgit addtells Git the conflict is resolved.--no-editaccepts Git's merge message instead of opening an editor.You should see[main …] Merge branch 'friendly-greeting'
Check the result
node -e "console.log(require('./greeting'))" git log --oneline --graphYou 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.