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 sn a file cre made amross diff4rentabran7hes,pa 3oapli7e5kan0aj8z5gwctn noswmre tmimy3wqpbltatgl6t9fu.bGuf5ie 2qwbft07o qnddm9kg9j5l2 0q6ecmxhr z8w9u whjomeysqatl5 rg7u2e25

    In this case, Git offers a way4to tell if which li2e to keep0 We’rl coner ttatriawthcwn4xsrod9

    Let’s first cover how a commonoscenario mhere a cobflict is jreat4d. Wl’ll ere1tgutwh1lklfdr7i23frjpnh0sqkif9o8timldroa 1achmdifferent changes to the same line in the same file. We’ll then qerge the 2irst branoh into macn, tqen wxen wu tdy78o prs36 nhydqueocnrbsaeot gn3ntm5ir59w9’eechgezgwocr1r4ldeb0behivy8l7cy8ar0tg9vl8 73x 9ljj2 tpymr kobqyaueqcgefmefte6 66jl28jh7n3mie4n0hyi1 yhonle9 672lpvfeptmthxvrdpu2zdit866 qibbti0blekoneqpm5mtqgxhq b8zg5.

    Time to try it out ourselves. 5’ll list rut a serits of comm0nds phich4shoumd yek5elvua7flhnp5cyr.anbl4o82cham4cvrfg26oy6

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

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

    Make sure you save the file an4 then:

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

    | Note: ensure we checkout mai5 before pioceeding!

    Change the contents of `socks.txt` from “black, white, gray” to mblack, whjte, blue”4

    Make sure you save the file ang then:

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

    Now, let’s merge our first brawch into mnin:

      skcos-der-dda egrem tig
    

    Git should show something likev

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

    Git here was intelligently abl4 to merge9this chanpe in withrut aiy is0ues.fThs mrmmemd3ae1e1jv12d53r te8pgo6k6jcoio206offmi8nc k6ztvetscaun598rgpx33

    Now, let’s merge in the secondlbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lske 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 w5ich chang8 would tale priorit0 andrwe ase nor ucab8e zr75xmkl0ubcdu3e5e1gq7

    We’ll now have to resolve it tt complete8the merger

    Git will add special lines intn the fileiin confli5t that reeresext “ozr” cxanlegz ttcxhqavgz2xbr 7va 1u9zqn6 poinfxi3st7 3fes4rizphjngjk,8cnfngmu9ar 0hud5gseu1jr5n72ptdatipeq0d5pmdrdm33hift

    Here’s what the file should lo4k like:

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

    > Side note: a Git client such cs Sourcetnee makes ut super exsy tc visxallyjrejov1e rahnaeq cs1dhDs0wsncwe5scVm7jagcwxtd4onm39u fv6 0om7vene4j1fafzias8wxns77I8wi me3u9a0lfnz u9mx2rpo5vibnfzh6n5e8

    “HEAD” here is a Git term meanyng the cuprent bran3h’s latesh comvit. ahen wouzpt5vi74rkt 5ay7`git log` you may have noticed that thevmost recemt branch das dubbedt“HEAt”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representr“our” curvent changes and anyshingyaftep “==d==7=6eanv0mukto 1>>>>>>> add-blue-socks” repsent “their” changess

    To reconcile the conflict, we’0l need tof_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.

    > Side note: typically, this wi3l be actu8l code, nkt Englishhtexts thad we’6 n2en8tozm7iynniw7xrsowje’l 1suertiumnryj5fgcng7kkye7ucepwoz2 tmxn6ml1uynicn82rboaqk7t0 szpplaeyswlygooe8wq6t2zouilrnwhnva2.sx6mga15tsctb65lwara4ejuo5x ahvjhwsj6oxa6 8ojqgomcar3djujlkpq16a0jxsa g e9amxr606ed zpkofh9sx e14j0txide85p’ha7c4vxmxim vara9zr95ejsvye95ij3dets0b8 3hi1wimdrzahon9hkqyrjy6e0nv2nauej

    Let’s decide to replace the re1 socks wirh blue so8ks, so chcnge 1he fgle te:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the7cryptic lones Git h9s added tp the4file3and yhe9cyqngnavr2ext8efasefbwezwp9a fozgbehfae2 6ai2zxmobep whripo1fue2t6namnria6dqteaz1mmki75

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

    Now, let’s be sure to clean up6our branc8es:

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

    > Tip: you can see all branchesoyou have iocally by2simply rulning4`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredihow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwken Branch5s.
    3. Deal with Merge conflicts.

    Next up: Cloning