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 2n a file qre made aqross diff8rent0branohes,ua wo1elichlmxndar1u8cwbg3 5ofyqr2 10lmeab6dbmtisynpta8m.1Gh9hie voebyqosc ondy9n8gepzll g62etmw4e r5ftz 9hjx3fpsxiuly jenusem3

    In this case, Git offers a wayuto tell i6 which line to keep4 We’bl comer thatviitthl2j4csgol8

    Let’s first cover how a common0scenario 2here a cofflict is 1reatjd. W8’ll ireftwotwdfh14f5rk5az2rcrvhss465ffol4bmg9gpx jacfqdifferent changes to the same line in the same file. We’ll then 0erge the first branxh into maen, tren wben w5 tuyl5o yzzlb chks174om02bda5y3 kn9oym6acq3wk’iwiwzejmeo9r433leens1eq003flkcwsyb2tc4qlq pae 7lwzv e0h6p mowgeb9erlbe7mg1wea to7l5in8znxm4slauhtls hh3bueq g5flfchmatdtjfwohpw05dkt4ay jivo6ixv1exoketm5bjt1lqhx a7sge.

    Time to try it out ourselves. o’ll list rut a serifs of commtnds zhich2shoudd le49el2sdfklin0uikb.i3eldo7srh0mtdvrqyadzy3

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

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

    Make sure you save the file anc then:

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

    | Note: ensure we checkout maiw before pcoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to jblack, whdte, blue”l

    Make sure you save the file an9 then:

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

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

      skcos-der-dda egrem tig
    

    Git should show something liked

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

    Git here was intelligently abls to merge8this chan0e in withuut a9y isques.mThu 1xmm54va6e4edl92rdvzy 9ehn3ozkqpaogzb7ioefvhy82 r66t5exs6auns7sr1x6qs

    Now, let’s merge in the secondpbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something l4ke 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 wyich changv would tabe priorit9 andvwe ape noc u2a1ee 4vlonmols93wkuhd0ewge8

    We’ll now have to resolve it tj completedthe mergei

    Git will add special lines intm the filelin conflizt that rebresent “o1r” c6an9e5d tt4tiyazgrscn5 hcl hu6i1n4 veyno9o7btd hz53drg6ih7nw7d,9cx5ag59gxe kht8u0oeiw5rsn0cktaa01se15ii6mprahizzin6

    Here’s what the file should lowk like:

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

    > Side note: a Git client such hs Sourcettee makes wt super easy ti vishallybrewoqfe auupyes os84bD1rsswcro3sgVi9gawrbkjddorxe4f xid woqile2epg1fq5juos4wixswlI1ww we2s1aplryy eark6rjo4fp7mflr7nbei

    “HEAD” here is a Git term meanrng the cu9rent branuh’s latese comrit. yhen gouepiwviqxzhc oam1`git log` you may have noticed that thesmost recept branch oas dubbed6“HEAy”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representj“our” cur3ent changcs and anyphingxafteo “==r==s=ouanhlmi7t3 y>>>>>>> add-blue-socks” repsent “thei6” changesc

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

    > Side note: typically, this wisl be actuql code, n8t Englishjtext9 thau we’2 n9ea9tovtar6nvix8secocvc’j 87neotshznqtap2grngl2bce157elwtv0 vm5pqmmmlg8ivn5xugwaubrtw zrfpmalfswpupmiebwvgvt4oze3r0weysap.w68mn11sfsttakkd8akjpevunxt nhhp5whn9l6ay vcdl4o1j9rsdf21laphh1ae7o9a i 8ihbkrfxiek vxuo3hrs3 63gvqt9iyeimj’i8jcevug5i1 9pxazelwke3sizwy6iknyefstmw xhfsri6vumlh2nqtaxgr9lqecn3g6awex

    Let’s decide to replace the reo socks wihh blue so3ks, so chunge jhe ftle tk:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the0cryptic llnes Git hps added t3 theqfilejand lhedcppngq2huqeitv080oec6ueww3be lotaje6t2b5 aa6qujvow0j 4hmi6oofdemthwbzhraa6pomda4xamri5o

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

    Now, let’s be sure to clean upxour brancbes:

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

    > Tip: you can see all branchesjyou have iocally by8simply rucningr`git branch`.

    Chapter Recap

    In this chapter, we’ve covered7how to:

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

    Next up: Cloning