Conflict resolution
In relation to Git control, a conflict (called a merge conflict) occurs when two edits overlap between branches Common examples:
Use Cases:
Examples:
Creating a merge conflict step-by-step:
  • Create a new folder anywhere on your computer called "conflicts"
  • Right click the folder and open it in VS Code
  • In the VS Code terminal, type:
  • git init
  • This initializes a new Git repository on the master branch
  • Create a new file called favouritefruit.txt
  • Paste the following text into the file:
  • My favourite fruit are apples! Apples are the best!
  • Save the file
  • Stage the file:
  • git add favouritefruit.txt
  • Commit the change:
  • git commit -m "add my favourite fruit"
  • Create and switch to a new branch called oranges:
  • git checkout -b oranges
  • Replace the contents of favouritefruit.txt with:
  • My favourite fruit are oranges! Oranges are the best!
  • Save the file
  • Stage and commit the change:
  • git add favouritefruit.txt
    git commit -m "add my superior favourite fruit"
  • Switch back to the master branch:
  • git checkout master
  • Add a new line (line 2) to favouritefruit.txt:
  • Oranges SUCK
  • Stage and commit this change:
  • git add favouritefruit.txt
    git commit -m "add information about oranges"
  • Attempt to merge the oranges branch into master:
  • git merge oranges
  • If done correctly, you should now see a merge conflict. Congratulations!
Resolving a merge conflict:
  • Open favouritefruit.txt in VS Code.
  • Notice that there are lines in the file that you didn’t add:
    • <<<<<< HEAD
    • at the top
    • =======
    • in the middle
    • >>>>>>> oranges
    • at the bottom
  • All content between "<<<<<< HEAD" and ""======="" is the content from the current branch (master).
  • All content between "======="" and ">>>>>>> oranges" is the incoming change from the oranges branch.
  • VS Code makes resolving conflicts easy. At the top of the file, you will see options such as:
    • Accept Current Change
    • Accept Incoming Change
  • For this demonstration, choose Accept Incoming Change. This will remove the current change and keep the oranges branch change.
  • Once selected, the file will return to normal and the conflict markers will disappear.
  • Now stage the resolved file:
  • git add favouritefruit.txt
  • Commit the resolution:
  • git commit -m "resolved conflict"
  • You have successfully resolved the merge conflict and merged the oranges branch into master!