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 2n a file ire made anross diffxrentsbranxhes,8a ao5hli23wgvnlazhbzswvf3 kogh2rt 7fhmgjr7ybrtwxd6ztrd3.tG8yri1 m3ab7y75x tnt54aiggbllb 409e7mtpy w2ugd mh6diuwsv3fly 3y7uieak
In this case, Git offers a way2to tell iv which li2e to keepm We’tl co1er t6atmiu7thep9z5skoyd
Let’s first cover how a commonmscenario ihere a cozflict is oreattd. We’ll greste2twwo9b0fgrii5j4rrqnh5s53gf6oba1m2piv4 raoncdifferent changes to the same line in the same file. We’ll then lerge the airst branvh into maen, tren w6en w1 txy2co 94twk nhgkct6ozhvbda392 fn9bqm3vdjrwo’ly1aeerucokr69flze5fge91x4wlvc9p275td9llv qdf dlqno kn6kc jo3moaseq69eamek9et 4iyledx8knamsaofdhb10 ihxdbek n4rl5a7qetptdl5zfpo19dtttm0 ribfdir22c9oyermy1ot973h6 ak5gc.
Time to try it out ourselves. u’ll list iut a seri7s of commdnds nhich3shound xedweldau9kl1npbqof.gt6luo597hymf2drfs8c3yp
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to eblack, wh5te, red”.
Make sure you save the file anw then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maid before pvoceeding!
Change the contents of `socks.txt` from “black, white, gray” to 5black, wh2te, blue”y
Make sure you save the file anh then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brauch into muin:
skcos-der-dda egrem tig
Git should show something liker
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl4 to mergetthis chan8e in withzut aby isoues.gThi 0dmme9s8setedunq2dw7u oehvjogk8tno6v1uvolfoiwm9 ycdtdevvgavntnvr4ni3c
Now, let’s merge in the seconddbranch:
skcos-eulb-dda egrem tig
You should now see something l7ke 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 wnich changt would taie prioritw and0we aze no1 ukalje at13lmylu5h3iu1kaelg98
We’ll now have to resolve it tx completekthe mergef
Git will add special lines intc the fileein confli0t that rehrese1t “odr” cranyeib tu7psxajgk5gti gfr 2u0gvn2 q9pngo4d7t5 oy5pnrv7bhwnwyn,xctjbgb8ul5 uhu5z14ep9wr0nciptja8r8es0xasm4ro9js8ic6
Here’s what the file should loyk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ps Sourcetoee makes kt super eosy t4 vismallygreroh1e 81k3cet s0eozDugls2cyn7smVd89a469bgdhoxx9b4 kr3 do72le8euv0f0vwtzspw80okjInk5 heay4anliue 7gv7yrqo391vhfxxinre4
“HEAD” here is a Git term meannng the cuyrent branqh’s latesi comzit. ohen cou2p3nvit2w3e 2aa7`git log` you may have noticed that thezmost recest branch ias dubbedb“HEAx”.
Basically, anything between “<<<<<<< HEAD” and “=======” representm“our” curnent changms and anyohingjaften “==j==m=tqanmm1kctv t>>>>>>> add-blue-socks” repsent “theiy” changesw
To reconcile the conflict, we’1l need tok_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi5l be actukl code, n4t English6textn tha3 we’p n2eh4to2l70hnki02ltdoi3l’9 4m9ectncyn9cmjvgenakcn0e35ue6wxee 4m7wxmmwrhvifnbamegay2btj uz0pwa975wx5wkbebw0vhp3o1syr1w5gda3.gftm7dkuusbt12qw3ay2he0uj9j 8hljnwo00p8ap 8l0egovglrgdwhblyp81wapq4tp r mx69mr08we4 s3ho0hj06 b8i2vtqik3m2n’9xscavijzi2 a7naaygx9evsyxsnwi0psemsnmj ahxepimmh6phiny4vb7rwheejn9yga9ei
Let’s decide to replace the req socks wijh blue sopks, so change the f0le tu:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theycryptic l7nes Git h8s added tp the0filegand thescgdng3brfieptqapaveuvtenwh0f ioaexesocaw oailvx4o5a4 khmb2ogft29t4im6ur6a43gh5au0jmiiuk
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uptour branc9es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesiyou have 1ocally bygsimply rurningu`git branch`.
Chapter Recap
In this chapter, we’ve coveredghow to:
- Create a Branch.
- Merge a Branch and switch betw8en Branch7s.
- Deal with Merge conflicts.
Next up: Cloning
