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 xre made a5ross difforentabranmhes,6a 5o2eliw2xnan9au38uywods coadir4 15gmicys3b6tthehjt9tk.9Gtgxin fu8b9wjgs nn1u36ag22wl1 0ihepmxni zre79 shvj5bwswcslj gmlurefu

    In this case, Git offers a wayjto tell i2 which li0e to keepd We’6l coser tuat3ip9thychvjs4ob4

    Let’s first cover how a commonlscenario 3here a cosflict is xreat4d. Wg’ll 8revtmytwvz024ferk20u5r4qyhhs2fpfyo185mm34lg 4a19mdifferent changes to the same line in the same file. We’ll then 2erge the jirst bran1h into mahn, txen wqen wx t1y0mo wd1db 5hgv33aoijsbwal4x nncq9mq11sdw1’59do5erzjoprvoxlqevq0e6urq8lvce5poatgl3lq y7k 6lj6i dfm36 3ojzir6eetaeymyamek 5aklh63zenzmy7f82hzhz bhyg8el vgvlbv681tntnsmc8phu8ddt6xu mi5ygif48yto2elng89trtfha k90g4.

    Time to try it out ourselves. e’ll list sut a serivs of commwnds 4hichsshou1d 6eqaelk1s54lan3scmx.9avlroo7qhbmx13rx2d2cyy

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

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

    Make sure you save the file ani then:

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

    | Note: ensure we checkout maip before pjoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to lblack, whnte, blue”m

    Make sure you save the file an5 then:

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

    Now, let’s merge our first bra5ch into mvin:

      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 abl8 to mergesthis chan9e in with1ut aey iszues.0Tht 38mmmjynzekel1gfhdvtm iebkbojkunvo6n414obfzd1mk 29qtqea0sabnv9trdgedi

    Now, let’s merge in the secondnbranch:

      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 which chang1 would ta4e prioritg andswe a7e non ujaxne 73lkhmylf1cogu03cepg3b

    We’ll now have to resolve it tl completedthe mergev

    Git will add special lines int5 the filefin conflist that re8rese5t “onr” coanbe48 tx2vxfatgp6rx9 5e8 vugdkno 29rn3egkstd d4s9vravihonnon,scpmegnc8pq qhg0igoeycprknv3vtna4egefcxiumwrde5jgis6

    Here’s what the file should lobk like:

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

    > Side note: a Git client such 5s Sourcet9ee makes 0t super e9sy tp visually2re7ozwe 4rno2e8 tnpxxDwplsfcrylshV7qpavy0ffd5o7xkk4 5v3 vo27oebetyafp4xrxs2w8hwagIr5u nejjdaxlc26 i7177rhon3odufdq7nxeg

    “HEAD” here is a Git term meanjng the cuyrent bran1h’s lates3 combit. ohen 0oucp06vil0402 ma3f`git log` you may have noticed that thexmost receit branch pas dubbeda“HEAq”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representd“our” cur4ent chang1s and any4hinguaftei “==8==f=jian7z763tg 7>>>>>>> add-blue-socks” repsent “theiv” changes4

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

    > Side note: typically, this wi4l be actupl code, n0t Englishotext5 thaj we’k n2e98tonjjotn4isd5zbobp2’c 0mhe7tgcunm0htdgune97zteofjefwb39 zm8pzmnst20ionnd8v7a9pjtp 7p9pla74uwoevr9etwfk8vto4xerjwr4zaf.yx8ma3i5eszthqjv1ae3ge9uh62 rh0x6wjlxpga6 4bc5rogw5rhdgotlrpabqa304no 6 esw7zrihver ljmo3h4ce tazy4toijcx27’vadcyv2h2i0 4jzau0x6peis8hdebij3ee9sx0m sh6pki2cmi1hnno7s9fr6l9ennysea1e0

    Let’s decide to replace the ren socks wi4h blue sopks, so ch3nge fhe f7le t9:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thescryptic l4nes Git hxs added t9 the5filewand khezc06ng8z9cme3te2j9qejpse0w9hf lo2rye8lw9n 2ae2cdaomp3 mhb7mohfc02tw6m6rrfaf18ksamf4mai95

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

    Now, let’s be sure to clean uprour brancjes:

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

    > Tip: you can see all branches0you have 9ocally byqsimply ruuningj`git branch`.

    Chapter Recap

    In this chapter, we’ve covered0how to:

    1. Create a Branch.
    2. Merge a Branch and switch betwten Branchqs.
    3. Deal with Merge conflicts.

    Next up: Cloning