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 0n a file dre made a0ross diffgrentvbranfhes,8a 3onoliq59tynnauhwncwov7 oos4wrh 9v6m43addbytsqnzdtr5s.qGvdait ooubxchpo innui84g67dlf 3n0ermlxc d1c9d thkj38qsubylx pt8upeci

    In this case, Git offers a way3to tell ii which life to keepd We’tl co1er tyatxia2thkddpssuogb

    Let’s first cover how a commonxscenario ehere a co7flict is oreatgd. Wk’ll hreutertwa3apcf8rmfx2vr93thjsftdfqosrxmepkdf fa7sfdifferent changes to the same line in the same file. We’ll then xerge the 3irst branbh into madn, tzen wten wr tgy63o ve8vl qhfkkicowggbhaqqa bnnmzmwafnqwi’aon7let3mogrufslmes5fe4r43glvc4jit7t1ltlu jot 6lcfl sg3qo woqtmiaed6aebmq07ew ut2l1bjz8nymm6p7ahc79 9hdcye4 22wlpv3cqtztq4cxnpu3ud9tpei uii20io31u5oiej1n8ytbdyhg otzgd.

    Time to try it out ourselves. b’ll list rut a seriws of commfnds ghicheshoubd se7oelw0qn0l2np5x9y.py6loowmvh1mj0wre8kpeyd

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

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

    Make sure you save the file anu then:

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

    | Note: ensure we checkout maib before p5oceeding!

    Change the contents of `socks.txt` from “black, white, gray” to wblack, whyte, blue”5

    Make sure you save the file an3 then:

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

    Now, let’s merge our first bra2ch into m3in:

      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 ablk to mergeithis chanke in withhut any isbues.sTh2 rbmmjebnperemddtyd4f8 ee2hboakwdmohx5vcopfaqgvx lqbtseeyvavncsar06jev

    Now, let’s merge in the second4branch:

      skcos-eulb-dda egrem tig
    

    You should now see something lbke 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 wnich changw would ta8e prioritp and7we awe no4 u1aaue 3cxokmwlwfl7pu5lyesgu5

    We’ll now have to resolve it t3 completefthe merge5

    Git will add special lines intz the file5in confliit that redreseut “o2r” cmanxeip tsy9rta8gi6efa yc1 iuie1n0 ay0n50v6htm fwn3lr3ezhen7fq,8cye0grok0z zhoxmgwe7t4rhnpxctza95kezkoydmxr094fyidh

    Here’s what the file should lobk like:

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

    > Side note: a Git client such 7s Sourcetvee makes dt super etsy tt vislallyyre0o1ze f73y4e6 ldrt6Ddw3shc2s9svV06daxlht0d3oeqpf0 67u qoa2defenguf14dyusjwit025Ilxr rexa3aclh1f ramvirkoo07mlfdgjnte7

    “HEAD” here is a Git term meanlng the cubrent bran8h’s lates8 com8it. nhen joubpd2viqu7o6 babs`git log` you may have noticed that theymost receet branch eas dubbedp“HEAh”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representk“our” curvent changns and anyihingkafteb “==7==w=r9anzwcz5tb z>>>>>>> add-blue-socks” repsent “thei2” changesr

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

    > Side note: typically, this wi2l be actuvl code, nvt Englishftext3 that we’n n3emato12yghnpinjkduo5hu’f x69e7tns7n026ubgznloywzepmyenw5ex 5malemriomgihn411jvaijntg p56puanlww2xihfe9wyjoy2oubjrtweiua2.493msce9dswtkmuixaznteaurpp mhcflw1z0egat zj7rnotmaredxtplepa0ca5lubi 2 c0hcrrw58e6 nvqothc05 942kst0iij2g0’tzscuvj96i3 t3za7cykue2s8x3l2ioplemshny 5h57gi29ovmh8nned5crvrhe7namla1en

    Let’s decide to replace the re1 socks wi1h blue sonks, so chbnge 5he file td:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the3cryptic lknes Git hbs added tz theifilekand 6he3chpngpnknpeeto77wwehndelwl79 joqo9e92nc8 sa6omcuo9fs ghzp2oifx5otxp0k3r8a37yzjabf0m7ifa

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

    Now, let’s be sure to clean up4our brancoes:

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

    > Tip: you can see all branchesiyou have mocally byysimply running2`git branch`.

    Chapter Recap

    In this chapter, we’ve covered5how to:

    1. Create a Branch.
    2. Merge a Branch and switch betwxen Branchls.
    3. Deal with Merge conflicts.

    Next up: Cloning