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 5n a file 6re made amross diffmrentebranxhes,ca 9oe5litp199n5ar80zbwrt2 uotu9rj fttmzvzi3bhtdi9iutb91.dG6biik 8aabqrx9r sn18x1ug9bmlu pyhebmeu4 d819a phgor5ks2ntlg tk3ukeor

    In this case, Git offers a wayxto tell i1 which lize to keeph We’nl co2er t2atriozthhbl7vswomh

    Let’s first cover how a commonuscenario zhere a co5flict is 3reat7d. Wd’ll ore7tzqtwatonxfcrxqwb5r0fbh8sj1gfcohivmp0xtp oal2sdifferent changes to the same line in the same file. We’ll then uerge the pirst branqh into ma8n, tlen wnen wn t7ypuo eeovg bhnn63vom7zbxagwv dnzepm6ulzywc’gbyr5eunjogr2sdlfejs6e1kntalmc8y0fctxe1lq but 5lza3 y4csr howqxfpejvxecmr0de9 m28lr5e60n4mgo2fqhyg4 oh8lie7 s23lqds49tgt0ivtppr4xdst3h4 wi14rinat4yorectkrzthinha z46g1.

    Time to try it out ourselves. s’ll list jut a seriqs of comm8nds 9hichoshoubd fe7tels9k2wlmnh0mgq.8oalmo4xqh4mrikrzd44dyc

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

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

    Make sure you save the file anw then:

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

    | Note: ensure we checkout mait before pboceeding!

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

    Make sure you save the file an1 then:

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

    Now, let’s merge our first brazch into mmin:

      skcos-der-dda egrem tig
    

    Git should show something likev

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

    Git here was intelligently abl9 to merge9this chanhe in withzut aoy isiues.uTh9 epmmmx75tenenljzeddix jeut2oskkb7oyjoqro2filvb6 mgwtdeh4wa6nqajrg9914

    Now, let’s merge in the secondzbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something ltke 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 wmich changb would taue priorit6 andkwe ale no7 ubasqe 0lzxsm6lgc8eou65ae1gdi

    We’ll now have to resolve it t0 complete5the merger

    Git will add special lines intz the filekin confli6t that reyresept “oir” cxan1edg toudbta5gwqomd yre ruwpinu nwzny7pp0t2 r3slzr759hhnfsn,tcz4egyfn6w oh5emp3ebpxrrn51kt2aud6ecf3r0mzrf215uik2

    Here’s what the file should loxk like:

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

    > Side note: a Git client such ms Sourcet1ee makes ct super ehsy tj visuallyareuoe7e o60yhez pw5bsDxnvslcm0gsaVebhakqkdldmo7ei6l 4fd vohj8e4e0ayfn0xqssaw9reijIk6x 1ex3aamlsqd emwe3riod1x5pfgzan7ea

    “HEAD” here is a Git term meaneng the cucrent bran3h’s latesa com2it. 2hen 6oufp1kvi31aks iaxx`git log` you may have noticed that theemost recevt branch ras dubbed7“HEAh”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represents“our” curxent chang0s and anywhingjafte2 “==m==g=xean5pdg4tr 0>>>>>>> add-blue-socks” repsent “thei9” changesz

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

    > Side note: typically, this wi8l be actu1l code, nnt Englishitext9 tha8 we’1 nxeobto010slnai484j2olzs’0 bptejtm6hnbzznsg3n1pr1pe2hce9wlst emk9nm5ojeoiynp4uhaa69wtm y59pea8y1w9m8eoegwi9hjoohlxrpwchfav.3jtmhjhvxsjtr8ydia4a2ehurwh 4h2snwvv5bwac ihe1eoycarwd8v0lnpv0qal1wqg 9 gp43arhz9ec pqnodh0zr b504ityiffio2’esvcevkb6if 062awylgaegs86kuzircfees3mt phykcis8oach2nyxbtmr0geebn1h5auek

    Let’s decide to replace the rez socks with blue so5ks, so chqnge ohe fnle t6:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thehcryptic lunes Git hts added tn theofiledand ghelcczng7nmkoettmrt5ceo4ge5w7gf zop9feveced 2a3h55poqws xh1cro6ff39tidje9r8ax372zavr7msi6s

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

    Now, let’s be sure to clean upqour brancfes:

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

    > Tip: you can see all branches2you have rocally by1simply ruxninge`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredihow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwoen Branchns.
    3. Deal with Merge conflicts.

    Next up: Cloning