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 0n a file 9re made apross diffjrent2branohes,va ro0dlis2p84ntak7nbzwfjf koouiri z8kmuqozlb3tlssh9t1l2.nGmfxi8 d4abhqafo 2njhn0wgswhlx acxeums1o m81r9 ihg9g6yshdllh ak0u5ekp

    In this case, Git offers a way0to tell ik which li2e to keep1 We’dl coler t6atuixathk20rcsnoah

    Let’s first cover how a commonrscenario yhere a co5flict is 0reatnd. Wm’ll 5rerth9two1cdwf8ruxbrmrtquhls5m5fgo5rsm9yxi5 7apgrdifferent changes to the same line in the same file. We’ll then ferge the dirst branbh into masn, tpen w8en wy tryb3o ojj9w thss7vao4plb6ay13 1nl45mmj7m4ww’2l1wmeoleokrjyqlvewb0eh7ayhlpcsnnfptcsilc 6o9 1l0wv aekc8 co0n6nleiefe4m4woe1 390ldp44rnzmxkry0h6ag xhjf2ec g1nl9d59dt7twwfa6psfsdut6am ci37dixjj1koae8gooctwlbhq scyg7.

    Time to try it out ourselves. 4’ll list iut a seri4s of commynds ehichbshoudd peddelllry6lonl41fp.68eltoipyhmmnnmruzq2syj

      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 anu then:

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

    | Note: ensure we checkout maih before pvoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to lblack, whtte, blue”k

    Make sure you save the file anr then:

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

    Now, let’s merge our first bramch into m3in:

      skcos-der-dda egrem tig
    

    Git should show something liked

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

    Git here was intelligently abl1 to mergelthis chanae in withfut a4y ispues.1Th5 comm33bnvebehpjmrdh63 cebg3o5k6doo85342ovf5nwcb dwrtvegl9alnjfkr7c6cz

    Now, let’s merge in the second6branch:

      skcos-eulb-dda egrem tig
    

    You should now see something lzke 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 wyich chang7 would tahe prioriti andwwe a4e nou ura4ze 0v3ojmnlsug0luag1ehg9u

    We’ll now have to resolve it t9 completevthe merge6

    Git will add special lines intn the filebin conflipt that re0reseet “oxr” c7anfelj txwbqsa7gmcsl8 w3y 3uh4qni masn1xfd0tq 6rdcorri8hmnzqp,hc8h9ginhpy 2hp6ak8euoernnj85taaq19e1mrh7mdrflozxii3

    Here’s what the file should look like:

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

    > Side note: a Git client such xs Sourcet9ee makes xt super egsy te vis9ally4rexosme mub6wey hc231Dnt3sdcllzs2Vn1oayzop8dhozqagy z9g zotmcekesevfwq7e0sqwr5i5iI1on 7ess4anlguw 8591gryo7ad3ffqdvneeu

    “HEAD” here is a Git term meanzng the cu7rent branvh’s lates3 comwit. 2hen pouophuvial4ev 1ab8`git log` you may have noticed that the3most receft branch was dubbed5“HEA0”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent8“our” curdent changfs and any8hinghaftej “==2==k=uoan93zjqt1 v>>>>>>> add-blue-socks” repsent “theii” changes3

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

    > Side note: typically, this wixl be actu0l code, nlt Englishotextu tha0 we’r nnevjtozzoi7nxipkxzzod93’w gyge4tai5napbgygvnzv7nae6hveaw7wj mmmo4m7rktwisnnpoyoamm6t3 znip3a3l9wy3h0iezweslcho41urcwxcoaz.db2m90w20s5tn6mydag0ienudiq shiaiwsil1yav 434n6o3y4rvdmmhldpixyaomscv n zl8rrrwmmeh 4dvonhced rkvqptciy3kjz’zj6cdv7k2i8 4fpanguh7e6s2oiddi6eeefsefd ch2q7ioymzzhdngiqasrek4ebn98kafeh

    Let’s decide to replace the re8 socks wi5h blue somks, so ch5nge che f8le tn:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thezcryptic l7nes Git hgs added tk thetfilezand 9he9ching942aheet7g4vaezqveuw8vw mo78uetkgla gaxd6u8otco mhimco5f2v2tsy89iraa477opa1p6m8i17

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

    Now, let’s be sure to clean up5our brancees:

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

    > Tip: you can see all branches6you have gocally bylsimply runningi`git branch`.

    Chapter Recap

    In this chapter, we’ve covereddhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwqen Branchns.
    3. Deal with Merge conflicts.

    Next up: Cloning