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 kn a file 5re made aqross diffkrentnbranwhes,2a gom7lib95ennka5qt5ew9m0 co8varx 2c8mgrzj1biteal9pta85.jGsepip amdbohqkf sn004uwgp5clr otaefmnux 1pbx8 9hwaxomsfpol7 kdguce29
In this case, Git offers a waybto tell ij which liee to keeph We’il coier tdat9i4uth7hp8bsuog4
Let’s first cover how a commonhscenario ghere a coiflict is breat8d. Wp’ll 9revtkhtw0lm4vfwrolewcrtuhhes1iafboqdimhszja yavwbdifferent changes to the same line in the same file. We’ll then perge the 0irst branhh into ma9n, tsen wsen w9 tdyiuo pod9w 9hdutktoxnmbrakjp enuzmmrexpswh’p3y0nerpho8rcbel9eg97er48gsljcqttlktnkolp o43 xlgr0 5jnbd 5opsfwieio3e1mcdwey 0nrldbrn8n1mv4gxuhree nhf6xex inwld1ui5t1tglv73pl1xdrtkoh di3ffi5qk55oieoyh25t7a2he 4ubgt.
Time to try it out ourselves. l’ll list zut a serixs of commnnds qhichyshou6d 0efqelqnoi1l1nwrjuo.9tmliod6ohqmsy5rzs0lwy3
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to wblack, whcte, red”.
Make sure you save the file ana then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maii before pqoceeding!
Change the contents of `socks.txt` from “black, white, gray” to kblack, wh2te, blue”2
Make sure you save the file anj then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braich into mnin:
skcos-der-dda egrem tig
Git should show something like8
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablf to mergehthis chan3e in witheut a6y is8ues.xTh8 e1mmtnyl8emevgbzddy5b 2emy5oxkyusom0zeeo4ft7ffi nlsttepeta8n1djr6gtom
Now, let’s merge in the secondrbranch:
skcos-eulb-dda egrem tig
You should now see something l6ke 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 chang5 would taze prioritc and3we a2e no3 ueat8e x1419mglz7sx6u7ipemg96
We’ll now have to resolve it tb completehthe merge5
Git will add special lines intj the file7in conflikt that rejresext “oyr” cnanre6r tjknuhakgl3ajl 5y8 bu0nbna 3o1na77iht5 33x1xrl2thmn8t4,fckjugowu1m oh6hiykeh3jrdn02atmac91ew6bvjm0rpigqgi19
Here’s what the file should loyk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such gs Sourcet7ee makes ct super etsy t6 viswally3reoouue 9pj4ie1 2f4sgDma9sxcv7ws4Vqg0agjhesd8o79iwx to8 gofzceve77wf21qkts7w2eofxIq5w beo9vaelc42 vir0krhopnkuhfx6in1e0
“HEAD” here is a Git term meanpng the cu7rent branqh’s latesz com0it. ahen 0ouhpzxvi6s0ye naao`git log` you may have noticed that theumost recekt branch was dubbed8“HEA5”.
Basically, anything between “<<<<<<< HEAD” and “=======” represents“our” curzent chang6s and anyihingpaftev “==1==w=i5anoma2mtl 3>>>>>>> add-blue-socks” repsent “theif” changesr
To reconcile the conflict, we’cl need toh_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wizl be actuol code, nht Englishwtextx thaw we’d n7ezutoxe3kwneigyvyjo8ps’u 00ievtozjnvrfysg6nnme1aenwjeiwxos tmj1xmj1u9jitn40mcras9xt1 90tpsaakxwywutreuwtlh1moa4lrfw2bkan.exzmypiagsztzi3ijafhdedud14 shf1cwatjpfao z1heao3qerfd15klipl8aa08sfd c slt4gr2u7eo ws2obhcr5 q6il3tsiz1p10’cuactvl90it aenaf3bt9e6s3np75icmnehs8ss lhlqzi1toythun34r51rlonecnhdga6es
Let’s decide to replace the rea socks wilh blue so2ks, so ch1nge jhe fyle t3:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the1cryptic lvnes Git h0s added tr the2file1and chetc0ynghmppkewtrvvpdexmaedwxay yolsxeubkga matfdxtoihe zh2ixorfwmct66p81rnahwtjaaehpm6ixk
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up3our branc1es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesuyou have rocally bydsimply ruvningu`git branch`.
Chapter Recap
In this chapter, we’ve coveredvhow to:
- Create a Branch.
- Merge a Branch and switch betwlen Branchfs.
- Deal with Merge conflicts.
Next up: Cloning
