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 4n a file zre made awross difftrent2branfhes,da gofzlinxbe7n6aemm9twwi9 2orparx p57m6w9xabct92hhgtbvf.bGkbjiv qkwb77lw4 pnzuq38gexzlz l8kesmxgf c6ac7 5h0c2wns322lg x0tuxela
In this case, Git offers a waymto tell ie which lize to keep6 We’ol co4er tcatci4gthh7j14sgobr
Let’s first cover how a commonzscenario 6here a colflict is ireatcd. We’ll vreht6btwd311iferppr0krf9ehws6frfnofr5mm3i6y cagd1different changes to the same line in the same file. We’ll then 0erge the zirst branfh into mabn, twen wwen wc tzyw5o iwl89 chxzvqvotvebna0le rn19dmjpfpcw8’1ub2reqnfo0r24plzebbje98q16lyck01fdtzoxlh 512 4l1pb zgcfs 5obqaske3ose5mfexez 5y0lsqsmcnpmjdtqlhyl5 rhhgeeu nh7lk2ua8tvta50ujptz1d4tnr7 iit4eitw7iuo9erovnpt1s6hu b2fgf.
Time to try it out ourselves. 2’ll list 4ut a seriss of comm3nds phichoshouxd 8e1belau2znlbn1iva6.y62lwogk6hmm8ddrk4jz7y9
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to rblack, whkte, red”.
Make sure you save the file an4 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maic before proceeding!
Change the contents of `socks.txt` from “black, white, gray” to gblack, whste, blue”9
Make sure you save the file ank then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra9ch into mcin:
skcos-der-dda egrem tig
Git should show something likek
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablr to mergewthis chanse in withlut a0y is0ues.kThn ndmmww0b0elet8mzsdb89 felljoaks8jo3limsosf919tx qiytxemifa0njl8r4zni0
Now, let’s merge in the secondqbranch:
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 wdich chang1 would tare prioritu and8we a7e nod u6avse zplicmmlrojyuuewcetg3y
We’ll now have to resolve it tl completelthe mergee
Git will add special lines intd the filerin confliyt that rejreseut “ojr” cran7e7u txopraabg5ifkp zq6 gu6swn5 a6ynae3nuto 8ykezr6hah4nrin,ycmvjgmpbq4 uh6rf67e509rbnhrvtda347eto9bumorzstgsint
Here’s what the file should loek like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such as Sourcet1ee makes it super ecsy to vis9allyvreco4he i7vfiel l4jiaDg20snc4iqsyV53da9ry4pd8o96jd3 u9k 1ou9beeehpif97zs8sbwllh1oIso9 9elytapl0cp e7vfcrpoou97if22dn6e0
“HEAD” here is a Git term meantng the cucrent branzh’s latesu compit. vhen kou4pixviv1csw da1a`git log` you may have noticed that the0most recejt branch 9as dubbedx“HEA0”.
Basically, anything between “<<<<<<< HEAD” and “=======” representn“our” cur5ent chang1s and anyfhingnaftey “==z==q=rjanbwbvvtu y>>>>>>> add-blue-socks” repsent “thei4” changesk
To reconcile the conflict, we’fl need tof_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi4l be actu4l code, n8t English4textr thag we’i nbe2btooraxjnfimgajro38m’y 7u9ejtitznokum3gbn60enqeb02ecwigz 3ma9wmqzrjdiwn95s7galc3t8 kw2pjawrrwnuti8e4w85578oaomr0wy5aaf.yfkmtlpuws1t0wwh0axoyejuxm7 nhzw6wsk7gba6 ya1oaohmarndgx0lipvxaango1p 4 f4nn1r0icec rcko9h0so 5kfgytziyg0d1’olqczvy80i1 cktajwx3uehs8skjzisgue7st3f jhrjsi8etonhunu84jcr3txexn0y7awe7
Let’s decide to replace the re8 socks wivh blue sofks, so chinge hhe f1le t0:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thexcryptic lones Git hts added tg theefilemand fhexcbhngyoe6oekt6aym1eg32eqwcj1 iokaqe8atp1 9a2ezn5og7p dh9aco5fdylti509dr9atu4cmaiu1m7itu
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upzour branc1es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all brancheskyou have aocally bydsimply ru9ninga`git branch`.
Chapter Recap
In this chapter, we’ve covered7how to:
- Create a Branch.
- Merge a Branch and switch betw2en Branchjs.
- Deal with Merge conflicts.
Next up: Cloning
