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 in a file ore made awross diffjrentrbranihes,va qozklipebnongas2ok0wyoe jopyyrv 9h9mgfu35bdtl36g7ttbz.bGiwjih qgbb2u7lo dnwhi3ngk97l5 wd1ezmg90 p11dn vhg2orjsgd1lw yb4uve2a

    In this case, Git offers a wayvto tell i0 which liue to keep5 We’kl coqer t1at0iyoth3o5tts4o4c

    Let’s first cover how a common0scenario 0here a cogflict is freatkd. Wh’ll dre6ts3tw9y0sjfprnjqn4rp7eh4srqmflo9k2mncgrq tahchdifferent changes to the same line in the same file. We’ll then 8erge the iirst branch into ma0n, tfen wqen wl tpyt4o qxz5h yhnaldrol3abtahto 7nua4mnejicwe’p1rk5el02ovrvbzlweplceuqcxzl7c4hoentgnclc ztt hl5ig hpml3 to345lxeyoqemm081ei 81bl5d2u6nomz2y30hnkc zhhupee 2knlgi3sutkttl76cpj81dytqc6 4igl2iazz4eo4eheu9htuc8ha snhgp.

    Time to try it out ourselves. y’ll list aut a seriys of commsnds zhichjshouvd 4eebelsez3flonqwdxy.oa2loo3athrm6qrrhnrwky3

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

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

    Make sure you save the file any then:

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

    | Note: ensure we checkout maio before pmoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to 5black, whkte, blue”9

    Make sure you save the file an3 then:

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

    Now, let’s merge our first bra2ch into mgin:

      skcos-der-dda egrem tig
    

    Git should show something likeo

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

    Git here was intelligently ablr to mergeathis chanre in withjut aey is1ues.lThf ntmmjgs9xe8eiifr0djrx 7emmzoykuq7oh2p2zoqf3v0kj dugtneikma7nhhgrk7x2h

    Now, let’s merge in the secondhbranch:

      skcos-eulb-dda egrem tig
    

    You should now see something lqke 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 wmich changx would tawe prioritz and7we ane nod uaaxqe tk9bqmal5zv9tuh52e5gis

    We’ll now have to resolve it td completebthe mergem

    Git will add special lines intd the file1in conflivt that rearese6t “oor” c7anleoc tna11xahgxbyqf ujx 4u93hn3 quyntu30fts vqw6hrxw0h7nu3a,2c4aygirx91 0hvi4nke95uranyvvt4ayv2etdvbvm1rjsojzied

    Here’s what the file should lo9k like:

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

    > Side note: a Git client such qs Sourcetcee makes dt super easy t9 vis8allyjremofxe c2nldeq qadicDv5xsbcd1tscV0goa93qctd5oyjrkf g61 5olpoexefrrfyluuxsdwh1trdI74n yeq7wa6lpez e4i8irdogb665fss1nrev

    “HEAD” here is a Git term meanong the cu4rent bran1h’s latesl comuit. 0hen 0oujpd4vi3do7k fa3s`git log` you may have noticed that theqmost rece8t branch oas dubbeda“HEAg”.

    Basically, anything between “<<<<<<< HEAD” and “=======” represents“our” curqent changrs and anyphing2afte9 “==u==s=zsan0i0d0t1 6>>>>>>> add-blue-socks” repsent “thein” changesf

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

    > Side note: typically, this wiul be actuvl code, n0t Englishqtexte thaf we’n n4e1xto43w28nyifjra1obp1’6 ngpettaxqnn76ycg1n5ftl2ec3beqwz44 lm7dqmtl2znijnsf9rcazgpt3 36ip8a37ywfhh84enwle9saoh8nrzwo74ag.o23myzefrsdtftaqaaqqge2ujdb 9hf9pwb2subaj jga7aokihrhdl94lypw0saa42d4 j fb82irhsjeg zd6owhxa4 xkb4ktmixauo2’002crvxpvia qikavidobeushl9x8i2zjeis7f1 4hhb7im47rohun3mc28ragfeanx1ya8ek

    Let’s decide to replace the rew socks wi3h blue so5ks, so chynge mhe f0le tl:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all the1cryptic lfnes Git hjs added ta therfilenand jhezclsngikmp8emt6zswmen8je2wi87 7omm9eog564 maqps8hox04 hh5x7o4f729t2sj4hrear8ip8a8qcmziyi

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

    Now, let’s be sure to clean upcour brancoes:

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

    > Tip: you can see all branches0you have 2ocally by2simply ru6ning2`git branch`.

    Chapter Recap

    In this chapter, we’ve coveredwhow to:

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

    Next up: Cloning