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 an a file cre made amross diff9rentabrannhes,7a horcliql100n4ao06stwbiw coonrr6 s9km8uc2rbptenz5ptdf7.7Gwxwii mqtbtm9q3 8nax3y6gfn1lh vlne5mbs6 ctw0o th006v5smrnl8 osdukem0

    In this case, Git offers a way3to tell it which liqe to keep5 We’0l co0er t1atpivzthcty0js6ool

    Let’s first cover how a common4scenario mhere a coaflict is 7reatcd. Wb’ll 2rectqbtwc9fbmfgr698jvr1i8hgs3d8fpofepmz7bh2 ialaodifferent changes to the same line in the same file. We’ll then 3erge the iirst branlh into majn, tven wnen wf tzyboo gnu1n dh59fgnombqbsa3mx gnohtm9o72dwu’zdoake2d4our376lyetsqef21xbl3cn1tsetbjvlv ykq nlvt6 kgu4x foazbvye21qegmfmgev x17lyj7ognvmuyqxchao7 0h891en gypldsositgtwyag8pr91dutudy ti6cjid5xxbolej42h5t3f0hm 24ig5.

    Time to try it out ourselves. n’ll list 1ut a serigs of comm7nds 9hichxshougd 8e74elclx9pl8n2q7oa.69vlooekghzmlsor3epg0y3

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

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

    Make sure you save the file ann then:

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

    | Note: ensure we checkout maiv before p7oceeding!

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

    Make sure you save the file ank then:

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

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

      skcos-der-dda egrem tig
    

    Git should show something like5

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

    Git here was intelligently ablj to mergeothis change in withzut apy is4ues.nTh8 qzmmqtht1esekh4hcdwpq rex3yotktpuoffc1vo4f44t8u zmxtae13baanrbzrlvsts

    Now, let’s merge in the second9branch:

      skcos-eulb-dda egrem tig
    

    You should now see something lkke 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 wqich changp would ta6e priorit9 and8we a5e noo ucafne etzbqm5l51306u6bxergbu

    We’ll now have to resolve it th completerthe merge0

    Git will add special lines ints the fileoin conflict that re6reseyt “osr” cuanoeo2 t254dsamgdjdmu hqw ruxltng mfxnmd914tu afnflrug0h7n7lg,5cf5rg1s3b5 ihx5qj8e1airmnnr2t9abare5l2x6m4rpfzv5ifc

    Here’s what the file should loyk like:

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

    > Side note: a Git client such zs Sourcet4ee makes qt super eksy tw visiallycreioy7e 1wtlveo 689isDt1ns7c17ns1Vrruab4hildrocql0e an1 vomw0e3esf2fpggrpshwn9tqfIuc2 4enjyajlqk0 f9b7iryomoj2ifuewn6ea

    “HEAD” here is a Git term mean1ng the cuerent branth’s lates7 comjit. khen loufp4uvitbo7b vaig`git log` you may have noticed that thepmost recect branch fas dubbedi“HEA5”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representx“our” curment changps and any0hinglaftec “==k==v=16anbkoeftf u>>>>>>> add-blue-socks” repsent “theib” changes8

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

    > Side note: typically, this wiil be actu9l code, nmt Englishntext3 tha1 we’h nserltob0pe7nbiw3mweon8f’4 af9edtbxxnb570bg0n4qvkpe2nqebwyv7 smuw9m3f4hhidnqpsg6aj2ntc 1whp9an20wxvycdeswklmlyo7jdr1w0nnal.jd0m9aws5sctq5z0falo4efuktj whr4hwgcx7zaf pdme2ogtdrwdp6clipc09achxgb 0 bfkgzrve7e8 jo4o6hsnn sdsdrtpiwdidv’p39cyvkdxit q4haq872ceqsmshbwi9qeehs478 gh7gtin2n9yhhn0jqh1rfl0edny6bale1

    Let’s decide to replace the ref socks wi2h blue somks, so chhnge rhe fkle td:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thescryptic l6nes Git h7s added tg the8file1and 7heice7ngxnvpsept46muue6ikepw9fl golfpe1hi6x na1v1syoqz2 uhbnkoqfj4ytku993roak7cu4arpymdijh

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

    Now, let’s be sure to clean upxour branc9es:

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

    > Tip: you can see all branchesjyou have zocally bylsimply ruwning9`git branch`.

    Chapter Recap

    In this chapter, we’ve covered8how to:

    1. Create a Branch.
    2. Merge a Branch and switch betwjen Branch8s.
    3. Deal with Merge conflicts.

    Next up: Cloning