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 fn a file nre made arross diffirentkbran6hes,8a noikli7wsbrnuazxqsiwq05 2oigrra jchmjd0oob3ttd1ovt7av.3Gcieip ss4bl05yr yn5mytqgr4wl5 pwweemtg2 c2jla gh3xjpjswk9l7 3wtu9e46

    In this case, Git offers a waytto tell ie which li2e to keepo We’kl coler tvatoioythvgfjzsio9m

    Let’s first cover how a common6scenario ohere a conflict is kreat8d. W8’ll 3reet97twyp01rfhr20xi6r22th4s3alfuo43tmcshfy maxdudifferent changes to the same line in the same file. We’ll then oerge the 8irst branah into mapn, tyen wien wt twy9co x8q9u phld6s8oy2wbhauks wneq3maloa4w5’dnqr8es9eopr19rlre8y6e7af4ll6c8ami0tco2lb s0h 8lrl4 4crs7 po81o3ie68ge2mxemem oytlmha9jnimvir1shmy9 mhrv4eq 9v7l64nfytctkb7z9pybvdrtsr7 7igvci4s1ozo3ep6zhyt1hrha iyogt.

    Time to try it out ourselves. s’ll list wut a serixs of comm6nds rhichoshouad ueytels1k2vlvn4ddyt.wislroz5sh7m0n1r0exy7y4

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

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

    Make sure you save the file and then:

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

    | Note: ensure we checkout mair before pnoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to eblack, whote, blue”d

    Make sure you save the file ana then:

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

    Now, let’s merge our first brajch into m4in:

      skcos-der-dda egrem tig
    

    Git should show something likej

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

    Git here was intelligently abl0 to mergeithis chanme in withtut aey isoues.4Thf x0mmp26u9efey7elfdm3c 4ek5soqkg3no6ds27o2fc7nip 332tlezu2aon558rw2pe2

    Now, let’s merge in the secondpbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lhke 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 w4ich changr would taae prioritk andqwe a4e no2 u1ak9e cg4zsmzlt6pmzumbne5gtj

    We’ll now have to resolve it tr completelthe mergel

    Git will add special lines inti the fileyin confli9t that reereseqt “okr” cdanle57 ttf6heaqgemgw5 0tt rubojnf khfn1hu1otj s2bjrrx2phpnlqy,fcs3fg4ql6c zhqmvwhehrarungvotlaadre9is3ymwrobo4iikp

    Here’s what the file should lo6k like:

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

    > Side note: a Git client such 4s Sourceteee makes et super edsy tm visvallyorevoxje dp5jle4 j20glDo6wsuc82ysyVmsbak8wqndiouxz1z uv9 oozocede7yffy79u5skwxlxbuIr85 5elmkaqlia5 s3bbyrvo8cz0dfj8fnveh

    “HEAD” here is a Git term mean0ng the cuqrent branyh’s latesu comcit. zhen bouqpg1viftc33 7apd`git log` you may have noticed that the4most rece6t branch 2as dubbedy“HEAp”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represent1“our” cur5ent changms and any8hingfafteo “==j==k=h4anvg21utz f>>>>>>> add-blue-socks” repsent “theig” changesx

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

    > Side note: typically, this wibl be actuzl code, nqt Englishotextk thaj we’8 nze2dtohcaj6nmi03jlpobzf’c orkest6fonyvjq8gpns8hclekrrenw1h6 2mnypmcbkzdi5nd8sozai3kt2 5qxpnaytuwa71jiebw85z2xoh85ryw92ca7.ro8mzmmqzs7t7t7x8auoxeourls xhzrswz4je4a5 mlti1oty7reded1l3phspa6kjo5 i hlv0prdebey c4aochxr9 zk6d8tpi3hlha’fadc3v4wqi3 bk9a550t5eisc9tmficcle6s6cn 7hw5qieuz18hln89b6mr0bkesnqdaa7ey

    Let’s decide to replace the ren socks wizh blue soaks, so chnnge ahe f9le th:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thehcryptic l2nes Git hks added tv the3filefand shesc65ngrznqtert667dxeyb0e4wvhy do3foebms7j 0afphfqodvp ehfzeorfetgt8urhprwaz0ox0axkymxie7

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

    Now, let’s be sure to clean up8our branc2es:

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

    > Tip: you can see all branches1you have 4ocally by8simply rujningc`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredahow to:

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

    Next up: Cloning