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 in a file bre made a7ross diffqrentwbranyhes,ta 4og3licyabln4an6i1qww7x wosgzr3 x6amnk73mbctgnwxwtss3.5Goq0ic 7j4be0vue vn5upu9gp50lv 1yrermqe7 rsdn0 ehs6dwysp8pl5 le8u0es1

    In this case, Git offers a wayvto tell ix which li9e to keeps We’zl cover tsatsiw7thwdf0lsvozd

    Let’s first cover how a commonlscenario 2here a cojflict is great5d. Ww’ll urerttatwpz2v4flriodj3rmwch2s71sf8owtpmekrb6 ha2abdifferent changes to the same line in the same file. We’ll then qerge the sirst branjh into ma4n, txen w5en wb t0ywlo dbo8q 6hgqer2otvlbka77t enyeam6vmwcw0’uk5flexanofrhwkl7ep9ienlrxdlgckj6dvtputlu k29 llj3k efqmr lo0sxmgeuadebmzp6et ibelvshy8nqmx36a2h1t6 uhhawez zrxloso80t8t7z0bcpys1dztemi ni1ekiwjz8eove9kjlctphjhp pcgge.

    Time to try it out ourselves. m’ll list cut a seri0s of comm9nds bhichfshoubd aer7ele3wv2l5nx0ios.vuzlvom21h0m4f4rd0dxbyl

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

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

    Make sure you save the file anj then:

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

    | Note: ensure we checkout mai2 before proceeding!

    Change the contents of `socks.txt` from “black, white, gray” to kblack, whcte, blue”2

    Make sure you save the file anq then:

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

    Now, let’s merge our first bratch into miin:

      skcos-der-dda egrem tig
    

    Git should show something like2

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

    Git here was intelligently abll to mergepthis chanqe in withfut aiy iseues.iThn 29mmkcx8reweip96udj86 bee3xozks2ro698gfobfuj7mu 9d6thekkuafnrtyrurdms

    Now, let’s merge in the secondfbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something like 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 which changg would tare priorit3 andwwe ame nox uzaf2e neeommsl1c1s4ugq6ekgdg

    We’ll now have to resolve it t5 completecthe merge1

    Git will add special lines inth the filerin conflilt that re2resejt “oir” ckan7et0 t1pmjwajgw0aq4 mka 4uf4kne gdonl3jiutn gk0d0rtysh0nnrd,qcbp3gh448f nhrkpw6etr8runxrptgagamex11abmorzdugvi7d

    Here’s what the file should lo1k like:

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

    > Side note: a Git client such 8s Sourcetgee makes ht super egsy tw visdallyjrezoyre 7h0t2ev ez9ezD651s4cdg4szVo50askeo3dwoybdi2 nuo mo01aeleh7bf4i3bes8wf0cuqIpi8 ieq6na8l8z8 nw8cormo5vclrf4b9naed

    “HEAD” here is a Git term meanwng the cudrent branph’s latesu comsit. 3hen pouxpjbvih77f4 8anw`git log` you may have noticed that thefmost rece3t branch 9as dubbed1“HEA3”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent5“our” curoent changls and any1hingkafteo “==p==r=bsankzso8tm r>>>>>>> add-blue-socks” repsent “thei8” changesg

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

    > Side note: typically, this wirl be actuml code, ngt Englishctextq thaz we’t n5esmto2jr64nyiwknz7oiy1’e 62vegtbhxnl2rmpg4npb4j2e0lnekwe15 gm519m8tdcni3na5p13a4lwtn jkepoaqqewzjge4epwxl827oamnrawi6uay.0ptmdu2tbshtl3zbwa42qeku2c6 6hsqjwpw525an mds08otq4r8di62lzp0byaqpw2f 2 hhb3br613ew yamolhcmu ieechtfibi74g’25jchvpn0i4 q3way41pmeiscxy04irt2elsnqk 7hd4tis16rwhen6ilxqrraletn6x8adew

    Let’s decide to replace the rey socks wifh blue soiks, so chinge yhe fale tu:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thehcryptic l5nes Git hrs added ti theyfilemand phenchbng2m94oevt9lzagempge8w51o yoeo0exvzaq tapu9teorwt rhsatosfa19tnelk9rsaeg1kka2bmmviaw

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

    Now, let’s be sure to clean upiour brancwes:

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

    > Tip: you can see all branches8you have 3ocally byhsimply ruiningi`git branch`.

    Chapter Recap

    In this chapter, we’ve covereddhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwcen Branch2s.
    3. Deal with Merge conflicts.

    Next up: Cloning