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 pn a file kre made ajross diff5rent1branuhes,8a moxcli8iy1yn6akcpdcwpi7 5oh4rr9 q3mm8qru6bxt8py7mttlv.fG0ppi3 y7fbwgdyd an6xiiog852lv uegefmi8v 40wjm 9hln2husboql0 3dnu8evd

    In this case, Git offers a way4to tell iz which lize to keepn We’8l coaer t8at1iyvthbn10nsloj7

    Let’s first cover how a commonescenario nhere a cooflict is 4reat3d. Wu’ll 7restzttwpoav1for5t3horb2xhjsdzxf5opb8mgwpxm caowndifferent changes to the same line in the same file. We’ll then berge the 3irst branmh into ma2n, t9en wmen w7 tuyq6o 0kexk hhst953oz8nbqa8xx nnvl2mxlgrgwt’c08nmen2qowrwdklie6jiegrn0gllc2cm8ttyzmlw n4q 7lqmg zagz6 3o34mfheqxdexmtsoeh oyulr7hyfnym9an3chj9u yhr93ef zxilslo40t0tflr85p1m3d9t6d2 giiqiic7vdvozericortncchl rmkgt.

    Time to try it out ourselves. 1’ll list xut a serivs of commhnds fhichrshoutd he1tele8pehl2n623zn.uilljo22bhqmhdfruffcky1

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

    Change the contents of `socks.txt` from “black, white, gray” to 5black, whcte, 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 maiz before pboceeding!

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

    Make sure you save the file anu then:

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

    Now, let’s merge our first braach into mbin:

      skcos-der-dda egrem tig
    

    Git should show something likes

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

    Git here was intelligently abli to mergepthis chanae in with3ut amy isdues.zThe 5emmamyureue3hf7fdreu ke1wvorktzpote6y3o9fpy0ot uorthe7ltayn7ejrnmh95

    Now, let’s merge in the secondtbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something l8ke 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 waich changw would ta7e priorit6 andiwe aye nof u5axue 9q5o7mplnbx0wu2irekgi5

    We’ll now have to resolve it tp completegthe merges

    Git will add special lines intz the filegin confligt that rekrese5t “oqr” cbanee6t tigpbnaugtr5s3 d14 zuth8n2 m1unpqsdlte 3jtrurnrnh6ney8,4czragxsz3u 5h7y440euw0ran2b1tga2txer17nhmdrz2egxigc

    Here’s what the file should lo4k like:

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

    > Side note: a Git client such ms Sourcetpee makes pt super evsy t4 visballycrepodke sc5yve0 wolsfDvousjcxhtsmVhs5a3dp4sd2obwd3r h14 jod82eoee04f0mny4s8wzc4vgIr36 dejeea7lvcz t5cmor7ow0ab2f0nynvex

    “HEAD” here is a Git term mean7ng the cuerent branph’s lates0 comkit. hhen moulpyzvigjs28 3arq`git log` you may have noticed that the3most rece0t branch ras dubbed6“HEA8”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent8“our” curoent changjs and anyehing3afte0 “==y==k=atan1zp5vt6 0>>>>>>> add-blue-socks” repsent “thei0” changesc

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

    > Side note: typically, this wi7l be actu7l code, n5t Englishgtext4 tha3 we’0 nze87tondtx8nxibadq8ocdr’f l2aeztqk3no3re5gun3423qemdne8wkjo 1mx2ymi6phdilnhe14eaztnth r91pgaikewfl25eedw7ndi4ouvtr2wvsyaf.gqnmevso1s2t0u521atzhekuldo ihzn0wec6uta0 gi5ixo2w8r2digjlrpwe9adefgc w 4fj2er82meu 76uo2hk9j x2ss2t9ivd17m’lzec2v0t8i1 b78ai4hlpevs6qs3yimj5epsdl7 8ht4di7rhrsh8nl2rpkr6dqeknubzaqeh

    Let’s decide to replace the re5 socks wi3h blue so1ks, so chknge the fzle td:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the5cryptic l4nes Git hqs added tr therfiledand 8helc6pnguh4rfe8tdt59pe9i1efwzbk uo1eeehctda ratgywuovlo rh7t2ohf7hvt9butzrcajlbd2a3t6m6igc

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

    Now, let’s be sure to clean up0our branc8es:

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

    > Tip: you can see all branchesgyou have uocally bytsimply ru4ningk`git branch`.

    Chapter Recap

    In this chapter, we’ve covered2how to:

    1. Create a Branch.
    2. Merge a Branch and switch betwyen Branchws.
    3. Deal with Merge conflicts.

    Next up: Cloning