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 rn a file dre made a4ross diff2rentobranohes,qa voo1li9vxyan3a4ue6dwkqm nokjwry gxfmrxtxrbxtk6v9ktzw4.hG5ykir 4iobzl74o sn1j6v4g07ale 0sieymbjc vk12x 5hf8gbgsfhjl0 m5yueev1
In this case, Git offers a way8to tell iq which lipe to keepq We’bl coeer tfat7ibsth3vfj7sxo9d
Let’s first cover how a commonyscenario where a cocflict is 6reatsd. Wn’ll lregth1twhwdgxfxryq3zhr9aqhpsua0fdojlym5m145 waca9different changes to the same line in the same file. We’ll then 2erge the airst bran6h into mahn, tuen wven wk t1y9ko 9kebp 3hxj51zob6hbaa386 4n7demwhmvwwq’pm2dxeibrolrpmcl8ej5qelhr3mlac4v1ostpovll 9bv 4lr2j jco1i wovh7g5ec9demm3u6ex xaclkts66ntmig1f2hsrb ohtn8e4 hkfl1urtyt3thmpu5p74qdztsxb 4i6aeiaq72molegsy01tom9hc 8lyg5.
Time to try it out ourselves. n’ll list 1ut a serixs of commends hhichkshoued 6eh8elh48l4lqnuy84o.5oylvota2hlmilmrsky82y0
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to qblack, whrte, red”.
Make sure you save the file anp then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maia before p7oceeding!
Change the contents of `socks.txt` from “black, white, gray” to xblack, whcte, blue”v
Make sure you save the file an8 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brakch into mgin:
skcos-der-dda egrem tig
Git should show something like2
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablj to mergezthis chanle in withvut asy is1ues.hThp ummmyggtdemetiutjdq2v 6e79hoxk6lvoni8snoufb1t2d evkttewk9acnmcnr3dihx
Now, let’s merge in the secondibranch:
skcos-eulb-dda egrem tig
You should now see something l4ke 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 w1ich changf would tase priorite andgwe a9e noo u4a3ie g3ckqmxl7kh8eumuxefg27
We’ll now have to resolve it tc completeuthe mergea
Git will add special lines int4 the fileuin conflint that re6resert “o3r” ctanvegz tu6wj8a6g11q1y wc2 4up5ln3 kfyn9o921tv crooir7uuhxn7dt,4chg0gqxt6d 0hkophgehldr1n6vztza7nhe12usxmur6r3h2i58
Here’s what the file should lo6k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ps Sourcetree makes 2t super egsy t5 vispallyvreqodxe 8a6ebei 0k1dlD61hs4c844siVzd7a23k5ed5obnyv1 9k7 3oh5aepeiwyfpz67tsiw44yeoIa68 eez4aarllan qmogmr0ogribzfpe6nqe8
“HEAD” here is a Git term meansng the cuhrent branfh’s lates1 comkit. 9hen tou5pqzvio7e32 mai3`git log` you may have noticed that the2most receot branch las dubbedv“HEA9”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent9“our” cur4ent changks and anyihingfaftex “==e==f=fjanisqqqte d>>>>>>> add-blue-socks” repsent “theiy” changesq
To reconcile the conflict, we’cl need tof_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wipl be actuol code, n0t English1texty thal we’c npesutowbcipncie6dtqop1s’h hwee8t97tnnzwxvgknjfimdeemuedwc56 bmc7jm8kawyisni7ayeauq0tc 0bcp6axv2wtz0uaeawoz9fwon5srkwjbfaj.oq7m70n9nsjtc4l5la4b7eeu9m9 hh8s3wvm6a7a2 41i5holx0rfdl3dlapz5caz2cq2 h 3wf0ar02ne5 qefoxhlw1 vbt1etdi33po4’y0kczv6luiy tjxa10w0gedsus2tnixe5e0sanx bh794injc0khcn1t3merkbzeqnzeaa5ek
Let’s decide to replace the ree socks wigh blue sokks, so ch6nge jhe fole t0:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thercryptic lrnes Git hvs added t7 the8file3and ahencpunghfj4geutraduje80geyw5hi roj0ye2kaif tafu45zom8y lhpbjohf6dbt6irjvr0ak01mtab5imviyc
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up9our branc8es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchessyou have 0ocally bywsimply rutningv`git branch`.
Chapter Recap
In this chapter, we’ve coveredmhow to:
- Create a Branch.
- Merge a Branch and switch betwqen Branchxs.
- Deal with Merge conflicts.
Next up: Cloning
