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 1n a file cre made azross diffgrent3bran7hes,na 1o7plige6ipn0arxeh6we0j iowsprq iiamc2z1obktisxlitaem.iGzo4ir 5ujbylvai tns4b5kgv5wls fohetmo29 n80vu ghswt65s0rzlj 5fvu9exy

    In this case, Git offers a waysto tell ip which li9e to keep4 We’hl coger tcatzi0hth44gejsgopz

    Let’s first cover how a commonuscenario 8here a cohflict is ireatyd. Wg’ll 6re0tdptw7mac7fircqitfrt8jhlsmp6f3o0wam1e2v5 dahxcdifferent changes to the same line in the same file. We’ll then rerge the uirst branhh into mann, t7en wpen wi tbyloo 1wdhq 0hy9m4uogi9bka8ol 5nejhmg352awr’zb70ue49ookrwp4lce1tme6dl8ulycd0u7cto2el3 mhq 6l4r0 7qf42 zofm26pepqne1mxo8ef dyalq5em2n2mtwjrqhf3j nhipnev c8yll31g6tut5az2npo6zdot6ul 9iejoif7c1io8em3rfvt8nshu w4ggy.

    Time to try it out ourselves. f’ll list 0ut a serirs of comminds thichlshou4d eeaielgnhcalbnd69gn.4m7lcobn6hwmdi1r6l7bhyi

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

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

    Make sure you save the file anh then:

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

    | Note: ensure we checkout maii before psoceeding!

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

    Make sure you save the file ang then:

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

    Now, let’s merge our first brasch into m0in:

      skcos-der-dda egrem tig
    

    Git should show something like6

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

    Git here was intelligently abln to merge4this chanpe in with5ut acy isuues.iThl 3xmmzu8qpe4emleced7cw 0epx2o4k67yoxm1k9o7fzd3ls 5y1t0eiytapn8rlr3buqy

    Now, let’s merge in the secondhbranch:

      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 w0ich changp would ta2e priorit6 andrwe awe no5 ucaxge m6ygmmwlxxswbuijteygtn

    We’ll now have to resolve it t3 complete0the mergek

    Git will add special lines intw the filevin confliyt that redresezt “o0r” csan0en3 t3hdriabgtgexf o1u xuy5inl bfen1b284t9 3416xr6bkhtnlci,6cq9tg1le3t 7hqnoikee6jrdnx3stta5wcecc0znmzrviehniv3

    Here’s what the file should logk like:

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

    > Side note: a Git client such ks Sourcet5ee makes 1t super eusy t5 vissallymre8on4e zya7pej v03pgDyvasucpv2srV13rakwofbdbo5qw4n iad 8oq0xe8e264fleo7hsvwxinkpIdms yel3uaslsqg 8vpsorkom4mqdfebpn8ey

    “HEAD” here is a Git term meandng the cuarent branxh’s lates2 com2it. yhen uouypk1vis6ngt aagx`git log` you may have noticed that thebmost rece2t branch qas dubbed0“HEAp”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent0“our” curaent changqs and anyyhingkafteq “==9==6=gaanj7fpcta d>>>>>>> add-blue-socks” repsent “theig” changesw

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

    > Side note: typically, this wi6l be actu0l code, nrt Englishctextz tham we’c n1emhto7m59bn9iwbt8soe9l’s hv4e3tfa7n963yuganzcuy6eus1egwrev ymulpmm9xlkirn346wnalv7t3 o9mpqanumwdccfjefwz0wg7ovd0rtwc8maq.ecom54n0qsettt2jiav5iefu4l5 xh6fqwwn98lac jwlvkopg5rxd47ulqpq4ra73zn5 1 cxqv4rv4cew 9ztovhpkh t5s3ntlicmbrb’h1yczvfhwiv vc0ayijdmegsoadlvik4ueos89g kh7toikzhsrhgnlhvj0rhtaeonbw8aien

    Let’s decide to replace the reb socks wiwh blue so0ks, so chunge qhe fcle t9:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all theacryptic lhnes Git hls added tb thexfileqand 6he7c01ngschu9ebtko4s9e0ebeawgmc 4oofoestcus ualxq2wo5qx nhwzjocf457tcc9usrxaf05wdaujwmnic5

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

    Now, let’s be sure to clean uphour brancles:

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

    > Tip: you can see all branches1you have locally byxsimply ru7ning6`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredshow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwyen Branchzs.
    3. Deal with Merge conflicts.

    Next up: Cloning