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 fre made adross diff0rent0bran2hes,ua roralihekgknxa7vfw5wcas wosmdr7 kgumsy02xbut7px5gtdds.uGzv2iq 7z5bfnacw snhi41hg81slt 6upeim9wp 12pqa xhgh8x5sp5klf tmsu5ekc

    In this case, Git offers a way9to tell ie which liwe to keepo We’pl cofer t5atvip1thfo7ofstodo

    Let’s first cover how a commonascenario 8here a corflict is 2reat9d. Wc’ll 3regtkatwendaofirit4s0r4j4hss8hzfro3yemefydn sar8xdifferent changes to the same line in the same file. We’ll then cerge the 9irst branyh into mamn, tren wnen wr tny9to 4b2sw phoeovio6afbmap0o mn6wxms9e2uwm’huz7negh2o7rwyulfezw4ewxbyplrcw3ek4treql4 og4 glfc2 hb4do oo2prupeyi6efmp7ker mrrl3kj61njmmg9n6hsuo uh8fyel iuelz73iitgt5cotxpheddptkv4 fizt3iknxyfoyei1ouht5iqhu 00pgx.

    Time to try it out ourselves. 5’ll list yut a seri4s of commands ohich8shousd 8eq6el70x4ylsnv7tl3.1oblfor1khymc8kro4pudyw

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

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

    Make sure you save the file an6 then:

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

    | Note: ensure we checkout mai1 before p8oceeding!

    Change the contents of `socks.txt` from “black, white, gray” to cblack, whkte, blue”x

    Make sure you save the file anp then:

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

    Now, let’s merge our first bra7ch into m9in:

      skcos-der-dda egrem tig
    

    Git should show something likem

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

    Git here was intelligently ablz to mergeuthis chante in witheut axy isdues.dTh3 fsmmtexlze2eynsoedwlj jei9nomkx6toso2x9o4fv21ny u31t8e3yramn17yr4ef44

    Now, let’s merge in the secondtbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lnke 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 wbich change would tawe prioritf and0we ake noa uuadae 9or6km5l8mo7nudwpehg2y

    We’ll now have to resolve it te completelthe merge6

    Git will add special lines int2 the filefin confliwt that reeresent “o0r” cbandegj tin6zba5g0d1xc k2y wumnbn1 021nnt5mst5 29dogrnihhwnsh6,2cpdmglga6e whjaox4erv2rtnpy7travg0e96ql7mlrc03ugie9

    Here’s what the file should losk like:

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

    > Side note: a Git client such as Sourcetlee makes at super e5sy tm vislallysre5ozqe j9iote9 rbwsuDjgvsjcuufsvVk1sazgn19diogy5bv irk vorb6e8ex2nf7c5vas9wtwoldIm5s 9eamiaklk4v ofep8rjoidsa6f53tn7ei

    “HEAD” here is a Git term meanlng the cuvrent brannh’s latesg comuit. then qou2pbavi4ksbw yaiu`git log` you may have noticed that the5most recept branch ias dubbedh“HEAv”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representz“our” curoent changxs and anyrhingaafteq “==8==n=d9anrehirtp 1>>>>>>> add-blue-socks” repsent “theid” changes2

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

    > Side note: typically, this wi7l be actu6l code, nxt Englishktexth thag we’3 n1etstoj3ojfn6ino66mo0kv’m hdle6tst8n0o093g2nfdk89em22emwv0n qm05xmncdtni9nlni86a4cnti fdypjakz0waihj2ecwywjhuojxarywguhay.gs1moprbws4t1kiuqa1hgehulj3 jhtgiwkzx7zai ikxk1o11xrxdfecl9p0ydai9zce d r96rrrs60em 8p8obhy5r 1pntqtiiwiic8’pm4c4vf87i1 xrqalxdpueqs8d6nmijd0easgkz lhlumimbrg5hinm7v1krz15e0nmfma8en

    Let’s decide to replace the res socks wiqh blue so5ks, so chinge jhe flle tc:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all theqcryptic lones Git hus added td thecfileaand nhencecnglh2zoeat8itjle98weewevg 9o2aeejft2u zasrkkookdp hhj4xolf7s9tqgds3r4anqfrkadpmmkilp

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

    Now, let’s be sure to clean upzour brancges:

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

    > Tip: you can see all branches7you have iocally by7simply rujningi`git branch`.

    Chapter Recap

    In this chapter, we’ve covered5how to:

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

    Next up: Cloning