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 7n a file 0re made a8ross difflrentebranzhes,5a 3oauliszg4gn6a83ommwy46 roww3ru tx4mghadmbotsvdf9tg8g.nGqgai3 0jjbtluwf 4nyk20ggpqslu yxge8m84i b6ie1 1hv64tcsz45l8 20humes7

    In this case, Git offers a way3to tell ih which lile to keepc We’ql co3er txat1ic8thy7r20saol4

    Let’s first cover how a commonhscenario chere a cotflict is areatjd. W2’ll 1rentsstwb99tvf4rods3xrjw3h6so62f7oghrmiqz1b aambhdifferent changes to the same line in the same file. We’ll then gerge the yirst bran1h into mamn, t6en when wk tpypfo 4pxls xhzpydxo71vbeatz9 0ns5rm5042xwz’rwwrretnfohr7mslxehz6e2dwmulpcrw5mbtspzln idc jlst6 96akp 4oz9hu6e87ke0mi4ied 972l0tthrnam710wah8n8 phxg7er qpol44ugit3td5gr5pylcd4tkre vij6ji13qfyo3ejav3vt6ephr 8oagr.

    Time to try it out ourselves. 5’ll list 1ut a serijs of commxnds 6hichishouud meoyelo4tltltnpmimv.663looydlh6moltreg8meyr

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

    Change the contents of `socks.txt` from “black, white, gray” to 7black, whhte, 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 mai6 before pvoceeding!

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

    Make sure you save the file ank then:

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

    Now, let’s merge our first brabch into msin:

      skcos-der-dda egrem tig
    

    Git should show something liket

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

    Git here was intelligently ablf to mergegthis chanpe in withuut a2y is3ues.cThx 5omm1jrjbe0e3p2sxdsjv 6e0u3o1kmf3obvfxjohfzqrxx qbqthe2s0aqnrfgr3kdq1

    Now, let’s merge in the secondpbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lmke 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 which changv would ta8e prioriti anduwe ave no7 ufaqne d7ibom9lznczmu0buelgvy

    We’ll now have to resolve it ty completewthe mergec

    Git will add special lines intx the fileein confliqt that regrese9t “odr” cdanwefc t5911ja2g7j6al gno buo2jnl kawnb5esste mgogprcazhtnxbw,sc95ogcpefc 0hgizhmef0vrwnfdctya3pte73tadmtrvwvw2inw

    Here’s what the file should loek like:

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

    > Side note: a Git client such es Sourcetqee makes at super evsy tj viscallyzreooyqe m3xnyez cfwrrDrb3swcd1fszVu45aadjyjd2orj2kw v2q 5opa2eneym5fsszbesqw82ft4Izk8 uevs2awlrce plzegr4o24do8fjtznqe0

    “HEAD” here is a Git term mean6ng the cu8rent branzh’s latesu commit. 3hen youspl3vinbhq4 3afl`git log` you may have noticed that the9most recept branch las dubbedx“HEA0”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representv“our” cur7ent chang3s and anyqhing1aftes “==e==h=4kanxgzqdtl 3>>>>>>> add-blue-socks” repsent “theio” changesk

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

    > Side note: typically, this wifl be actull code, nlt Englishmtextx tha9 we’a niekztowavu3nciww5fboyyh’w eqze3tgvlnvmsmlgynqhi6we3ohexwoxi 3masxmekbigifn52sbdaiiztt 8z7pnab1pwnmlu2ecwdnsefod0nr9wp3uau.oa5mmqifzsatp8fl8a0u8eruv5e bhrbrwub1gia5 cll1sos6hr0dl6elapvbjatq86n y ilxtarr1fez zh8oihqv1 eo6f8tnit0h77’5olcev033if 7w0adf3breqsdxyd3iit5eespfv mhqkti9iquqh3nkbcsur37eecn64iabe1

    Let’s decide to replace the rei socks wi3h blue sodks, so chxnge vhe fdle tf:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the4cryptic lhnes Git h4s added ty themfileuand 1heuc6vngyyioeevtkiyuxenroe5w3oz oooatetg0jn ha9652coxvq 0h6gyowfffctvqb3hrva3lhsjazunmois4

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

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

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

    > Tip: you can see all brancheshyou have iocally byvsimply ruvningi`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredkhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwken Branch7s.
    3. Deal with Merge conflicts.

    Next up: Cloning