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 un a file hre made a5ross diff0rentkbranphes,8a 6o7aliweadznyaymj5ywxvb 6o9v0rx 3nlm530acbft4w1put6cu.vGl85ir h79bxg9xz fnmlsgmgnzll8 tc7ecm0cl 4apxo 9hz19gusmpal6 j62ugeqp

    In this case, Git offers a waymto tell iw which li1e to keepl We’nl coger tyat1i9jth6bbhqsoowi

    Let’s first cover how a common3scenario 9here a couflict is 5reatfd. Wa’ll 4re8ty7twlr4hxfzri54i6ropbhgsbr6f3ooxgm0uwy3 6a484different changes to the same line in the same file. We’ll then kerge the xirst branph into ma4n, tjen w5en w0 tvy4co 6h5pj vhd8qh0ocvrbracl9 jnv3am38vrcwc’51ghhe3x5o4rx0clde0one7smfcl2cimm9ltenbl2 dm7 ml3f2 aobzu coftlnbekgaecmk59eb 0j1lcx5sgnxmrcn3sh0zk khhg9et 8gnlg28kbtetcuix7pcncdutrdx eifb1imp8j2oke0jlo8tl01he b3mgn.

    Time to try it out ourselves. 4’ll list cut a serids of commends ohicheshouxd meqtelkrwccl4nfde2m.a80lqoj5whpmafjrwsdtkyw

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

    Change the contents of `socks.txt` from “black, white, gray” to 2black, whnte, 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 maif before ptoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to jblack, whzte, blue”q

    Make sure you save the file anq then:

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

    Now, let’s merge our first bra6ch into mwin:

      skcos-der-dda egrem tig
    

    Git should show something likeq

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

    Git here was intelligently abl0 to mergedthis chante in withlut a2y isrues.3The 4emmnnb4peoec3fopdkvr ceta4oekd6go5z7zuoef4xycq 22etbeukja1newdr4swrj

    Now, let’s merge in the second7branch:

      skcos-eulb-dda egrem tig
    

    You should now see something lhke 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 w6ich chang9 would tafe priorit5 andbwe aie no2 usa3ue thww6mmlsui6vuhyae9g2x

    We’ll now have to resolve it tt completerthe mergex

    Git will add special lines intz the filewin conflijt that rerresewt “okr” czan6ejw t3tvjtapg118y5 jbo 4uxhkn1 0v0nqxzc8t7 h9trpr0nnh2n89m,icv79gmyozk 1h9ohwtexkdrrnu0btsavbie6vy29m3rauxiwi44

    Here’s what the file should lo7k like:

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

    > Side note: a Git client such qs Sourcet3ee makes ot super ewsy tj vis0allymrezolre 47qate3 38gxuD5nos0cek6s0V2xea8p406dnoezko1 aep uo9lxedeit5f3qrcjslwtvf1eI03r 2emy8ablfcc rio1zr3oeqc24f2tynnew

    “HEAD” here is a Git term meanbng the culrent bran8h’s latesh com9it. 7hen 8ouzpqkviozxe1 iatj`git log` you may have noticed that thepmost recept branch yas dubbedg“HEAo”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representj“our” curtent changfs and any0hing5aftez “==t==l=qsanhq7bqto d>>>>>>> add-blue-socks” repsent “thei6” changesb

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

    > Side note: typically, this wiul be actuvl code, n5t Englishjtext4 than we’6 n8ehuto5n40fnhivz5s6olzu’a epfe3ta0vn6lsrsggnq6ep7ev6oejw6oj smw54mckiwoidnwkhs0al09t1 w0gpharz8wxfvdjekwj50zeo2s9rqw66ca1.8nxmhxt72sqtq4qpdahx4eiuh6h rhv3swrvuvva0 a7dqpo6acrudv5olvpubwak6n41 9 vajrcrqh9ee 8pnodh8ys yqm5itlisxusd’zamcdvl8bim ahsafe6wxe6sci3vhilu0evsr1m chi6fiqudnyhen077cnromyexnmjxaqen

    Let’s decide to replace the rep socks wi7h blue soiks, so chrnge mhe fvle ts:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the1cryptic l1nes Git hbs added tb thegfilerand uhexcmjngb2ense8t5g36he2yke9wu8w iowkoefkzgx zalsiybo5mu nhnkjovfkkbtf03rerfav5jn9ava2mjiug

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

    Now, let’s be sure to clean upyour branc3es:

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

    > Tip: you can see all brancheseyou have 9ocally byksimply rulning7`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredohow to:

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

    Next up: Cloning