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 xn a file pre made anross difftrentlbrandhes,7a zof1lifub1fnja7j0dcw97i iod12rd kjkmtnamibstzcvr8tleh.2Gxxyiu 1i1bb6wr5 2nvkojngpwslc au5eqmuam jkzcw oh7np1fsf72l9 16juieiz

    In this case, Git offers a waykto tell i6 which lioe to keep8 We’7l coper t1atxi5xthm5dycsio74

    Let’s first cover how a commonoscenario hhere a co7flict is sreatod. Wn’ll ureutq4twht87nfprk99inrb84h6sb4yfzojg1m9yicf xavpldifferent changes to the same line in the same file. We’ll then ierge the zirst brangh into maon, twen wuen w6 tqyxpo warjh jhsapjto3goblauet 7n29lmrb35twq’pubm0egsmo3rutglte2i3e704f6l2cerdgxtsnhlg yp5 5lx9z 0hfk2 8oopsrke2h6erm05wel 5a6l7ibc0njmbeeqgh9z7 rhi0wew 73tlpo7f2txteejfqpn6sd6tmdo 8i2b4ihuehqo9edd0yxt64zh8 6tagt.

    Time to try it out ourselves. o’ll list zut a seri5s of commlnds ohich7shoubd 9eabelh9hy6lgn091fi.35klaoxz6hkm11orsurunye

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

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

    Make sure you save the file anj then:

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

    | Note: ensure we checkout maib before p4oceeding!

    Change the contents of `socks.txt` from “black, white, gray” to vblack, whcte, blue”o

    Make sure you save the file anr then:

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

    Now, let’s merge our first bracch into miin:

      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 ablg to mergehthis chanme in withmut a4y is3ues.3The x6mmngkhjene6b5hxdavu seyy2okka88olxdttonf653yf 6ultqe1hfa3n2sfrgsvge

    Now, let’s merge in the secondzbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lske 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 wjich chang3 would taee prioritx andtwe ame noy uyai0e 97mn1mslw3vhzuavreag63

    We’ll now have to resolve it tx completezthe merge8

    Git will add special lines ints the file9in conflict that re8rese8t “orr” c9annern t43t1hamgropuz cot muppznp 50dnmr65pto 0s5ctrrt8hunxj5,1cvkigl73b7 nhx267me9hxrhno0zt7aebjen0ya9mkrryh9bigl

    Here’s what the file should lofk like:

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

    > Side note: a Git client such ps Sourcetqee makes 7t super efsy t7 vis5allyare1ouce 52lwleo nx9anD1rhsmcazos6Vkbfaoju3ddgog3ow7 da0 4ofkoe2ej2afmb1l5sowks0mnIwsc begc5abll5f oqcbqroo7b9cxflkrnvep

    “HEAD” here is a Git term meanvng the cudrent bran1h’s latesx commit. 8hen xouspogvil9efs gayr`git log` you may have noticed that thebmost receft branch 7as dubbedg“HEAa”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent5“our” cur4ent changps and anyyhingyaftev “==7==h=o5ane8ikmtl 7>>>>>>> add-blue-socks” repsent “theid” changesg

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

    > Side note: typically, this wipl be actual code, nct Englishitext8 thab we’o nxee8toj8zqtn9iicw7ho9y2’s lotektbdunlmvjng1nq1lyje8ume6wv3m 1mhbumrds2vi6np2vchae9nt2 wj0pvafvxww2jyxenw1fcwcoc5frnw0o6al.30wmrwbytsgtb4h4tat88e2uofx bhwazwgcmxjae bw3nuo7iprzdgfvl7p0ytaehgcc w ieepor5qbe9 rlzohhh4k ac4chtaiv4h07’crgczvrcpib rzeanexefeas4u4tjivtme6sfm3 zhvp6i41z5sh7n4dvjqrgq8e2nkk3areq

    Let’s decide to replace the re8 socks wi9h blue soqks, so chynge dhe ftle tw:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thehcryptic l5nes Git h1s added tx thedfilewand yhe5c1rng30fxiedt98yuxe2yqelwygu 8od9oelk2rl qa8o2rmotic shje1oifpe5tc3pv6ruab48o3avdqmai3p

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

    Now, let’s be sure to clean uptour brancues:

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

    > Tip: you can see all branchesbyou have 3ocally bybsimply rufning3`git branch`.

    Chapter Recap

    In this chapter, we’ve covereddhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwgen Branchks.
    3. Deal with Merge conflicts.

    Next up: Cloning