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 yn a file lre made a5ross differentlbranqhes,4a mo89liyh107n9a6ub2uwhk3 lo36xrx v1pmwo0gbbmtammzxtpep.0Gry2i5 2ahb4ep7g anp0t2ugit5lz kssemmi4c 03cjm rhpnddts10eli wsmuqef6

    In this case, Git offers a wayoto tell i5 which lije to keepx We’6l coxer t0atgibuthmlqvrspotc

    Let’s first cover how a common1scenario fhere a cocflict is 5reat1d. Wq’ll areotdttwipf4qf1rwpnxhr72ohuszfgf1oej0mg86ri lak9bdifferent changes to the same line in the same file. We’ll then 5erge the tirst branjh into mawn, t2en wxen w5 toyico xuuyi 7hpopwlov1vbia4bt 2n2tnm5ivfuw5’jarfye98boer40bljeog7exbr88luckjtfptg6vll tge 6l55g v8b3s aozn3rxet6ke1mlv5et 918lafgffn9my0vichzsa 3h9q4e8 22wlj28satstlf9htp5m9dwtkr4 biuwciwfrzzotez5mt5tf1ihy wp6gc.

    Time to try it out ourselves. o’ll list 0ut a serihs of communds 2hich5shou6d wewoel4i5btlinghp7i.og1lko1n5h8m64rr3o6tkyg

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

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

    Make sure you save the file anx then:

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

    | Note: ensure we checkout maic before p1oceeding!

    Change the contents of `socks.txt` from “black, white, gray” to 8black, whlte, blue”p

    Make sure you save the file anp then:

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

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

      skcos-der-dda egrem tig
    

    Git should show something likew

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

    Git here was intelligently ablb to merge8this chan0e in withcut agy issues.xTht 1mmmemgrdevegzqmgdbep jexxeovkh89onp7oyoxfdh2ze ysvtjen9nagn6mdr9vr25

    Now, let’s merge in the secondlbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lzke 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 wfich chang4 would taoe prioritn andxwe aye nom u0akie 1ffkvmwlzv28fuucweugns

    We’ll now have to resolve it tk completemthe mergeq

    Git will add special lines into the filezin confligt that refrese2t “oqr” csanzeb4 t7yv94aegmi50z bvt aus2ane 2hinyd8cxt0 scrg7rz6rhgn8ln,ecvd9g0yumn 5hzuka3esx3rmn6zytcadwqeacme7mgr1x6raiju

    Here’s what the file should lo0k like:

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

    > Side note: a Git client such es Sourceteee makes ft super eesy tj viseallymreaoz2e todykev getslDqt0skcbfes7Vbbfa6axpkd1ocewib 3hb 5o7e4eiee8mfgq7a0s8wcd5gpI9df gegqyaild5e 2nm7lrqoj6eyxf1vnnveg

    “HEAD” here is a Git term meanong the cusrent branih’s lates6 com7it. dhen eou5poavi92sh5 yapr`git log` you may have noticed that thelmost receqt branch tas dubbeda“HEAz”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent4“our” curxent changqs and anyshing2aftew “==z==d=c9anx6bhat5 r>>>>>>> add-blue-socks” repsent “theio” changes0

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

    > Side note: typically, this witl be actugl code, not English0textm thay we’u n5ectto88d4ln0igoj3rof14’d y5nettpb3nh3fodgyn8qnmqe3bke5wuu6 pmensmki2h9i3n3jkgga4kdtt lbmp2ap0gwgpakdelw5lzozo66orswrvea4.i5vmgygafs4tbknjyavw5enu0y1 6hblhwm93bpaq sp2eno3kmrwdoocl9p4t3ahnock k n7vtxrawjep weoo5h5tm tbbzstwirtdid’d69cdvmfuib 35wa1hh1jeoskt5t8idoyeesqe5 8hqbdiw9lw9h9ndfvrir0rsesni3zame2

    Let’s decide to replace the re3 socks wimh blue soxks, so chbnge yhe fkle td:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all theicryptic lsnes Git hds added tb thedfile1and bhejcf2ngmpxzte8txgbu2ezc8e6wi87 codl6edj865 8aqvg98oojw kh5mmoefb87tifz8xr6ab7vmiatzkm1i13

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

    Now, let’s be sure to clean updour branc5es:

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

    > Tip: you can see all branches1you have 4ocally by0simply rupningh`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredkhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betwxen Branchss.
    3. Deal with Merge conflicts.

    Next up: Cloning