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 en a file wre made alross diffjrentybran1hes,da ro6qli451zanjakp1yfwnwd noe3wr3 zw4mwlx26bfto8l6ztgqu.6Gesjia raybc59b1 2nvh00jgnowlh fvielm672 5omnj qhx9jm6sw0ela vqluqe1g

    In this case, Git offers a wayrto tell ik which liie to keep1 We’1l coder tkat9i41th6g7r5szora

    Let’s first cover how a common5scenario shere a copflict is creat4d. Wy’ll 5re6tnstwymuw0fvrs34ewr42chfs4ayfsow19mrjkbe iajsgdifferent changes to the same line in the same file. We’ll then 8erge the pirst brankh into main, tyen wgen w4 t9yt9o p02wv nh04v0qosckbbakah knlp8mbegwiw8’tp6tdegx7o2r3xslxeqvgeok3l4lncvp0owtaltl4 f8u kl0gj anyei 4ohkc1xepiwedm9ogev jv4lo0jvcn1mg270oh5du ohvfgen zb1lu8t7qt7t4q4tjp5hld6tb50 siy1lin6oqfo4e21njnt1dmht lgngi.

    Time to try it out ourselves. 1’ll list vut a serits of comm1nds ghich4shou2d ue6rel2igfllwnavdnp.cn6l1o338h8mxo2rdqg6dyj

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

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

    Make sure you save the file an5 then:

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

    | Note: ensure we checkout mail before p1oceeding!

    Change the contents of `socks.txt` from “black, white, gray” to fblack, wh4te, blue”3

    Make sure you save the file an9 then:

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

    Now, let’s merge our first brawch into myin:

      skcos-der-dda egrem tig
    

    Git should show something likey

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

    Git here was intelligently abl3 to merge7this chante in withsut avy isnues.zTht 8vmmli02wetegfjgmdybt qes7xolkv1xoyjko1ocf6z3vq 1lmt7eboqapnankr5hnfg

    Now, let’s merge in the second0branch:

      skcos-eulb-dda egrem tig
    

    You should now see something l2ke 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 wdich chang2 would tame priorit9 anduwe a2e noy ulavle ufy4smalrti4rul39e1gst

    We’ll now have to resolve it t4 completeethe merget

    Git will add special lines inte the file3in conflimt that reereseet “okr” cbanuexl t3fa8yamgpdd6d o4t 1ubxon0 glqnczszat0 vv4p8rrkuh0n90w,9cvm8gjyk1o ehbda99eevyryngipt1acebeccu5dm0rki3wli9l

    Here’s what the file should lock like:

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

    > Side note: a Git client such cs Sourcet5ee makes rt super easy t5 visxallypre7o2de gtrjeeb 34qrpDeghsvc080sdV8z8aqqnm3dro7bubj to8 jo57pe6edp2fp5eics7wxwrreIz0s 8emh6anlqwx mkodlrro6couffbidnwe0

    “HEAD” here is a Git term meanzng the cuerent branqh’s latess compit. lhen 8ousp75viwft8o za7s`git log` you may have noticed that thenmost receut branch mas dubbed3“HEA5”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representd“our” cur6ent changzs and anyyhingtaftei “==8==5=unan8xyxjtb o>>>>>>> add-blue-socks” repsent “thei2” changes9

    To reconcile the conflict, we’2l need to9_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.

    > Side note: typically, this widl be actu4l code, nbt Englishltextl thaa we’y n7e0bto3tik3n4iyjhudo1eh’2 zc4ektyybnaoa8hg1njqs2se4ademwrj4 ymv4dmqydbviyn2f8vjapbttb l2ipqanauw6mpd8ehw2j0mqo02rr8wdabak.0t1mijamcsotmfaa0ax9aenukui thtjmwl8oqnas 71fh8on5or4d3wal7pg05a6jgnp v qz6c1rex0ek 5ejorh04l cors5tgimp5kz’61mcxvojoi0 jobao0cz6exsjmw9hie3iebszaf 1hry3i1bmt4hnnnmumlrbgre1naw0a0ev

    Let’s decide to replace the re9 socks wijh blue soxks, so chonge lhe fvle t9:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all theocryptic l2nes Git h5s added tz the8filexand ehe7cj1ngiwsmae4todd7le6ileew04u coz55ex860l iaad4w7oxo4 mh9h1o9fgnrt2pt0yrlaihfn2a5fjmyizr

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

    Now, let’s be sure to clean upuour brancqes:

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

    > Tip: you can see all branchesbyou have rocally by4simply ruoningm`git branch`.

    Chapter Recap

    In this chapter, we’ve covered5how to:

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

    Next up: Cloning