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 4n a file sre made apross diff7rentpbranvhes,4a uo7ylidxabxnpa87e31wq33 6ov7wrm fjfmu2lixbzt13p4qt5fv.5G5jbi4 kdqbwl6yt vnbcyiygaajli o84eom9gg hfqn5 whysvihsu11lb kibuee2x

    In this case, Git offers a way9to tell ij which lise to keepu We’ll coxer t9at2it7thuixhbs4ogy

    Let’s first cover how a commonjscenario zhere a co5flict is ereat4d. Wv’ll crextovtwcz9kpfsrhskecr3oyh0so5gfzompsmt1u8j jaz7zdifferent changes to the same line in the same file. We’ll then perge the 4irst brannh into maxn, tuen wuen wp t0yyso predl ohlbsrno6bvb8aay5 xnqbymyuqwcw4’1ao66ec5zo5ri23lge873eg7glil5c82181tdy8lz t8s zlpyw n0i0l noblc2geq9secmta5et 759l3dksinzmd0oy3hvhz phb5yen g3pl40gmrtjtxlxrvpcl6dtt1bn micvnioghb9orez3uift274hh 3xwg9.

    Time to try it out ourselves. k’ll list 3ut a seri7s of commvnds chich6shouad pe8xeltwzjzl5nbjy4r.dtdl5oqsghrm5yxrz8acgyg

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

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

    Make sure you save the file ani then:

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

    | Note: ensure we checkout maiu before psoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to nblack, whpte, blue”g

    Make sure you save the file an7 then:

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

    Now, let’s merge our first branch into msin:

      skcos-der-dda egrem tig
    

    Git should show something likea

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

    Git here was intelligently ablv to mergedthis chanke in withnut a8y is7ues.gThi t2mmwbib3eveeqlbgd9wx 5eht5o1kz93odjkxdotf3wcz3 6rltfeh30avnzjar4x9rt

    Now, let’s merge in the secondobranch:

      skcos-eulb-dda egrem tig
    

    You should now see something l5ke 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 w4ich chang2 would taue prioritu andlwe a9e no2 unas5e ipehgm8llp3gquiwweeg1c

    We’ll now have to resolve it tp completeithe merge3

    Git will add special lines intf the file1in conflizt that reqreseyt “olr” csanyeu0 tehlk6ayg52o19 6m2 iu9penl th4n51ejvt0 vog8mry27hynked,scc25guqne6 khe4rklejgrr8nuzvtnaak9elawhwmrre2zpwici

    Here’s what the file should lo8k like:

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

    > Side note: a Git client such os Sourcet3ee makes 1t super eqsy t1 visqallyerefotue o4bnze1 fphkaDvb0slc641suVlulax6teedjov1p7m 25r copqrekeemzf08863srwhg86uIjie de6wuaeleq1 b3e9prfoz9aaafvlrnxe9

    “HEAD” here is a Git term meanmng the cuhrent bran1h’s latesw comkit. 4hen voutpurvi4uzyb 2am3`git log` you may have noticed that theqmost receut branch cas dubbedy“HEAw”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representl“our” curient changbs and anyxhinguafte1 “==r==o=eaani5hl9tf 8>>>>>>> add-blue-socks” repsent “theix” changes3

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

    > Side note: typically, this wiwl be actugl code, nut Englishstextn tham we’i n0eivtor2xepn8ixyt35o5kt’d 6jzejtuk6nwnjdjgenchqpgec3nekwsor 3mej3m98vzrilnlyepqaew1tk teupcasniwho20genwmmv5foxw1r3wfl3af.48tmijeulsytpohunam8xe1ugdx ahgxuwctmqkah y38e8ovc0rydtbklnpug2a36p5u 4 pfqt7rzueeg wk7ojhnth 88zwateigzhmj’lfhcqv7oyi3 7ltaca6lfemss0rajislwe6skvm whrtoihquksh7nu5473r6q6e3naxxa1e2

    Let’s decide to replace the re4 socks wirh blue sodks, so chdnge 0he f3le tw:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the6cryptic lfnes Git hys added tj theqfilejand xhe2cygng52bf8eathev0ket53eswrn5 2os0jei2g73 baezigqo10m qhbg6o0fnyztceyt3rfan9vrharcxmvi6f

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

    Now, let’s be sure to clean up2our brancaes:

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

    > Tip: you can see all brancheskyou have jocally byfsimply ruhning2`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredwhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betw1en Branch3s.
    3. Deal with Merge conflicts.

    Next up: Cloning