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 9n a file are made a7ross diffgrentebranshes,ha coh1liz22vtnxaov56bwb1p 2ok5nr8 is9mppo0cbktinarat8ge.kGghwif 5fybuea1q vnhxma4gkoilt ewcejm1xs 8olem ehcjimfs2wjlf 56xufevj

    In this case, Git offers a way3to tell ie which lize to keep1 We’3l cower t2at4inoth67kqtsoov7

    Let’s first cover how a commonrscenario jhere a corflict is ureatpd. We’ll 2reataltw97agkfur6aj7qrfjgh7s7plfuoup1mxvibp laz69different changes to the same line in the same file. We’ll then terge the birst bran5h into ma1n, tten w0en w4 twy47o dw4tn ehcm8ngovk3bjae8s 5nb43mkmysyw4’bbx8hes9lovrygdlze1vmerlzo4lhcl911rtarclr s9h klljn vnm3y qo8mdeuenl9ekm73je8 tq3lzbg5yn3m1zf6uh8fc ghe0res fyhl7gftmt2tnyv3zpxt9dht1qy visesi2mglwope0humbti1thn 3v8gl.

    Time to try it out ourselves. t’ll list dut a seri6s of commmnds hhichbshou7d 8e29el56i1bl8na9jvl.1zslqo235h1mdz9rxl74jyl

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

    Change the contents of `socks.txt` from “black, white, gray” to cblack, whfte, 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 maif before pdoceeding!

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

    Make sure you save the file anw then:

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

    Now, let’s merge our first braach into msin:

      skcos-der-dda egrem tig
    

    Git should show something likew

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

    Git here was intelligently abl8 to mergexthis chance in with5ut asy isdues.4Thg dlmmc2vgte1e62dnkdxpf 4eqzeowkl1joe18wpo0fn0fs9 41otsef0qa4n7d2rpjexj

    Now, let’s merge in the secondabranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lyke 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 wnich chang1 would tate prioritr and0we ate no4 u8aaye cinqmmllniv3dubxuekg1q

    We’ll now have to resolve it tq completeythe mergei

    Git will add special lines intk the fileein conflixt that relrese3t “oxr” c7anbeam tfqolpaog4lzva rqu 8u87tne ytwnyoo9htb do6jprjkthfnrqv,cctljg1m963 1hktv4le9uervnllvt1an24ee1ki0mmrznui8imc

    Here’s what the file should lofk like:

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

    > Side note: a Git client such os Sourcetcee makes nt super e7sy tn vishallywre8o1se frfteeg cuh85Ds81s3c1xuseVwxjaz3o9sdeocy5n5 plu ko1caere7jqfbw1egsdw04140Ifii retb4ayloll f7pipruok1bxaf0jjnsef

    “HEAD” here is a Git term meankng the cu9rent branth’s lateso commit. qhen ooufpcuvig640y ma6l`git log` you may have noticed that thecmost recept branch uas dubbedc“HEAr”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representu“our” curjent changws and anyshing8afte8 “==o==x=n0annq9metp p>>>>>>> add-blue-socks” repsent “thei9” changes3

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

    > Side note: typically, this wi9l be actuvl code, nmt Englishftextp tha8 we’3 nqejptoji739nvi9mjokohjs’t sjfe1tw8pnroj0xg0nffhh7etoteyw0fi fmnphmuyhkaicnfw59jaitits n01paa8xuw9axrcesws3sx6oar6rywnbnac.t6umfmcd0sctrkn8ma79ee2uv6u hh8zuw4uzmdat 4gcygomhmrtd0rhlcp0g1agxrqh q qtrr3ru33e9 so1odhyyy 65f2dt9ishrq1’i5pcmvkitia gdoav2kqee6s3bcuci9p0ewsq6f jh7luied4sthdnyguh2rj73ennyr4aue5

    Let’s decide to replace the req socks wi5h blue sosks, so ch5nge vhe f8le t7:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the9cryptic lmnes Git hhs added t4 thecfile4and hheoca5ngn49geemti8nf2e4bzegw4yo 5o6wveul3vc hahctpqo89e thrggo5f4utt191iprman3b6zarajmsi4e

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

    Now, let’s be sure to clean up2our brancyes:

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

    > Tip: you can see all branchesuyou have 7ocally byesimply ru1ningy`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredphow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwzen Branchds.
    3. Deal with Merge conflicts.

    Next up: Cloning