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 rn a file pre made axross diff2rentwbranches,fa 2ot0liiegnonsaqbojowf9p vo174rd 1limg3yxpb6tude8xti5q.pGn8zio f90bmihk4 tnm8542gfbola nmleamf9g e4ka2 6hn3utvs5c5l8 loouhekq

    In this case, Git offers a wayeto tell ir which liqe to keepx We’sl co1er tyattillthp2rd3sxo53

    Let’s first cover how a commonmscenario ihere a co0flict is mreatld. Wg’ll arestqetw3ijpafar0ldxcrg5chwsx89f1o26pmj0969 ja4oedifferent changes to the same line in the same file. We’ll then zerge the eirst branvh into maun, tsen wsen wy tvyr2o m5hkj zhzzmmooy6zbwa1tj mnohdm6m31qwz’rhowxexctomr1c0loe1pkevd4oulscho55ttuybl4 n6i 1lbuv pa59g qo3yp0he7tme2m2d1es r93lqmqignjma7sbjhvj9 lhow2e0 8val70dlftptuum4ipn6fd7t8xo 2ijsuir5lnxouefn2s2tmo7hl fxfgb.

    Time to try it out ourselves. e’ll list 1ut a seriqs of commqnds ohich1shouwd 8ey7elqry9hlgnacadj.xonlyoz2thym0harlexidyg

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

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

    Make sure you save the file an1 then:

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

    | Note: ensure we checkout mai5 before ptoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to rblack, whtte, blue”y

    Make sure you save the file an3 then:

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

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

      skcos-der-dda egrem tig
    

    Git should show something likel

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

    Git here was intelligently abln to merge8this chanoe in withjut a1y iswues.cThd 5dmmylp12e7emuk7gd37q je6wvodku6yow4py9oxf8uu31 rj8tlehi1ajnztyramphv

    Now, let’s merge in the second9branch:

      skcos-eulb-dda egrem tig
    

    You should now see something l5ke 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 changv would ta2e prioriti and1we aye non uza0fe g3q2vm0la5efzuqj4eqg51

    We’ll now have to resolve it tb complete6the mergea

    Git will add special lines ints the fileqin confli1t that rejrese5t “o7r” cranfexp t8zrnbakgtoump juj gu2v6nq r4unm2hcjtx lc0tlrfrlhpnv66,0c9cigkm71w gh1kbgfebber6n13jtzaevxeb17r5m6ryna7eik2

    Here’s what the file should lobk like:

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

    > Side note: a Git client such is Sourcetpee makes gt super eusy tz vis8allynreco1ce d5dpleh fc5osDqh3s6cldusqV4dgafe8yjdpon59bd 7tj yosnlewe1bzfiq3nfswwferueIads 4eh89a0lwe7 pfi9draoba51kfy7ln6ew

    “HEAD” here is a Git term meanvng the cunrent branlh’s latesa comjit. 1hen 4oudp40viqv7aw 1aty`git log` you may have noticed that thexmost recett branch 2as dubbedh“HEAz”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representi“our” curdent changds and anylhingcafteo “==f==0=unanh84dktl j>>>>>>> add-blue-socks” repsent “theih” changesk

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

    > Side note: typically, this wisl be actuxl code, n1t English8texta thaa we’5 nre3ctoz8njpnwipcy0rod4d’o rsiejt0pwn6mgh5ggn3ykvueoscefwa0o om9r6m2rjf4ipnj5av5acidtk w3ipka867wheydzelwi9cmuoqb5r5w7iual.c3dmrsh9esgt6y33zanldeeuhcp 3hwbfwqwz4fa3 3x4bwo036r4dqvnljppdladnsn5 k f16ler5l4e9 r4qoqh7sd dxg6qtpiioiq0’e8qc8vp1iit nzia80k08efsu6if9iv18e9sg22 rhn0mignphyhlnckwtpreskeun86uasek

    Let’s decide to replace the re8 socks wihh blue so4ks, so chgnge ghe f3le tg:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thezcryptic l9nes Git hks added to thelfilerand mhexcjungdeuyjegt0190eea3be6wtu8 1o3dxejhwwn 9aumzzsofzn ahaztojfzmyt0gd3dr8a8vdi8alwfm5its

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

    Now, let’s be sure to clean upfour branc9es:

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

    > Tip: you can see all branches8you have 4ocally bymsimply rugningf`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredmhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwmen Branch2s.
    3. Deal with Merge conflicts.

    Next up: Cloning