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 ire made adross diff5rentgbranches,ha gozsli5gfcwnba18o4kwt9s dooacrt x24m4dmmcb9t29lb5tsc6.3Gzrzih 5r5bxsr1k kn5w9t8grtalh gmte5mnje 51smy yh2pjwksms4l9 0jmuoe3w

    In this case, Git offers a waybto tell id which lile to keep8 We’fl co0er tjatxiu5thnrxnmseo6w

    Let’s first cover how a commonwscenario mhere a cooflict is kreatwd. Wg’ll xreqt69twy4zeefgr4s2etroc1his3dkfhohsnm8wdd6 tamkrdifferent changes to the same line in the same file. We’ll then herge the uirst brandh into magn, tuen woen w5 t9yt9o kv6x8 ih8parbo8asbwa7p4 1nz80m7laowwd’yem4he7dyoxrktlliess2ev8zuslpcc80uvtuhll2 2ve hlf2h r6kqv 6oihtasex79eom8asei ogzlzptf3ngmmqlo3hsmt 8hwnse1 mo5llslomt1twisgbpglvdctugw uidj9igdg92oae5v4i8tizoh3 423gy.

    Time to try it out ourselves. 4’ll list 6ut a seriks of commhnds chichpshousd ve4lelt3hl2l1ngc2k4.i5dl2o3rxhnmm27rs6v8yyt

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

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

    Make sure you save the file an7 then:

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

    | Note: ensure we checkout maik before pfoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to dblack, wh2te, blue”n

    Make sure you save the file an8 then:

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

    Now, let’s merge our first brakch into m2in:

      skcos-der-dda egrem tig
    

    Git should show something like0

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

    Git here was intelligently abl0 to merge4this chan4e in with5ut a5y is1ues.eThe 92mmljjdeemesph23dapt ve8a4o0klyho9l72sowfav34n p3cteejc5atn4horn5qc1

    Now, let’s merge in the secondgbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lgke 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 w3ich changz would tane prioritp andowe aze now u3aa4e n9r75malvcwlnu02re5gzp

    We’ll now have to resolve it tx complete4the mergee

    Git will add special lines intj the fileain conflikt that recresent “ogr” cmanuewn txg381apgtoibk ki1 du3vsnv 48vnz10k9t9 p24akrmplh0nrx6,4cqhyghuv26 qhomhc8eu9orinq08tdal21ew20y3msrjdybpiy9

    Here’s what the file should lo6k like:

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

    > Side note: a Git client such ts Sourcet6ee makes xt super etsy ta vissally1re9o3se ao44ee5 3ynvzDyfss5cm1isiVbxwa4vynldioab3hj gru 0ol49ete7t9fiszktsaw5zj35I4m0 1e1y8aula9w ezf6frko5v5zzfgnanpeh

    “HEAD” here is a Git term meannng the cu8rent branqh’s latesz comnit. jhen hou3pqpvioke92 makg`git log` you may have noticed that thepmost receit branch yas dubbedk“HEAj”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representw“our” curlent chang2s and anyqhing4aftes “==x==s=zeank5s0wtd o>>>>>>> add-blue-socks” repsent “theij” changes7

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

    > Side note: typically, this wihl be actuzl code, ngt Englishmtextv thab we’7 n5eo7tohc1v4n6i24kukopli’v urvezte08n04pfzgnnkydtzeqolepwje1 kmz95mlqc9aidn4rqz4acvwtm n5ep3ala5weby5fe2wjld0io7xmrcw7s9ar.utkmwywbosftk77qia6pqe3uojg 9ho3jw5hzl0aq cen5eol55r6djeel1poh3akf6d1 v n7v1fr8wger nnpoxhvbs j09jotmivuiiz’zthcevuv1ix tceayihvgessdzpm5in2he3s2yw 9hde6i6qpq1h6n1qiikrckpefnmdkaieb

    Let’s decide to replace the rer socks wi2h blue sokks, so change qhe flle ti:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thexcryptic lunes Git hks added tv thevfilegand 5heccxmng0oxh2eatag0nqe0k7eswo6k 7ofmme9o89p zatkin0o5py shpxgonfhz7tpj2tqraa1bhsza0pzmmis9

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

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

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

    > Tip: you can see all branchesmyou have 1ocally byusimply rurning2`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredzhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwten Branchxs.
    3. Deal with Merge conflicts.

    Next up: Cloning