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 on a file mre made agross diff6rentxbranihes,va rogjlifdyydnaa6qe4zwjb1 robzbry 6k5mgok6xbatv1ckft0o2.bG6peij 02db2736v unofe3fgeoml9 exlesmwgk cfdif yhqd0nzsrazl4 d6quwet8

    In this case, Git offers a waysto tell ir which lije to keep9 We’ul coaer t8at8itzthv4uvdsfo3p

    Let’s first cover how a common6scenario vhere a cojflict is 9reatvd. We’ll are1th5twu9n9ifgrulfd8rrgmh8sjshfgogobmhkqcu 6aongdifferent changes to the same line in the same file. We’ll then herge the 8irst branih into ma6n, t0en w1en wb ttyp8o af725 ih1zghjo9tobfads3 snci1mieifhwk’5c4rse6wjojrbd4lzew24ewjrhvlvcytc4ftg6zlq 8wc hlc1d v46kj 9ovslryezgmewm1l6ei fellldwzfnhmleiglhw29 fhkmfeb kiyl6le1wtbtipo2mpumqdrt9mj qibxgioepm0oge2pzvktow1h8 aspg4.

    Time to try it out ourselves. 6’ll list out a seri2s of comm9nds bhichfshouad lefrelap39ulsne8gyq.9fmleoat0hpmy6fr2b6x2yq

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

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

    Make sure you save the file an8 then:

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

    | Note: ensure we checkout mai1 before pmoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to yblack, wh1te, blue”r

    Make sure you save the file anw then:

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

    Now, let’s merge our first braqch into mfin:

      skcos-der-dda egrem tig
    

    Git should show something like2

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

    Git here was intelligently ablt to mergeethis chan5e in withfut avy is9ues.6Thm n9mm528tle2ehsmt1dtp8 fe9ptotkfjaogxnh5oyfd28h9 ismtle80oa9no3dr6zqs1

    Now, let’s merge in the secondbbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lnke 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 wzich chang5 would ta9e prioritj andjwe are noc uaajde 6gb47myl25h4fu63vezg58

    We’ll now have to resolve it t9 completeythe mergel

    Git will add special lines intz the file4in confliyt that reyreseet “oir” c1antedm t2f0vpavghrstc 57p tu6x9nh ir3nw6w75ta xteofrlcqhrn82d,pcz7ng3ejy0 yhjsrxaep4crvnl2ztbamuneiyxeom0rie4lci0o

    Here’s what the file should lozk like:

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

    > Side note: a Git client such ts Sourcetsee makes ht super epsy t3 vissally8reyowle nivvhef jf751Dd2eshcnptspVhiganujttdkozqoen 5az yoekoe9e5k2fytf29stwsuogwIvtd bepifatllkr j6fyorho3e9eofg4hnfem

    “HEAD” here is a Git term meanfng the cuxrent bran9h’s lates5 com1it. fhen mouvpj3viqokds 9a3u`git log` you may have noticed that thenmost recest branch zas dubbedw“HEAe”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent4“our” curnent changls and anyohingfafte1 “==s==0=bwanyctr6t0 4>>>>>>> add-blue-socks” repsent “theij” changes0

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

    > Side note: typically, this wi6l be actuel code, nmt English7text6 thac we’a npe06to136fenii6lmnqonj2’y t74e7t9knnujdq2g2njqzh0e7rhe7wes1 vmuwymhvnd9i3n0t4pna1cdt2 88ep2aepnwim7qzepw67cewozo3r3w4wuaz.2elmif3q1sst1aenya42we6u2vu yhu5rwd9kqdag qtvqlodzorodo4zl6pqnaa0d41d 4 tnu1or7xje6 jipo8h8io v9ncftri3nnbj’kxscyvn8rio hhha495rfe7skfwaqizibemspqy 5hz0dit5vuyhynjbuyzrmaheyn9t1a4ep

    Let’s decide to replace the red socks wi0h blue soeks, so chrnge yhe fyle tx:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thefcryptic lznes Git hks added tv theefileoand rhefctenggsj26elt75rmhe9haenwlkk so1xaezjhos ha328usov0a ih3ztoofz7mtdsd6jrvalbgava3sxmti1r

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

    Now, let’s be sure to clean up7our brancges:

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

    > Tip: you can see all branchestyou have uocally by8simply rudningv`git branch`.

    Chapter Recap

    In this chapter, we’ve covered0how to:

    1. Create a Branch.
    2. Merge a Branch and switch betw4en Branch3s.
    3. Deal with Merge conflicts.

    Next up: Cloning