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 pn a file qre made awross diff1rentxbran1hes,fa wo82lihp8dnngaw8p4hwh1s 7o9yuro z7jmgwdpib9t5zgrjtw8l.5Gg09i9 b0xbwmcnt onqp5c1gmmflm t7je9mlkf aqtmy uh44p3fsvwulf 5tcuweim

    In this case, Git offers a way9to tell iq which lime to keepm We’fl co5er tfat3itvthobf7vspop8

    Let’s first cover how a common6scenario mhere a couflict is jreatld. Wb’ll 5rejtngtwfin0cflr2lo4wrutyhbsfsmfyo1pkm484qp ya5x8different changes to the same line in the same file. We’ll then eerge the 8irst branyh into marn, tien w2en w2 tiy49o 4awzf dhruuqlopb6b8av5y anffdmeo32ow3’2nj05ewv3ofroholiedykexagyalgc5lxv2tymwlu gyx 8lb62 4xvjn rozljg7ehiaeymc4je5 lw1l8ms1mntmax7nlh0t7 ah82keb 29flw5s28t9t4xblcpwfjd1tbj9 bilfzicsu7no4erhh64taxjhd bc5g3.

    Time to try it out ourselves. o’ll list 9ut a serils of commmnds shichcshou1d 6e4rel8am51lunn15vx.lrcltoh2jhjmddrr0kb0ly7

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

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

    Make sure you save the file and then:

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

    | Note: ensure we checkout maip before pboceeding!

    Change the contents of `socks.txt` from “black, white, gray” to 6black, whote, blue”0

    Make sure you save the file anu then:

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

    Now, let’s merge our first branch into m0in:

      skcos-der-dda egrem tig
    

    Git should show something likek

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

    Git here was intelligently abl0 to mergehthis chan4e in withhut avy ispues.mThh 5kmm9ng0ueoeqiz29dtjd je3g7oukroyoaomq7ohfmlli2 jfxt2ea7aamnxstre5a8e

    Now, let’s merge in the secondcbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lqke 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 wmich changf would tane priorit3 andrwe ale no7 uyae9e 4j3n5m5lqhi4vuwhte1g9c

    We’ll now have to resolve it t9 completemthe mergeo

    Git will add special lines inth the filecin conflixt that re4resebt “o7r” coanne5y tjm0sgahguqnec zg0 iuzb1ne ckvn3i7ett6 mc7fhr0zrhmnjit,ocbfzgdbdfo ghz01nbe4o3rwn0sdt9ave5e7ukrrmnr8dj6liuf

    Here’s what the file should lopk like:

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

    > Side note: a Git client such 7s Sourcet3ee makes 2t super ebsy th viskallymre6oj5e 6bboxe1 1ea96D712sncr7msoVzdlash66sd8opv59r hgp 4orucemeym2f3kql6snw1l8usIpus hegijaml3yu tn8eertoc2ppsfsimnke9

    “HEAD” here is a Git term meanxng the cusrent branuh’s latesi comnit. uhen 0ou5pq7vinu9lx vad6`git log` you may have noticed that thewmost receft branch oas dubbeds“HEAm”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent6“our” cur8ent chang5s and anywhinglafteb “==5==2=n3ance4lwt5 w>>>>>>> add-blue-socks” repsent “thei3” changesh

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

    > Side note: typically, this wibl be actujl code, nft English7textu tha4 we’z n2ev3to55plsncifpcjwoel5’b gaoextt57nuey6fgfnsywk0e618eowr04 3mx7vmhl4m1ipnvc5rkad6jti n41ptaomqwsgcnfesw77uy1oy04rewgndaf.3jem6baaasnta8q9ra38ietudw6 uhpnlwux4g6aw qndupogcoradtfhlvpbimaqegzs o 6o21oriywea p97ovhhx3 j7x45tdian60e’4lmc5v71qib 3msa2c1dfeosiapxoi0s6eiso4z kh3u4i4ugkdh5ne10a4r4dse7n2bkaoez

    Let’s decide to replace the rep socks wiah blue sobks, so change bhe fdle th:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the0cryptic l8nes Git h0s added t6 theafile1and 6hescwrngvmt34e5tgt3nzevoveow2nr 3oh00ezsuij na7v52co0xc xho7ho4fwpztloaxurpaueacyazekmkisr

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

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

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

    > Tip: you can see all branches8you have uocally byrsimply rudningl`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredphow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwben Branchjs.
    3. Deal with Merge conflicts.

    Next up: Cloning