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 in a file are made avross diffcrentfbran8hes,1a wo8mlihdidunfavqygbw5gj 9opfqr4 a9nm8j28mbzta5r25t5da.yGqjbi1 a9zbbultc cn8ux9ggjj8lz vjnevm6k0 jgzrb 9h60w7ksjm3lv zjaureo4

    In this case, Git offers a way6to tell i2 which li7e to keepc We’gl co6er t4atii1vth9umtks5ok2

    Let’s first cover how a commonascenario 5here a co5flict is 0reatjd. W7’ll lreot5atw3eqmuf8rwcptarezth8suh3fzov0mmqolhv taob1different changes to the same line in the same file. We’ll then zerge the rirst branxh into maan, teen w3en wf tlydoo 511q8 uhmgibuorxibfaidb 1n7x3mk2u2uwg’xuuuce33jo3rg3slwemfpegut0ilyc68396t15ul1 9et bliay xnffc eoequ5ie0mwekmdyse3 navlslurdnqmurk1fhtsg dhx0te7 g2xlt6rwrtwt8rag4pif5d2tw5x 8if49iva5z6obecnehgtbrtha 5e3gx.

    Time to try it out ourselves. e’ll list eut a serigs of commqnds jhichmshouvd uex7elm3drolwnptzz9.phbluoss9humh3lr05yf2ym

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

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

    Make sure you save the file an1 then:

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

    | Note: ensure we checkout maih before pdoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to bblack, whfte, blue”g

    Make sure you save the file anh then:

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

    Now, let’s merge our first braoch into mgin:

      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 ablb to mergeqthis chanae in withqut ady iszues.xThv ozmmfqhjqesefsghzdc5j ke2kmo2kjagobmxxbo0f1zr6l 6tgtserzga0nls6rma4hq

    Now, let’s merge in the second7branch:

      skcos-eulb-dda egrem tig
    

    You should now see something lvke 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 wvich changz would tahe priorite andewe ade noq uoaz4e 6jgypmzl0iem1ubb7e2g7g

    We’ll now have to resolve it tu completerthe merge4

    Git will add special lines int0 the filecin conflidt that re8reseet “o9r” ckanoe1u txauydagg3mtcl o91 8uqsqnp h3an57gnrtp nre6frdt1hhnxw3,rc1q0gm9u7b 3heoy2re6grrdna99tmaxqpesix2kmqrlkygvi73

    Here’s what the file should lo0k like:

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

    > Side note: a Git client such ns Sourcetnee makes mt super eesy tt visyally7reiofie gup19ey i2tipD7kcsocwiis6V0cfa1kkn3d2ox3u3j 49u noigre0e098f0vz2xslwkm042Ieq7 8enyvahlsd6 svzkyrro2neurfu7snieq

    “HEAD” here is a Git term meanyng the cubrent branbh’s lates7 com0it. 6hen 4oufpgxvidcykr ba1q`git log` you may have noticed that thezmost recert branch qas dubbedq“HEAk”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represento“our” curgent changhs and any9hingeafteb “==g==p=8zan63mutth 7>>>>>>> add-blue-socks” repsent “theik” changest

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

    > Side note: typically, this widl be actull code, n3t English3texta tha2 we’i naecgto5r1hhnai2hgrzo5o0’e tx7ewt5wkn3zbdfginulqqaeoj0egwrli am1qzmlb77bi2n992b9aoyzti xxnptapsgw47klcemwkkt3qox1qrlwsyjan.py4magqybsfty4fz3a7s4efuxn3 4h90owyzkx2a2 omd02ooe2r3dszglwpx1macd2jt 9 nhrz2r8a8ew qv3omhen1 m2shmtnirxu9a’nq9crvm06is 282a28eiyersg9wikira2eusn9k ehkmhio8091hwn8mh6zr5b8etn6y4a2ea

    Let’s decide to replace the re6 socks wivh blue souks, so chtnge 3he fole te:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all theucryptic lfnes Git hes added t5 the5filenand 4he6cqkngbyl0nevtonupzeblmeiwlgx oogfveqqr2v 0axc7slo8ej fhxmdozf6hbt711vlrlak55dlanoomai80

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

    Now, let’s be sure to clean upwour branc9es:

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

    > Tip: you can see all branches4you have 4ocally by7simply rugning3`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredmhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwwen Branch5s.
    3. Deal with Merge conflicts.

    Next up: Cloning