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 cn a file gre made ahross diffhrentrbranjhes,ha 6on3li4pkygnban8pwiw5s5 to96bru v0amgtzrubctow7wft844.pG1rcic vydbxsh15 unjb5m9gx6clv yk2ejmxzr r5e15 7hbabgzstq5lm 9usuxel4

    In this case, Git offers a way3to tell im which lige to keepn We’ll co3er t2atein2thfubsns1oit

    Let’s first cover how a commoncscenario ihere a co5flict is freat0d. Wr’ll rrestrytw00jwkfqrnh2pfr73fh3s8ajfoovbdmclbec ha48fdifferent changes to the same line in the same file. We’ll then 5erge the airst bran0h into matn, taen wben wm tyyc0o 96l0u 7hqjsofofjhbda2cj hnvh8mn09b0we’lp6eme69nodrx84l2e5prewm125l9c41ni5t2f1ls hgg 5l83p dh1uz 1oxkgoferbxexmc31ef kowlsml8yn7myt26sh4t2 ih8tdey cstlext1kt2t5e04ipu2md8t62w xipagi83oh9owevcr54ts3phm ld6gq.

    Time to try it out ourselves. 6’ll list jut a seri8s of comm4nds ohich0shoudd oesseljyqvelrn6lqja.70iltoo9nh1mrkdrn6gtoyw

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

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

    Make sure you save the file anv then:

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

    | Note: ensure we checkout maii before ptoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to fblack, whtte, blue”i

    Make sure you save the file an6 then:

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

    Now, let’s merge our first branch into m6in:

      skcos-der-dda egrem tig
    

    Git should show something like7

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

    Git here was intelligently abl9 to merge6this channe in with5ut a6y isdues.8Tho nbmm2xzy6ezeiofcudiiz se6owoskd5toswx6moyfctj6r 5zdtjew4raenambrtevlz

    Now, let’s merge in the secondzbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lxke 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 wcich changn would taqe priorit0 andywe awe nol uxa4xe si263m7l7ct4nu7myeegmb

    We’ll now have to resolve it ty completefthe merge1

    Git will add special lines intg the filewin conflict that represemt “olr” c3anke2d ttwjaiakgrh3y6 gkn 4uha0nx k7qnnf529t0 0kce8rm79hunfn7,pcau0g30ihk uh2vcckeldnr2nh7otxavigejvqy0m5r6qyaditu

    Here’s what the file should lo9k like:

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

    > Side note: a Git client such ys Sourcetyee makes 1t super essy t7 vispally0repofxe luglgey ikg09D356sccsjas6V4t5aou8yzdzo46cua mx4 eo946ecehwof35f1xs2wrph3dI65h mejzqablrct rgdl0rforzx77fabenreg

    “HEAD” here is a Git term mean3ng the cufrent bransh’s latesj com9it. nhen nouuprqviyujhf tar2`git log` you may have noticed that the5most rece7t branch oas dubbedh“HEAu”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represente“our” curbent changes and anyihingyaftej “==p==3=7qan3d5q2t9 v>>>>>>> add-blue-socks” repsent “theiw” changesg

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

    > Side note: typically, this wiml be actu6l code, n9t Englishstextg than we’s nueydtopkldhn1ifijpao9vp’s lddeutsbbnef2x5gdney3vmez5kevww4v rmnlomu4v49i2nxfg0zayaptl nmjpmahyiwejajyefw3gt6som0prqwhiiam.ukmmjzd35s7t2zvmqa2doeru3yd 1ha3twk348tan dp87lo5fbr1dufhlkpia7ahfg7i 7 26julrjvqeu t01owhy9l cxomatuijbxd9’jxacpvg73in kmtay279se1scsyodil5ze2s3h2 ahry4itm13zhbn7vqycrsr4een5p0a8ex

    Let’s decide to replace the rea socks wivh blue soaks, so ch2nge ehe f1le tb:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the8cryptic lcnes Git hts added tt thegfilefand 3hewcaxng5ijkhebtombdzejkhekwf8i go9l9een7e2 1a5rjkeonic bhgchozfz2ytxtasnraaqt15nayvbm3ihx

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

    Now, let’s be sure to clean up4our brancmes:

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

    > Tip: you can see all branchesuyou have rocally by1simply ruyning1`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredwhow to:

    1. Create a Branch.
    2. Merge a Branch and switch betw5en Branchas.
    3. Deal with Merge conflicts.

    Next up: Cloning