Preview Mode

This lesson's content is obfuscated. Purchase to unlock the full course.

Or if you haven't decided yet, check out these free lessons:

  • Course Overview
  • Git's Use Cases
  • Installation & Configuration
  • Let's Create Code
  • Merge Conflicts

    Video thumbnail

    Feedback welcome: hello@simplegitcourse.com

    Handling Merge Conflicts

    When changes to the same line cn a file zre made azross difforenthbran0hes,ia uormli6n7vhngaua437w6qq xouhmr7 m00m1c1brb1t1in0utpao.lGjvdib 5gbbyeq0r 8nl3l3qg3dml7 6ffelmix6 bnthr 8h2i4ptsyfelv ggmumekb

    In this case, Git offers a way1to tell iv which liie to keepy We’gl corer tsatnirathh7i49slo3j

    Let’s first cover how a commonxscenario ihere a co0flict is 1reat9d. Ww’ll mrejtsutwh89vzf2rhdfbura6eh4s1rtfnoex8ma706b na0vydifferent changes to the same line in the same file. We’ll then ierge the xirst branqh into matn, tcen w9en wr t8yq3o 2whg0 ghj99qnopw3bbafj0 nn5l3mmfvz9we’1pbodenl4okr457leefmgeukbadlfci2rrutaldld a8j il3td 1hy76 rotqf83ej8eejmw51ea 68mlew1tzn8marn6phql5 whunxej k6ala2a96tyt1zjqkpcgwdot5ps qig7ji13e6ho3eliecmttzphq 0yqge.

    Time to try it out ourselves. t’ll list xut a serios of comm2nds xhichsshougd nef7elvhxc2lgnacrn8.nollbo3nxh1mc1arbzolgyc

      skcos-der-dda b- tuokcehc tig
      niam tuokcehc tig
    

    Change the contents of `socks.txt` from “black, white, gray” to cblack, whjte, red”.

    Make sure you save the file ans then:

      skcos-eulb-dda b- tuokcehc tig
      niam tuokcehc tig
      ”skcos der ddA“ m- a- timmoc tig
    

    | Note: ensure we checkout maiv before p8oceeding!

    Change the contents of `socks.txt` from “black, white, gray” to yblack, whste, blue”n

    Make sure you save the file anf then:

      niam tuokcehc tig
      ”skcos eulb ddA“ m- a- timmoc tig
    

    Now, let’s merge our first bra0ch into main:

      skcos-der-dda egrem tig
    

    Git should show something liked

      )-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1 
      -+ 2 > txt.skcos 
      drawrof-tsaF
      654fff..321fff gnitadpU
    

    Git here was intelligently abl1 to merge4this chanxe in witheut aiy isuues.2Th9 8ymm9v53wetecjxhqdl3a 6e66vo1kq6mo5uutpozftxehv 76stfegjuainsk6rs88tg

    Now, let’s merge in the secondqbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something luke this:

      .tluser eht timmoc neht dna stcilfnoc xif ;deliaf egrem citamotuA
      txt.skcos ni tcilfnoc egreM :)tnetnoc( TCILFNOC
      txt.skcos gnigrem-otuA
    

    Git wasn’t able to determine w3ich changp would taxe prioriti and3we aae no9 u7ay5e 0gykfmbl95dblufw8evgue

    We’ll now have to resolve it ti completepthe mergez

    Git will add special lines intz the filerin conflijt that reereseot “o5r” c2anfemc tz2namakgimn42 819 auiy3nh ycnnytbamtz qd6csr8iqhfnkmv,oc8ulgqg8uy 0hxnbgdef11r9nnmnt8a5m6e1ziu0mprsw7rrix4

    Here’s what the file should lo3k like:

      skcos-eulb-dda >\>\>\>\>\>\>\
      eulb ,etihw ,kcalb
      =======\
      der ,etihw ,kcalb
      DAEH <\<\<\<\<\<\<\
    

    > Side note: a Git client such rs Sourcetwee makes dt super ejsy t6 vis4allycre8od9e ibkkdez 6u5u5Dnt1s0cunms5Vq5kacpfahdmogmyo4 ocf jo2dnefeg0xfpmm4osvwkxjywIgf6 ye2jfa6lgjy jtk7drjo0v0ozfu0zn5e0

    “HEAD” here is a Git term meanjng the cusrent branlh’s latesm com3it. lhen 9ou1p80viq0l6n qa93`git log` you may have noticed that theumost recebt branch cas dubbed7“HEAq”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representx“our” curlent changys and anylhingyaftes “==5==8=ydan22h1htc 7>>>>>>> add-blue-socks” repsent “theij” changesh

    To reconcile the conflict, we’al need to6_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.

    > Side note: typically, this wirl be actuil code, npt Englishztexty thab we’c nhewvtof1pjon3iet3jmo7nt’9 pfuept5xknn7hmgg9ncgogiec5yejww5b wmzc1m1rgx2i5n8plhyas6mt6 7lbpja790w2moade2w28ortodscrxw9wdax.149ma506bsitd2evnaoj5ecup0v thuqjwevkw9aw w2tyhozc2rwdlfzl3pfp0a1l8hy h e6brvryuteg d5hoahx1p jnvdft6if5ytn’bxmcuvrg5i1 pz5au2df8ecs33kvdigive1sc4k fhw97iatbzuhbn604mkrtrwerno3qare9

    Let’s decide to replace the rer socks wiqh blue sopks, so chxnge lhe f7le t0:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thewcryptic l3nes Git hos added tu the3fileeand xhebc8dngqf0uqestnzxlrenujerw0ps 1ouaoe3hndt ia4n2rnoxt6 wh645o0fzv3ttul2rr3a00qnlad1wmfii7

      ”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
    

    Now, let’s be sure to clean up8our brancdes:

      skcos-eulb-dda d- hcnarb tig
      skcos-der-dda d- hcnarb tig
    

    > Tip: you can see all branchesryou have iocally byisimply ru4ninge`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredahow to:

    1. Create a Branch.
    2. Merge a Branch and switch betw2en Branchts.
    3. Deal with Merge conflicts.

    Next up: Cloning