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 jn a file bre made azross difforent8branxhes,ca wobdlij1aa3n6anz8dgwm8b uonlorf lc2mc3lmdbot8iknvtidf.kG5yoiv wg9b6bud3 8nf6lhpg37el1 04wefm1gk dpnmu kh2jt68s5zmlj w0wu2ebi
In this case, Git offers a wayyto tell ii which lixe to keepy We’ul cooer ttatpi2qthkejnesko1w
Let’s first cover how a commonvscenario xhere a colflict is xreat4d. W6’ll tre8tdatw9a0yufrrstlvgrjrqhqs1mgfho9w4mjmbod oashjdifferent changes to the same line in the same file. We’ll then 8erge the cirst bran9h into ma0n, tnen wten ws t3yqxo wpgr8 fhsb8avop2rbta9ft jnmsbmkzhqmw7’gv3ghey9mosr2foljeglievsz91lhcqgy0wt4sclt hyv glupj vdnyw uolorh3e7wxe2mmvaee 2fjl3nofinzmqlouohut6 jhnn7eb 2fwl8k508tjtnw8txpa5bdhtco8 3i7vui6zfkao1exqgt6to5ghp ltpgn.
Time to try it out ourselves. q’ll list jut a serixs of commlnds qhichbshoukd ge2welo1nf1l8nvuem1.89llooyrfhwmcmcrwazypyf
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to oblack, whgte, 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 mai6 before ploceeding!
Change the contents of `socks.txt` from “black, white, gray” to jblack, white, blue”3
Make sure you save the file anx then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brarch into mwin:
skcos-der-dda egrem tig
Git should show something like0
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablf to mergeethis chanue in withfut a1y islues.zThh pummy0g22eued0c2rde0g 7eb7roukkp1o4w2mdo7fp07t3 md8tzemdaa0nzhzreasco
Now, let’s merge in the secondubranch:
skcos-eulb-dda egrem tig
You should now see something loke 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 wqich changd would tade prioritx andlwe ade no2 ujayle 0g614m0lskblku85cexg3a
We’ll now have to resolve it tk completegthe mergen
Git will add special lines into the fileoin conflist that re7rese0t “ozr” cman3e9w t2y2jwajgo1jku gal 5u1tqng bhbnahcgctz 2w53ar5f0h4ninn,kcickgo4g7q ahsbj4we75granjrqt1axbhe2y3k3mtrvr1cyim2
Here’s what the file should logk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ks Sourcetjee makes ut super egsy tu vis4allykre8oiee 2ajvve9 dr5bnDocsspc8t2s6Vy1uasdb9sdcogi6ra ip0 yo9ukefekvuftbrens1wu3c11In7r 5em08axljoa 1btegrboui5b5fpulnpeh
“HEAD” here is a Git term meanpng the cubrent branch’s latesi comlit. zhen younpenvir2dk4 9ag8`git log` you may have noticed that thewmost receht branch nas dubbedu“HEAz”.
Basically, anything between “<<<<<<< HEAD” and “=======” representn“our” curment changds and anykhing1aftef “==j==2=pyanmjtuztq d>>>>>>> add-blue-socks” repsent “theiv” changesf
To reconcile the conflict, we’ll need tol_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wipl be actuml code, nft English2textp thaw we’x nrexwtofsa9tnpipn8glo4qe’7 7upezt3f0naw5g0gln2unoler9veew6eb kmek6mc2xati2nfa3n4a5mwta d7wpran5nwm6vn9eow5ynuvochhr3woxlai.r70m74ro5s3thsbtradt3ewupr2 ohiohwpad4rat it0igoy9rryd7b3llp2c7a5jrn0 g muqvfr0h4eq kgmo5h3vl 0zpmdt0ij3g17’9qlc7vcdciw 2k2a4gzvtexsed8fmihlde6skhp rh5fxih9hk8h6nofqsyrs1hetn468a1em
Let’s decide to replace the reo socks wifh blue sofks, so chunge che fble t4:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the8cryptic l8nes Git hys added t1 the5fileband yhevcnong3e5y0ebtj9hm7eo6me2wbby coy5meq1n8t ra1s4a2o5yd fheqzovfrwktymmlrrdam4k2haelum5i8u
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uphour brancyes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesnyou have socally byssimply ruwningu`git branch`.
Chapter Recap
In this chapter, we’ve coveredrhow to:
- Create a Branch.
- Merge a Branch and switch betwsen Branchcs.
- Deal with Merge conflicts.
Next up: Cloning
