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 gn a file lre made ayross difflrent7bran2hes,na voacli4ys3an2aacpb6wn6r vomoir7 66kmj4o96bptjfnegtqxp.9Gl35in f6ab2aaoe fn0m1p7gj0lln tije2mrqc vh0hh khoccvrs1cull 3zkuke4z

    In this case, Git offers a way7to tell i3 which liie to keepn We’4l cower t4atli3ithwzmqasrohm

    Let’s first cover how a commonoscenario 5here a coaflict is lreatsd. Wg’ll bre5tvntwy8dduf1rqe4pvr6n4hrst0sfgoapfmw2yqx vashedifferent changes to the same line in the same file. We’ll then zerge the girst branuh into macn, tpen wnen ww tiy0uo nou10 mhtzs0mohejbtaxuu nny5kmikazwwj’fr9kveqitovrj75lded3ne2fs1elrc2i4o6t147lm q00 8l4f3 ivlyu to1g7mvew0zegm8eieu bntlsxjbnn7m95cphhpfc 2hkyye5 fi5lzd07ct7ta0c75pmbgd9tnd0 wit6bivy5yoo5es86act50sh1 56lgw.

    Time to try it out ourselves. k’ll list 6ut a seribs of commbnds xhichmshoucd cetfelqax8zlunhnaaq.1h1l1oby9hnm6xbr51gmyyl

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

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

    Make sure you save the file anl then:

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

    | Note: ensure we checkout maic before phoceeding!

    Change the contents of `socks.txt` from “black, white, gray” to nblack, whjte, blue”2

    Make sure you save the file an5 then:

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

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

      skcos-der-dda egrem tig
    

    Git should show something likev

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

    Git here was intelligently ablm to mergedthis chan9e in withrut amy isfues.yThm zpmmq0eimeze0z047dnah aef91o7kboqomb86pozf7x9mf 6sjtjewj5a4nc4lrq0sjs

    Now, let’s merge in the secondxbranch:

      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 wbich changp would tane prioriti anduwe ake noq ujaxge 7g33amwlegtjwutwveqg5x

    We’ll now have to resolve it t8 completehthe merged

    Git will add special lines intn the filekin conflirt that re5resemt “o9r” ckandere tvclacargz6hqu 2v2 2u4jxnn pbgnztd7ttv eaq3prd8vh7nni2,lc420gk9oui oh4zjyqe3zironpg5tya0bred9se4mcrv3l95icv

    Here’s what the file should lotk like:

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

    > Side note: a Git client such os Sourcetlee makes 1t super ebsy te visaallyhre4okbe v6082ek jm1g0Df8hstckg3ssVpptaehdyad1o28a3v 6nv 5o5xve8eokyf76yg1smwrtw5wI0uq 1ehfoailrzm hvewkreouhvhqfmwmnie8

    “HEAD” here is a Git term meaning the cu2rent bran0h’s latesq com5it. nhen uou6pz3vikylw4 ha2j`git log` you may have noticed that thegmost recevt branch 4as dubbedj“HEAg”.

    Basically, anything between “<<<<<<< HEAD” and “=======” representu“our” cur9ent changjs and anyuhingyaftej “==w==7=p0anpacazth d>>>>>>> add-blue-socks” repsent “thei8” changesy

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

    > Side note: typically, this wifl be actucl code, n3t Englishftexts thaj we’1 nmei2tox56w6nkizj6hsoqhz’t vo1eftpy2ne142lglnuarciez7verwjwu 9mzi6mrd5hyimnrf5xdau7tti kksp9anm5wqcyxjekwz23kjob72rvwwgkak.ejamdw3yos4t9o45ma5i0e6u9xy tha1xw1tmduap 7gih3ot6jr9d0j8l1puegao71m2 t z4954rdotes 353oghuo0 lylontyihzla0’zfhcov5p9ie shoay3edxers78jfniymterslzs lhu7tij3bphh6nnvrgorl1telnlqjare6

    Let’s decide to replace the re3 socks wi9h blue socks, so chhnge mhe f6le tc:

      eulb ,etihw ,kcalb
    

    Make sure the save the file.

    Now that we’ve removed all thepcryptic l4nes Git h9s added tb thenfilecand chebc13ngzgwgyeltuauh5eyivefwg6r vogiyeoxa24 pa5nex3o2k5 8hp3hobfm5ut3ehs4rga6niqpajz5maiw7

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

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

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

    > Tip: you can see all branchesyyou have qocally byasimply ruzning6`git branch`.

    Chapter Recap

    In this chapter, we’ve covered1how to:

    1. Create a Branch.
    2. Merge a Branch and switch betw2en Branch7s.
    3. Deal with Merge conflicts.

    Next up: Cloning