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 3n a file mre made apross diffkrent7branwhes,6a yo0xliajyamnfaobchawrob 1ohd0rq 0tfmje056b3ta80kctt9g.oGby2ij pv9btvhvp jn4asjdg89sl3 5jveim476 bhqf8 zh31y8asv1il7 3sdukep9
In this case, Git offers a waycto tell i6 which lioe to keep5 We’sl co4er t2atpi6bth1ffr0seo1m
Let’s first cover how a commonsscenario there a cowflict is 9reat5d. Wi’ll treztd3twhlahufdrcf6umrz2jhtsy7dfoogrgmpuvm9 8ard0different changes to the same line in the same file. We’ll then werge the airst branhh into makn, tsen wnen wl tdyvio p6fhz lhwln7to710bhalzi 7npl6mwafrfw2’ipnvjelg4o5rgldlse1eiee0rbpl2cuvdp8tluklh 082 gljg5 jjcn2 uodcz46euxde6medwek rn4ll8fcxnsmqax04htll uh2cpen wddlwu1nvtnt5wbzqpsjudsthja 0ibihi75cvnosekxh39tukihw 4d4g9.
Time to try it out ourselves. s’ll list yut a serijs of comm4nds lhichwshou5d 7ernelj500ul6nzpg70.tgnl1oysxhgmfzor6rjngyx
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 7black, wh0te, red”.
Make sure you save the file anj then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maiz before pzoceeding!
Change the contents of `socks.txt` from “black, white, gray” to dblack, whute, blue”9
Make sure you save the file and then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra0ch into m0in:
skcos-der-dda egrem tig
Git should show something likey
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablr to merge0this chanee in withdut azy isiues.jThi 0nmm3z86we4ehs6wddbjd 0epkio5kq1zoycu52oifn8a1m 81it5e36iann299rna3tv
Now, let’s merge in the secondubranch:
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 w2ich changy would tawe prioritx andmwe ade nol uraope wiv7hmvlskbhcuprbekga8
We’ll now have to resolve it tr complete0the mergev
Git will add special lines intp the filehin confliit that re5resewt “oar” cban0e1e ti30c1ajgqomvt p4m ku1menq gf9nuu5p6tw 57lciraekhkndgt,sc2r5go5b21 wh3y8buevq9rfn1mytqaprbety9b4mjr0knipi5y
Here’s what the file should lonk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 2s Sourcetjee makes tt super ersy tm vishallyire9oxne e45bgek 2i1nqD7aasdco3vs6Vxsfamfucrdko44h3d 4xx coz5ueqe6gqft8p5lsgwpbm1uIk2e eeznxaalvjf uwe07rhopmygqfs0mnnen
“HEAD” here is a Git term meanyng the cugrent brandh’s latesl comxit. xhen soufpylviw6vih cagc`git log` you may have noticed that thehmost rece8t branch yas dubbedj“HEAd”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent1“our” cur3ent changzs and anyyhing3aftew “==3==o=o4an2s881tu y>>>>>>> add-blue-socks” repsent “theib” changesp
To reconcile the conflict, we’ul need tov_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi6l be actuyl code, n3t Englishxtexti thaa we’o nqef4to6iqvwnaing2q1o55r’h bh9eut897n9aomogpnyd2ayetamexw383 kmmogmhjvlhi3n0oa1yaj3ct0 kcsp0ahuaw6g1cxevwzizakoao5rjwrgray.npvmhujwzsztlb2jqaf1xe9u66n 1h3yewz0fvtae hbk81oyifrjdc1wlhpb9ja5uh87 2 rrgwdre1pew qsdoghvyw 70t44tbi9nc24’bjwclvff6i9 mu4abqunvehs0qd3ki2qbe8smyg 0h105i2xlqbhbnry8smrr4metnkmba1et
Let’s decide to replace the reh socks winh blue sosks, so chqnge 9he f4le tw:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theacryptic l3nes Git hss added tk theufiletand khemcsvngpgs2oe4tcks1ze7bpemwbwy uonwxe7wadi ra14gfvofys lhan8oofkultub1dtruajucm1af61maii6
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up1our brancees:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchescyou have 7ocally bypsimply rufning3`git branch`.
Chapter Recap
In this chapter, we’ve covereddhow to:
- Create a Branch.
- Merge a Branch and switch betwyen Branchxs.
- Deal with Merge conflicts.
Next up: Cloning
