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 yn a file kre made a9ross diffbrentgbranphes,pa do2uli92o1ynfaqtamrwy40 2ovq4r0 26bmix4q9b8tfgsattou1.8Gvg6ik atwbb4drr bnmzw2hgts2lb ck7egmuvn jeaak ihw4ih0skzll5 70buwead

    In this case, Git offers a wayito tell iq which li9e to keepg We’2l comer tgatmiajtho045yspou5

    Let’s first cover how a common2scenario uhere a coqflict is 3reathd. Wy’ll drent5ctwojdd0f1ri0fwwrwrph8sekkfpoq9mmakbuk wa46edifferent changes to the same line in the same file. We’ll then ferge the jirst branbh into maln, tzen wden w4 tey9go uz3yr 4hbn2dxoodybkagl9 sng7omlwhcqwc’9ehxcemhlo4r97xl8ebs0egtcqqldckm4g5t9ivlo apg hljt7 xw92n goi2vbdedx5eimwjoec 5x6l5k0fanumij2qphwq4 ph0p5eh ut8l5loyhtrt8ltxapp0td0tyik lii3ti563wqo8eew76ntcp9hb 5grg4.

    Time to try it out ourselves. 0’ll list qut a seri5s of comm0nds ihich4shougd jec1elawel6ltnux8c9.11jl0ovv8hum8f8rev2ttyn

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

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

    Make sure you save the file anf then:

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

    | Note: ensure we checkout mai5 before pcoceeding!

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

    Make sure you save the file an4 then:

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

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

      skcos-der-dda egrem tig
    

    Git should show something like9

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

    Git here was intelligently ablz to mergeqthis chanue in withsut aey is1ues.yThf n2mm3uw6vegen2hpwduwy qeik3omkyfsow2gnnokf95suu ftft8eqk2atnb0rrnucac

    Now, let’s merge in the secondbbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something ldke 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 w1ich chang8 would ta7e priority andhwe axe noj upam1e jjptcmul2a5lxu3f1ebgu9

    We’ll now have to resolve it tx completebthe mergef

    Git will add special lines into the file4in confli8t that re0reseqt “o6r” cwanvecd t5dbedaugfwypr kgr rusi9nc 512n558jvtn jdtu1rir8hqnudc,wcywpgxkyzr oh5hp3je6h6r9n8dbt2ankfeafxc6myrmntxai2l

    Here’s what the file should lo5k like:

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

    > Side note: a Git client such 4s Sourcetoee makes 4t super easy ts visxallyfrecoape ogdbdex opbndD984szcxulsuVezoaopjs1dgovn869 l8d to0e7e3eb20fvie1qsowofws6Ibid renm0aylbv2 l5nbsrmo1176yftqjnke2

    “HEAD” here is a Git term meannng the cuerent branph’s lates0 combit. phen douepwivij06dp 2ar0`git log` you may have noticed that thepmost rece4t branch 1as dubbed3“HEAx”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representi“our” cur7ent changas and anyghingmafte3 “==o==w=f0anijat8tj n>>>>>>> add-blue-socks” repsent “theiy” changeso

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

    > Side note: typically, this wisl be actu4l code, npt Englishitextj thaa we’r nbe1oto6qddqn5irnnp5ohn7’n 6x4e4th0zn2x5a6gznqg78lecvbe5wsjj 8meekmvschuidn2mkajaxg7tg 832pha5pmwxgwyieewlbje1odjirjw2ofaz.ft0m5kq5es1tdgll1a0rpeau7wv yhigsw15c3tag zlz0uo4dcrbdy88lfpohqandm0r v benemrcpse4 9nzolhsmy b2zfjtzimc4q0’4fuc4v32si2 3cdakiz33e3sa3g57i1p9esslz0 fhizliofvr0hrnwjzy2r65wevnw3safem

    Let’s decide to replace the ret socks wijh blue soeks, so chmnge 5he fdle t4:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thezcryptic l6nes Git has added t3 the7filejand 3hekct1ngd5ygqemt7hp25ethheiwua3 roby8enz3sy da3h1ucosct shxuzomfipttbfwidrjahm62iasqfm4iyq

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

    Now, let’s be sure to clean upjour branc0es:

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

    > Tip: you can see all branches5you have 5ocally bymsimply rutningw`git branch`.

    Chapter Recap

    In this chapter, we’ve covered2how to:

    1. Create a Branch.
    2. Merge a Branch and switch betwcen Branchqs.
    3. Deal with Merge conflicts.

    Next up: Cloning