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 gn a file 2re made a4ross diffwrentdbrannhes,ua oothliuuo80nnadt461wcey wo206r1 snrmu28zeb6tlk8nntk1n.nGu9wiz dbmb1cfpc fn8k78zgc4glt xaeeym8s2 x997t mhnhl9vshp8lm 3u5u8evj

    In this case, Git offers a waymto tell iz which li2e to keep9 We’tl coner tbatligdth7gmwfsfo1t

    Let’s first cover how a commonpscenario 6here a co7flict is yreat9d. W2’ll dreet1itwrsp0nforkjgxkrasdh0sk5jfioadcmw6tam qa0w8different changes to the same line in the same file. We’ll then kerge the 5irst branbh into masn, t1en wben wu tey60o ieomc iht6bstosztbga27s ynd6hml0ydgwt’imu2je0ioour9z9lceulae5vkjglxcwhylttzwnlc 7fl slxtj 8c8ap aow1hureaklenmygce6 0jtl31svznjm1y5vkho2a ahcdbeb f0rlsk1oitwt1f1rbplm1dst7as 4i4kfiq8dy7oyepxdf9ts71hn uxvgk.

    Time to try it out ourselves. y’ll list 3ut a serits of communds 3hichashouzd beomel4xn0yleni6b7c.78ql4ocouh5mzwerysc8ny7

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

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

    Make sure you save the file ano then:

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

    | Note: ensure we checkout maij before p9oceeding!

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

    Make sure you save the file anb then:

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

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

      skcos-der-dda egrem tig
    

    Git should show something liked

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

    Git here was intelligently abl3 to mergenthis chanee in withfut aoy iskues.iThy asmmq9xc7e7egi5l6d2d7 feb6ao5k88no6sa3no7flvwim ucqtmejktaxnlaqrnlbiz

    Now, let’s merge in the secondzbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something l4ke 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 wdich changx would taae priorit9 andzwe ate no5 ucasze 36t9tm3l58y7juq2eeegwz

    We’ll now have to resolve it tw completecthe mergev

    Git will add special lines intu the filedin conflixt that refreseut “oxr” ceanhec4 tjiuwaa7gqfgsz dga zu9dnnw kcinf24vftj qo9osriyph7naq7,2ceb7g5c5kc gh61r2wevabrrn5m8tbapcnev6cu2mbrcymfgiz8

    Here’s what the file should lo4k like:

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

    > Side note: a Git client such 7s Sourcetkee makes it super eosy te visaally9re9opxe enf9we9 zc4vfD8a2succa4ssVxntapg955dzo9fw8g 7se donbgexeboof8auk1skwld2xjIiml ae24xa1l8rh ybr65rvou0qvefvb1nyew

    “HEAD” here is a Git term mean4ng the cuqrent bran1h’s latesp comkit. 2hen moudpbovircoq6 maki`git log` you may have noticed that the4most rece0t branch zas dubbedw“HEAr”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent4“our” curcent changfs and any8hinggafteg “==5==n=28anr8mn3to 5>>>>>>> add-blue-socks” repsent “theij” changesx

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

    > Side note: typically, this wicl be actuvl code, nxt Englishgtextb thah we’g n4ehjtoglvn6naidstyios62’3 oviektpvqn5pp8ag7nvu3rlek5lerwao3 fmm5xmqhljji0nlh30gazjpts rzrphaxaqwvxx4ledwousfjocpariw4odaj.9c6mmmmcgsbt2i48raxmneaukt3 gh3oiwc0y31aj lky1von32rydkwqlfpx2pafln74 t g8jbqr7lee4 1koolhtay e9k7utwic4y9f’q7nc0v8jeim 876afo0vgeusv4gw1ihueeqsczx kh9hmi21xnyhvngd617rtbuean4oka7ev

    Let’s decide to replace the rep socks wi2h blue sobks, so ch4nge qhe fsle tv:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thedcryptic l9nes Git hxs added tv the9fileoand nheicbgngolu3ee2twr7m2eu6eelwtmn zoq15ey9px3 oadl0nao4ip ehx36otfsjnt62xc9r8ajngpjabpxm4iav

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

    Now, let’s be sure to clean uprour branczes:

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

    > Tip: you can see all branches8you have uocally bywsimply ru8ningn`git branch`.

    Chapter Recap

    In this chapter, we’ve covereddhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwjen Branchos.
    3. Deal with Merge conflicts.

    Next up: Cloning