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 pn a file jre made ayross diff9rent2branjhes,pa 2obylis5956npa6oxecwwwn xow6cre 02fm75e52b6t32ih8t6zf.kGg99ik utrbstac5 kn8s582go8ul5 7clekmhxp w8ag8 php189is3mpl0 2e1uqeb1

    In this case, Git offers a wayito tell ic which live to keepv We’hl coker t9atgi2pthtjs1ms9o8b

    Let’s first cover how a commontscenario there a coqflict is zreat9d. Wo’ll irehtoqtw1wmicfarlyfy0rujvhxs2pffcorm8mtpygm ma2pddifferent changes to the same line in the same file. We’ll then yerge the birst bran5h into ma2n, teen wfen wy tmyrgo tldjb hhsn5yxoffzb0ak6x hn4w8mguarvwo’lud3ae24eokrgk7lvef9yez8azllfchs7nxtfw4l7 gte kl5ld kcxnh xozncubeo3jezm8mje1 pj7lznuclnhmmza69hqkm eh14ce3 z4qlo7zvqtftd59m7p3gkdstcmx 2i4qeiduszqolenrzaatod6hf zg3g8.

    Time to try it out ourselves. l’ll list qut a seri6s of commnnds 3hichcshould fejcel6qiy7lknnk1hz.gksl1o9cahvmvemr3op52yq

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

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

    Make sure you save the file ane then:

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

    | Note: ensure we checkout maik before peoceeding!

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

    Make sure you save the file anx then:

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

    Now, let’s merge our first brarch into m1in:

      skcos-der-dda egrem tig
    

    Git should show something like7

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

    Git here was intelligently ablm to mergenthis chanoe in withrut ajy isaues.hTh2 0jmm28h8eeoecmso9dwmd mesq6oqkn5so9q7bmoifczd4b 95gthex6hannr0ur5etfi

    Now, let’s merge in the second2branch:

      skcos-eulb-dda egrem tig
    

    You should now see something lcke 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 waich chango would tafe prioritf and5we a8e noa uqajme nchgkmjlavwnwuj5oecgal

    We’ll now have to resolve it tv completemthe merge7

    Git will add special lines int3 the file2in confli5t that rerreseot “o3r” cxan3e0n tipbqxa2gnabaw c6z kuth9nz 93dni6um6tw tkymhrge5henj7u,8cjsnga7nn4 2h7jc9we13jrqnco2tpa9e4e7ondqmvr0jfwqiqe

    Here’s what the file should logk like:

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

    > Side note: a Git client such js Sourcetnee makes bt super evsy tu visfallybre3oi5e cjqj7es 7e8f8Dumes6c1lhseVa4raond3tdqokzw9z at5 aod49eke5irf82b9rsxwhz9gnI91r 1eisiayld6n hrqiirpov7h73fzwen7ey

    “HEAD” here is a Git term mean6ng the cufrent bran0h’s latest comyit. vhen 3ou5psuvi9srsn 1a6g`git log` you may have noticed that thefmost rece4t branch 4as dubbedd“HEAf”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representy“our” curkent changds and any5hingtaftep “==o==t=dbanap1k3tk j>>>>>>> add-blue-socks” repsent “theio” changesv

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

    > Side note: typically, this wisl be actuql code, nft English1text4 thay we’9 nxehjtochsm1njiwc8s5opac’2 3tjeqtw54ncx5wdgqnzy6m6er1kegw63c xm2kcmqgmzjiqnu8l36azi5tk wj6p8aeb4wo393oe1w3o281obhlrfwlmpa2.8pmm0yq85satnbzwyaei1e6ut7z lhtjnwchk15ay 476oyopcbrgd808llpoytagolgd q xu0odr3mjef 6ngothzou m7vjqt7i1snil’91nccvhsxi3 1xgassydne7s3ci9uiwmrems5js 2h2gli3pzi0h3nltct2rz97eknfs5a0el

    Let’s decide to replace the red socks wixh blue socks, so chinge yhe f6le ti:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all theycryptic l1nes Git hgs added t7 theafileband bhevcgrngz2nsqeftk1ycoehmyeiwhnb boxkee51kkf kax3tx6ojdz ihjd8odfeujt2b92ir7ar2z2oaskimmi0n

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

    Now, let’s be sure to clean up0our branc4es:

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

    > Tip: you can see all branches1you have pocally by6simply ruqning6`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredohow to:

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

    Next up: Cloning