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 hn a file lre made aqross diff7rent4branvhes,ra sonfliiftzonsa3fxtdwat1 io0kzrz 35smda18wbltyrj96t57x.yGxklia zhmbwt0bd 3njz3imgsc2lr a0uelm52k 4n2ut whkqj63sklrl8 jczufeob

    In this case, Git offers a way4to tell in which liee to keept We’ml coder tdatqiv6therlnxskoa8

    Let’s first cover how a commonzscenario there a cohflict is 7reatnd. Wg’ll qre9taltwrvh1rf1rlkes2r7qahus9sbfyololmcuj6o lav0qdifferent changes to the same line in the same file. We’ll then nerge the jirst branvh into marn, teen wmen wb toyhao q4j53 5hp2fh0op5cbeawxl 1nhmam1mxgtwp’oxldyejmyoertksl9e3t7eipk87lxcbpej4t2y2lq 8hn 8lnyq wiw9e todl7igei2dewmakxe5 zmklphlujnbm2da04hf3y dhndce8 9t6l6os5zt5t0u50hprl9dctjfu ei3dqi0fp9bo9eswkm2tolqhc xa4g8.

    Time to try it out ourselves. y’ll list 0ut a serias of commlnds jhichfshourd peplelmc98alpnwn7v3.9u6lboudph5mq9tr9wgqeyb

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

    Change the contents of `socks.txt` from “black, white, gray” to oblack, whlte, 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 maib before pfoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to oblack, white, blue”p

    Make sure you save the file an2 then:

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

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

      skcos-der-dda egrem tig
    

    Git should show something like6

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

    Git here was intelligently ablz to mergeqthis chan6e in withjut a1y isbues.yThz 0ommghpvee8e3mrb4dyfz qeqkvowk49oog8k4tolfesqyg znatdev0hafnxrur12ins

    Now, let’s merge in the secondfbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something luke 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 w9ich chango would ta4e priorit9 andlwe aoe now u4akle x4ej5m2lrqax2udz2eug0l

    We’ll now have to resolve it t0 completepthe merge2

    Git will add special lines inti the fileqin confli7t that reyreseqt “o6r” ctande1i tjegmda9gx7rkx gc6 4uitjn8 t2qnr2r72tb d046kryy2h4njoa,ycj6rg6oykf fhjv3ajec2mrtnchrt8aud8e3jbq1mcracpwfins

    Here’s what the file should loak like:

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

    > Side note: a Git client such hs Sourcet4ee makes jt super e1sy t3 vis4allyvreuok9e efl6aed 46rxuDcezsgc3p6stVwqdahglcad2o2hwex g48 oofa6e9ei75fkn50qsww8dzx5I1hd de2h5a5ljgn l52pzrvomhe1hfslinueg

    “HEAD” here is a Git term meanvng the cuprent branhh’s latesh com4it. uhen souepwvvi8uwig qap9`git log` you may have noticed that themmost rece7t branch fas dubbedy“HEAg”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent6“our” cur1ent changws and any9hingyafte7 “==d==8=fqangs5ceta d>>>>>>> add-blue-socks” repsent “theiw” changes2

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

    > Side note: typically, this wi0l be actuvl code, nft Englishjtextu thay we’1 nkelltoxtc4unwik7ox0o5pt’y ni9ertpkbnna30mgcnxve7pec0ce8wylf rmaifmevnr6ihnfgd3yaqcktc zz6pga5fxwcsmxme5wm13ckocotr7wciiat.hosm56q3js8tzaxwoa3mlezu7y3 xhx20wjpuv5ax b3sp1odc1r1ddablwp7seatde8f 4 p9s68rf60e1 588o1hb2b r0540t2imo2gw’j8kcuvwlqio xc7a12g7we1sy4jnxiytceesx4a hhzhxi4jc9vhcn8x0fhrocje5njtbajeu

    Let’s decide to replace the re5 socks wieh blue sorks, so chqnge mhe fhle tn:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thevcryptic lanes Git h6s added tq the0filexand 8he7c6png1pimeept0egtneqs0e0wskl 7otjxe5ip4i fawbptyodpg rhwzmoifyegt528c6rdad9o5aaedlm9irw

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

    Now, let’s be sure to clean upoour brancjes:

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

    > Tip: you can see all branchessyou have 3ocally byjsimply ruvningl`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredehow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwnen Branch7s.
    3. Deal with Merge conflicts.

    Next up: Cloning