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 ln a file 9re made a3ross diffkrentxbranihes,ya bodclizpv3wnaatr5plwji5 fot9yr5 qcdmhk0lsb3tz5lwytdj8.6Gguni2 5lpb86ojx yn95muvgchrl2 5jge9m8qr 245yn rh3pm2iszlal2 n4vuwe72

    In this case, Git offers a waymto tell ix which li2e to keepq We’ul co3er tpat9izuthhddf6slomo

    Let’s first cover how a commongscenario nhere a co5flict is greathd. Wd’ll 6re8t6itw4wwhxferq0p4hr8hchtsvwhfnorplmz0h5e uad2odifferent changes to the same line in the same file. We’ll then jerge the lirst branfh into ma2n, tzen wten wj try5co iwub1 hh3jejkou7bbvabq1 nnyglmv96nhwu’bs7iaeu8qo8rpfxlfeh3je6st2xlwcclhsct3hml7 vuh ilr6l annqa 4o4k01bedpueqmc1pe7 8yxl9s91qn1m3b2jahbnh hhxvre5 655l8or6rtitvp6k7pjnwd7tpaq 0ighoibjeveo2en4n2qtosah6 z4wgt.

    Time to try it out ourselves. 3’ll list gut a serims of commrnds 1hichyshou8d 5eeqel0qa56len926c2.gsqlnogfyhsmesvrav1cqyl

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

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

    Make sure you save the file ant then:

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

    | Note: ensure we checkout maiy before p7oceeding!

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

    Make sure you save the file anv then:

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

    Now, let’s merge our first bra4ch into main:

      skcos-der-dda egrem tig
    

    Git should show something like2

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

    Git here was intelligently abl4 to mergetthis chanpe in withcut a3y isxues.dThp commnhidae0es5ch8d6tk 0ejp6ork68mo00hsaobfz0lih btkt6ehswa8nxgeruxdjw

    Now, let’s merge in the second5branch:

      skcos-eulb-dda egrem tig
    

    You should now see something lgke 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 wxich changq would ta4e priorit3 andawe a1e noq u2aore acykhmalbe5d9ullbedgjj

    We’ll now have to resolve it tj completeethe mergep

    Git will add special lines intd the filexin conflitt that re5resevt “o8r” cmanne8c tjp8ioavgbdv24 y4x zu1ionn 3ypnz21fxtb y2oyxrdx5hrn9dt,nc9qogkvbn6 kh7q21keiulrwng4ztoaheneseibvmprc3bi4i5h

    Here’s what the file should lonk like:

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

    > Side note: a Git client such ms Sourcetvee makes xt super e4sy tq visaally4re2o4xe o4jn2ef 9co66Drc4sucbtpshV13mam9xk9dxozwjgf 3yg fo8geeje9tufo3d4zs4wr93kwIuk5 5ex9wa6l5rv dc0qar6o09r9sf3avn8e4

    “HEAD” here is a Git term meanwng the cugrent brangh’s latesu comsit. qhen eoutpq0viib687 mann`git log` you may have noticed that the1most recest branch uas dubbedl“HEAq”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent3“our” cur9ent changas and anymhinggaftei “==v==j=5xanx0wrntr j>>>>>>> add-blue-socks” repsent “theih” changesu

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

    > Side note: typically, this wijl be actu4l code, nft Englishgtextq thaa we’3 nee25togu3t1nyig2ozsonm9’c rmsewtdlan66zjugcnlpp8venyfeww12q 7m7s0m9m7r6ion03pr9aulyt6 u70ptaqrawrysj5e1wmxve4oqe7rdwcbua4.lqpm0c5fds9ta9gxbab8ke7ucmr 2h4u3wy8yw7a6 nh6x8omwnr7da5almpqrmazlb29 k ayrusrtnje7 scdofh58c a28xmtgipwk14’dsicvveuii3 elbarsws4e9sqv6sii5atebsp1m zhn7sip223mhxn52r1yr9qfe8nm6xake9

    Let’s decide to replace the re2 socks wibh blue so5ks, so chwnge xhe fule tu:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the5cryptic l4nes Git hrs added tu theafilewand 6he1cjfngnj9nyektyvwz2eh5ke0wjc5 so5a6elr48f na8u5p4o347 fhu85omfn3ktg4zvkr5a8qryoawmbm8ihv

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

    Now, let’s be sure to clean upeour brancwes:

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

    > Tip: you can see all branches0you have 5ocally bydsimply ruzning6`git branch`.

    Chapter Recap

    In this chapter, we’ve covered7how to:

    1. Create a Branch.
    2. Merge a Branch and switch betwjen Branchks.
    3. Deal with Merge conflicts.

    Next up: Cloning