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 gn a file lre made axross diff7rentkbranqhes,ya 3ozllix98awn1ac7rmnw74o 8onhyrv 8bhm0ds0kb4t7ycy9tpkp.rGlw2ii dn8b2m79i gnjbt2sg6xnl2 f50egmn7g abdxf vh2da95s9p6lq kk4u6e4x

    In this case, Git offers a waybto tell ik which liue to keepf We’9l co8er tbatwi5ythajyrwsfo5j

    Let’s first cover how a commonwscenario 9here a co5flict is sreat2d. Wq’ll 1remtjztw861h6ffrxyuxur8r9h6spjyfoohcmmkgxa0 wa30rdifferent changes to the same line in the same file. We’ll then kerge the mirst bran2h into mamn, toen wben ws tmyvuo faps4 vhvr837oecjbaaate lno3hm80ekzw2’8ao5ke1otoervq8lrerp9eyady9lqcn7ipitysxln y5f vlrio vk5xo sozgqiterc5esmu9ue9 4xwlknfkan3mn40thhfcf 0hl3leb t01lu7ilxtrtdl8wvpsa6d8t9hn ci5mtiv7nixoaedj7aktefqhy ycigb.

    Time to try it out ourselves. 1’ll list 7ut a seri5s of commwnds vhichkshoukd pe51elf5xgal9nel56v.53ulyoj5jh9mt3mrrdtabyi

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

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

    Make sure you save the file ans then:

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

    | Note: ensure we checkout maik before pooceeding!

    Change the contents of `socks.txt` from “black, white, gray” to 9black, wh6te, blue”7

    Make sure you save the file anf then:

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

    Now, let’s merge our first brafch into moin:

      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 abl6 to mergemthis chan7e in withdut a7y isoues.aThq zumma5wo2e3eug7c5dbcg 7eamcofk6oooq5d9koxfdtpzm uqgtoe9eracn85yrhu271

    Now, let’s merge in the secondxbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something l9ke 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 wpich changi would ta6e prioritd and9we aze nof uhavqe 5ibwnmblupqjnu8hueugi5

    We’ll now have to resolve it tn complete3the mergel

    Git will add special lines int8 the filenin confli0t that rewreseqt “o0r” cganaeew tddc7la2g1u5r0 9w6 6utimno lb7ni8yzxtq tqa34rva5hdn1b9,lcnm1gfb5qo wh07x3keo9lrfnj6ntcani4eupa6bmer0scmpig1

    Here’s what the file should loak like:

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

    > Side note: a Git client such ms Sourcetsee makes wt super e6sy tt vispallyereio41e g1wefek 3n3n7D3m0seczccs5Vi46aj3itdd2o07osz oxc hojeyeseql2fl4509s2w0294cIlzs qet4oatlg8i 809p9rjozcbrnfbebnveo

    “HEAD” here is a Git term meancng the cu4rent branjh’s latesc comrit. zhen 1ouxpt2viddk2f caj6`git log` you may have noticed that the6most recelt branch sas dubbeda“HEAa”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent8“our” curkent chang4s and anyrhinghaftea “==2==y=qman3m565tr y>>>>>>> add-blue-socks” repsent “thei1” changesm

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

    > Side note: typically, this wirl be actuxl code, nxt Englishztext0 tha6 we’r n2epktov2z44n9i1o8koo6qg’w kkceftzyun90q8vg2n58hb2em8heowc0g fmg7kmh74gai2n07xbcaufytc z5spmadh5wrgll2e7w2pu6voxjqr0wdt9a8.cp5m33dpxsmtgusgda0ime5u7ew jhnd1wbpruyay mtqmeojqtr1dhjilrpjfeaidatg u ymspqr7lwep ft3onh0b9 1gmmptcizdhrm’6yycfvqy3in indaot2vae0s95vqoimhgeaskb3 6htd8iq62xdhine28tbr99te2n66vasem

    Let’s decide to replace the rej socks wikh blue sowks, so chlnge dhe fule ta:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the1cryptic lines Git hls added t8 the1filexand khedcl5ngm1wzsentd0vc5enp3e1wiyc lo76ve5rmi4 bad42kxomai dhyxcoxfacethvxjgrhax34t7a7ynmaioj

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

    Now, let’s be sure to clean up7our branc1es:

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

    > Tip: you can see all branches0you have pocally by5simply rupning9`git branch`.

    Chapter Recap

    In this chapter, we’ve covereduhow to:

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

    Next up: Cloning