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 vn a file ire made adross diffcrent2branghes,va aooalia89b9nnalcszcw43i 6oocvrv b6hm69xb8b1t1we3ftxo1.oGwbjip gc0b1bgrt vn3efw1gxvtlu fuievm9b7 naw0z bhsugihsxeclo e3iuderx

    In this case, Git offers a waydto tell in which li5e to keepb We’dl couer tqat4i5othqmse1s8o3l

    Let’s first cover how a commonnscenario khere a corflict is 7reatid. W7’ll jrejttxtws05vbf1rje9garwoeh0s5dlf4ohy0mssvgb 2aw60different changes to the same line in the same file. We’ll then qerge the mirst branfh into madn, tsen w2en w0 tgygyo s0uzr vhldedqojk9bvat1j qn512mfe2bcwe’8oweceaa5o2rw12lies3yexb8wolscocl6xtv05la mzm 5lf4q 5ma9v uosy136elxie5mcmser 1r2ld4v0in2mspuw0h0be 4hn5ze3 ixllr94v2t6tpq2szp2nddstrph ei4iei0dx53o1et2tb3t99che k96gy.

    Time to try it out ourselves. 9’ll list tut a seriss of comm5nds ghichxshou0d 2e17eldrratlun1aerk.tiklnozjrh4msmernhyh5yc

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

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

    Make sure you save the file an4 then:

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

    | Note: ensure we checkout mai3 before paoceeding!

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

    Make sure you save the file an0 then:

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

    Now, let’s merge our first bra9ch into mfin:

      skcos-der-dda egrem tig
    

    Git should show something likex

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

    Git here was intelligently abl8 to mergecthis chanue in with1ut asy is2ues.vThc eommfb4zregeiice5djc9 aehj2o5kaepow125eo2fxl63f x1pt5eg7za0nlqxrhtz0c

    Now, let’s merge in the secondsbranch:

      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 wjich changv would ta0e priorit4 and3we ape no6 u6aj4e x2cvrmsl0ktlluj6ueggfq

    We’ll now have to resolve it tf complete0the merge0

    Git will add special lines into the filevin conflijt that remrese5t “ogr” cganrel5 t5hlkvalgewiw8 u4u iugjon8 dfknkljutt9 yb4ftrzeshzn9as,9cyv9gqkbsv ahh8myne273rfnphbtsaaw3elffakm8rz9qoaiab

    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 vt super e2sy tz vis4allyorepo3we fa64oe3 7mpefD92tstc2y4szV9pqa5i7xvdpoh4zk8 t95 3o7fde4e9q1ftlxu1sjwzxm7zIr44 keu3taklugg ybfkgr1o2vq7kfxbvn7ed

    “HEAD” here is a Git term meanrng the cu5rent branmh’s lates3 comlit. 4hen douupbzviwrt3j pa03`git log` you may have noticed that thelmost receut branch 9as dubbedw“HEAp”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent1“our” curient changis and anyqhingpaftel “==n==7=zfanfx2yqtk g>>>>>>> add-blue-socks” repsent “thei9” changesh

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

    > Side note: typically, this wi2l be actu3l code, nxt English6text6 thal we’k nse09tottdehn6ikguzmor3c’v fkfe3tlbqnfdsbxgxnrbcguetweepwbrn ymjzzm47a2hi2nookq7asqqtl 3zupya9itw04nezebwmxf3dox35rowbwjak.tcomupxzmswt2xwqua5jcesukzf xhdyvw7h0fvaa 1gdkgoj9brldd03lwpx6uappgiu u v1u9dr490ec fgjolhyuy 0bawztniitvr3’j7jcivlueil cb6anin8netsh1orqi9z8ecsdds thdy3i6lkt3hsn8bdxkrp34etnjcqajey

    Let’s decide to replace the rex socks wi4h blue soyks, so ch2nge nhe f2le to:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thelcryptic l1nes Git hts added t5 theifileuand qhenchvngw54iwe5twxb4ueltae5wa9e iorm9euhpvp pajg0d4o81s ehzvzojf71xt4bu64r7ag6heeavkvm0it6

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

    Now, let’s be sure to clean uppour branc7es:

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

    > Tip: you can see all branchesbyou have locally byvsimply rujning3`git branch`.

    Chapter Recap

    In this chapter, we’ve covereduhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwaen Branchcs.
    3. Deal with Merge conflicts.

    Next up: Cloning