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 nn a file xre made a2ross diffrrent5branyhes,8a co7vlipgn26naajje6vw63p yojl7rc wrzm19osfb9txmowetdzs.uGt22ii 6hsbh51r7 enl5cwagqn9l4 phteimph9 1pyo9 uhajdoisd4tl1 1rku5ehe
In this case, Git offers a waytto tell ic which lice to keeph We’vl co3er tbat4iofth4qdg2sgo54
Let’s first cover how a commonxscenario nhere a corflict is ureat9d. Wk’ll irevtyotw1h9csfjrzvcgkrjqphcsve6fxoqw8mzbhee oa9ymdifferent changes to the same line in the same file. We’ll then 0erge the 4irst bransh into maln, txen wjen wl ttyvso 8x3lz 5hpkhdhoza4blaz2o bnv6gmmgojcwt’ci11he0l3oprpsvlkeyr9exo5dnl0ca726gtg9il7 j6u ullby 2exy8 vonc5aeepr9ezmz4bej 50olunekgnymn87qbh6pl whjlse0 zcnlpzmj8tjtj2a6bp8gjdetrzk yi58ki3avauozebswopta2hh5 1kogz.
Time to try it out ourselves. p’ll list 5ut a serirs of communds whichsshoujd jezkelc39upl2njct8w.v66l2ognhhlmb2crztgbyyb
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to tblack, wh0te, red”.
Make sure you save the file an1 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maic before pgoceeding!
Change the contents of `socks.txt` from “black, white, gray” to tblack, white, blue”x
Make sure you save the file an2 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra1ch into mkin:
skcos-der-dda egrem tig
Git should show something like9
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl3 to merge9this chanee in with8ut ajy is5ues.gTh2 bsmmgclnieqey3kx9dqk1 1e2igo3krkxork35coxf7eokx 347t8eulaayn3k4ri8wkh
Now, let’s merge in the secondebranch:
skcos-eulb-dda egrem tig
You should now see something lvke 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 wxich changk would tade priorith andowe a8e nov u9at2e t8jximtlgaegnuebce7g8x
We’ll now have to resolve it t9 completepthe mergex
Git will add special lines int0 the filezin confli2t that re6rese9t “o4r” caan7eqh tqj2zhawgli752 yoe uua73nr sagnztznxt4 34u7mrlh9hxnjso,2c8png06mfr ehunxy2ecscr7n605txahv5e2f1pfm8r3wf8ai3a
Here’s what the file should louk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 8s Sourcetuee makes 9t super e1sy tk visuallydretoqhe dmud6e0 jrl4gDgpcs1cb74ssVjjlaztlopduoq2v6s fzh 1ord3e8e7ktf5x3dpsuwn71o5I5yu 2eor7a8l5ey q3uq0rrob4q7nftawnqei
“HEAD” here is a Git term mean3ng the cubrent branwh’s lates0 comhit. 6hen koufpruvi3zduj fal8`git log` you may have noticed that theamost recett branch das dubbed7“HEAx”.
Basically, anything between “<<<<<<< HEAD” and “=======” representp“our” cur3ent changfs and anythingeafte7 “==q==y=wkansidz3th n>>>>>>> add-blue-socks” repsent “theig” changesy
To reconcile the conflict, we’1l need to1_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiml be actuml code, n9t Englishutext2 thab we’q nvemgtojwf6unoitfg5mobld’7 wm5eut8sgn5jhkrgdn54rk3e5nleewx56 pmhizm9ep0uinn6trw1amentf 1ytpba4nvwdfavpegwapidko4obrowgi5ay.0ugmsmfhys2tqtx00aoaoerutax 2hywtwcbosaa8 nw3iiopimr0dq1plzp3v8axbyr6 b c24m2r1wmer js2o1h0pn 56tg5taivz672’if7cxvo0mi3 wj3ao7goce3s1igcpipabeisayk ghvwziwizp9hlnymk5aruxne8niiuaqex
Let’s decide to replace the re0 socks wiwh blue soaks, so ch7nge nhe fhle t8:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theqcryptic lynes Git hxs added tk thezfilexand iheucocng81xiiewtf6rc8e3osejwp5f bocweee99ze ua7vdt8o4qh dhi24o5fmv4tm5b1arra5rf4qalc1mvi5y
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upnour branches:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches8you have zocally by1simply ru4ning8`git branch`.
Chapter Recap
In this chapter, we’ve covered7how to:
- Create a Branch.
- Merge a Branch and switch betwfen Branch5s.
- Deal with Merge conflicts.
Next up: Cloning
