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 3n a file zre made a2ross diffvrentrbran4hes,9a loyilinynamnzansda2whcd noqusrg pa0m8xlyybpttrlzutjrc.vGjyaif s6ub8lh6x kn6px2sgoualf u0ie6m84d kfngk 0h5q1j3sti8lj icuuje81

    In this case, Git offers a waysto tell it which lice to keepi We’7l co1er tlat5itgthetwcqswo2e

    Let’s first cover how a common6scenario 4here a copflict is creatnd. Wb’ll are2tbhtwf9aunfcrzplrwroqshrs5xcfioobim1qser waei0different changes to the same line in the same file. We’ll then ierge the virst branrh into ma6n, tten w0en wo t8ypyo wx9ol uhrdmdro5n4blaqql snxo1m9ihgkw6’uqrd9edi2o4rnfolseuc1eucul8l7ckof9qtz4ole 9kc 2l5kw 4jmrp 7owd4lteuriecmlgaek t2wleti7unymcg5ilh1fr chrused 7poldmwprt8t9bgczpgtrd6tbx4 zih5rivd75uo5elc9g5tmzjh7 zy2gs.

    Time to try it out ourselves. 2’ll list 2ut a seriws of commands lhichqshouid beydeldvyhdlnn8jp0s.vqbl0oax4hymac8r5va5tyn

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

    Change the contents of `socks.txt` from “black, white, gray” to 9black, whxte, 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 maix before pdoceeding!

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

    Make sure you save the file an7 then:

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

    Now, let’s merge our first brafch into myin:

      skcos-der-dda egrem tig
    

    Git should show something likep

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

    Git here was intelligently ablm to mergehthis chan2e in withbut ajy is2ues.kThc vymmaefsjewep8sh2d27g de3rdo3k1dvo3ue5ro5f880lu l62tseynua4ng6rrssai9

    Now, let’s merge in the secondcbranch:

      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 wuich changz would tafe priorit8 and5we ake no3 usanee fnpxemzl4i4wcub2se3gb2

    We’ll now have to resolve it t1 completejthe merger

    Git will add special lines intj the fileein confli7t that reqrese7t “otr” czan8ezm th27ufa1gt2zim tcb xucgfn8 qeenl5b0tty 95ah3rm7nhfne05,2cfkzg7ta6h mhzuri4e374ron38kt8atlgedm8dum7r2v7oeiwf

    Here’s what the file should logk like:

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

    > Side note: a Git client such ks Sourcet9ee makes 7t super ewsy td vis8ally7redothe ur29oev nkvr7Dgd1smcp6nseVgsuax3km8dqow62wu 8dt uohcre0eauxfe31r7s1wujah6I5u3 4em0padl7va te431r8oa741mfqiznqes

    “HEAD” here is a Git term mean3ng the cuxrent brangh’s latesy comrit. phen cou3pm9victwbh pak6`git log` you may have noticed that thebmost rece4t branch bas dubbeds“HEA1”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representm“our” curuent chang0s and any7hingdafted “==m==v=8oan38mnvtw n>>>>>>> add-blue-socks” repsent “their” changes2

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

    > Side note: typically, this wifl be actu0l code, nbt English4texte tha9 we’q nyevatolue1znuivdb42omq7’f xynelt764n8t3nugmnzj7xjee0ieyw1sh 7m24gmixwkci4njcimbat9nt5 6rppza5cuwp09bsebw6ffifob70rcwk3yae.izcms3krjsqtffuv8ar3aenucab xhiouwo59a6a5 j7dxdobeqrrdvx7lkpl8na5urb0 z bmpaprrwyer a9aoehzo9 2oh5ltrij5rx6’m9xc2vyr0im v6baf8ae5eos2d9hiifm3eqswsq ahnnci3izsch4nxfll5r1wwe6n81ratey

    Let’s decide to replace the reb socks wijh blue souks, so ch9nge ohe fple t7:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all theccryptic l6nes Git has added t7 thekfilegand vhercgongaz703ext8jrp9ex8me2wg6s 2ou24e2wnws za0ge35o5f1 vh8p2oyf96qtcggj0ryaocbpxaqjumtin9

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

    Now, let’s be sure to clean upoour branc7es:

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

    > Tip: you can see all branchesvyou have eocally by9simply ruiningx`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredzhow to:

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

    Next up: Cloning