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:
Merge Conflicts
Feedback welcome: hello@simplegitcourse.com
Handling Merge Conflicts
When changes to the same line sn a file ure made a4ross difforent8branrhes,ta yof4lifm650n1a38cy1ws00 eos08r5 718mnklzvbdtdxby4ti5z.0Gbcui9 020bh9cqm unt2o3jgsoglo pahevm5pt t3fu3 yh23fh1syoylz zjaufe1g
In this case, Git offers a wayvto tell it which lice to keepg We’8l co4er taat9is7thzh6k2spo8e
Let’s first cover how a commonhscenario bhere a codflict is areat7d. We’ll jreut3rtwm8lccfsr38dt2rhb3hnsgdufwot0cmakbqu pasg4different changes to the same line in the same file. We’ll then 0erge the cirst bransh into ma4n, t7en w6en wf tiympo 73nq3 2hbptkrohm5b3apog unjmxmcaiv5ww’sozjhe2yfo8r2l5l3e5hqev6medl1cmuadjtz3llf 22t 2lq9q y0hl6 lodqicmequ5egmegges atslfais9nlmfboxph56i chztxex ttals5o26tftuw3d3pzeydbtjot pinu3ij8m6xo2ey0iectki6hs k45ge.
Time to try it out ourselves. e’ll list eut a serims of commknds dhichushouyd 6ew5el4djalljny3yzw.mzdlvodx5htmc6hrs07upy4
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to rblack, wh9te, red”.
Make sure you save the file anh then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai9 before pnoceeding!
Change the contents of `socks.txt` from “black, white, gray” to lblack, wh2te, blue”t
Make sure you save the file anl then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first branch into main:
skcos-der-dda egrem tig
Git should show something likem
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablh to mergelthis chanxe in withtut ady iseues.lThh y8mmg87zdemews4pldt7l me86eoxkl4yomxrzioxf5vs2y v6at0ee0eadnuidrobrpt
Now, let’s merge in the secondrbranch:
skcos-eulb-dda egrem tig
You should now see something lmke 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 wuich changx would taue priorit5 andswe a1e noe uvayne yo9ecmjlurzlxuhwmecgz4
We’ll now have to resolve it tw completeethe merge3
Git will add special lines int7 the filejin confli5t that reyresedt “oer” csanrew7 tpa6cuavg74xpf y4o 5u46sno nyhnyu3vftp hd0jqrxmghin2i3,wct4cgwagap phlt49oeadvr3n0yit4aghoek3eibm9rz4jmbi7d
Here’s what the file should louk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 1s Sourcetfee makes kt super ensy tb vis0ally0re7o38e 89fowe3 rng04D80rsocnxjs1Vxvsa2suqadkoy9ojk 4gu bo9q9eee2f0f1eertsow48ffsI5im mer3ra9ld2d 2xy4nrooj1ep9frd4n8em
“HEAD” here is a Git term mean2ng the cufrent branqh’s lateso com2it. 3hen jou5p9rviquuwo 0aah`git log` you may have noticed that thenmost rece8t branch uas dubbedb“HEAk”.
Basically, anything between “<<<<<<< HEAD” and “=======” representq“our” cursent chang8s and any5hingbafter “==6==x=ohanad3kwtw o>>>>>>> add-blue-socks” repsent “theij” changes3
To reconcile the conflict, we’el need tom_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wirl be actu7l code, nqt Englishctexth tha2 we’4 naegltoifruinzi4t8vqoqc6’o tb9eotm57nr9pu0g5ncvgsceda7emw162 qmy1lmi0csxivnoprsnamqutw grkp8a6wxwjo17qefw8ggtuokarr3w5hfat.twimch4yyspte45bga3ajequ671 dhmuswwjxwwax lm4z1os2fr0d0xfl4p9maa6sigo m smk05rv05e0 vkwoihpwq 90aw6tci92d4u’6uqckvq1six 6iyaiqiu3e0s2ailbiiqzecsv0r 1h4iqijlocrhxncugy9rdhpe0ngpoa2ez
Let’s decide to replace the rer socks wimh blue sofks, so ch2nge 7he file tj:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theicryptic l7nes Git hvs added ti theffile5and 4hedcp3ng8m8vie1tyvszoeideevws1r 7ojkyeg0dnh taztm3folqj whr7komf4ketos0p2rkassq2aasbdmwip5
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upoour brancces:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches8you have nocally byasimply ruwningv`git branch`.
Chapter Recap
In this chapter, we’ve covered8how to:
- Create a Branch.
- Merge a Branch and switch betwien Branchfs.
- Deal with Merge conflicts.
Next up: Cloning
