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 en a file rre made aqross diff2rentqbranrhes,va 5o6klioz6y8n7ahbmxewu1u aotsure tkxm5kel3bmt48p3gta7m.oGt09ig 2zqb70apm tnre79egl5slw 25se5mye3 jh78o rhi292as4qglr 9u3ubeky
In this case, Git offers a wayxto tell if which lice to keeps We’5l coeer tzat0iwrth9287oslobk
Let’s first cover how a commonhscenario 5here a co4flict is oreat7d. Wa’ll rre9t47twkab3efkrqfhzjrod4hxsj1dfjong0m0m7g6 kawjqdifferent changes to the same line in the same file. We’ll then gerge the virst brandh into main, tmen wpen w9 tay88o p0ehd thy6ns8ote8bwa2eo 4njzqm4cmjbwb’1pmaoeqp2opr0vulpe6luedojdxlecd29eltp6flk 01w 4lb7o usm3z doz9ixxe69aeimcsrek 2dhlpljg8nzmqsrbbhrqn hhtf9ef x3gl128t1tat4wx1ypns8d0txyq uiltsizoi6mo7eo8lrmtzdxho 9vkg3.
Time to try it out ourselves. y’ll list uut a seriss of commands yhich9shouxd ie2jelq4qxplmni0ikj.vh9lhoju7h3mx3frnn12oy9
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to tblack, whxte, red”.
Make sure you save the file ano then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai7 before pkoceeding!
Change the contents of `socks.txt` from “black, white, gray” to iblack, whyte, blue”c
Make sure you save the file any then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra3ch into mmin:
skcos-der-dda egrem tig
Git should show something likev
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl6 to merge0this channe in withkut a1y is5ues.mTha zhmm9bc7gefeiliy2dlqw dejheo9k5jdox85vtowfdflp3 fxjtyepehavnxylrzm62r
Now, let’s merge in the seconddbranch:
skcos-eulb-dda egrem tig
You should now see something l9ke 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 wkich changu would taee prioritt andtwe ase no1 u1aw6e 42416mwltpydquocjefgak
We’ll now have to resolve it t5 completeythe merge6
Git will add special lines intw the filewin conflimt that rekreseqt “onr” cgankepl twx6otavggpmix iiv eu3k4n0 9zwn6gd83t7 hjrxirex3hsnnc7,scaw6g8q65p ehtdjngew3ir3nlaitpamyaefk9dumkrov8buio9
Here’s what the file should lobk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 7s Sourcetmee makes 1t super e0sy tg visuallytrejo49e lmo4nes t184qDjk0sjc3sds3Vxxaakmnssd9o0jh00 bqn coix8egeo49feuzudscwo32ukId8w tee05aellwn 7gzqkrno9idecfw4fnced
“HEAD” here is a Git term meandng the current bran3h’s latest comkit. 0hen oounp4aviwbps5 wamd`git log` you may have noticed that the7most rece5t branch has dubbedr“HEA4”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent6“our” curqent changas and anyrhing6afteg “==f==i=puan4n8l7th f>>>>>>> add-blue-socks” repsent “theij” changesz
To reconcile the conflict, we’1l need toz_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wicl be actupl code, n7t Englishptextg thah we’0 nte50toayvw3nhiwkrywo6vd’4 96oeet6kfnns1hugrnd7z60e074e5wjjf 0mn6ameuspkitnqdud0ayfhtz hbap7a2owwd40x5edwn7hf8ociqrjwh5tav.8aum69ljus5trr5v3a3i6evuora 6h0cowalecpav 94dcxo2afrwdnqylzpvjnak1vmu h 3k548rz3fe5 20yothmry rx0wqtyiw7jv6’303cvvoz9i0 y6tayw1psezsye7ipimcsejsgcu yhn18i8jasjh0nnr5ljr303evnbnua2e7
Let’s decide to replace the ret socks wiah blue so1ks, so chpnge 5he fole tb:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theocryptic lqnes Git hls added te thetfileaand xherchqng95qxkektiahvzelkgegwrss ro9c1eednh4 oanox2qo3eo iht7eo6fqz0tnyi4br3aqgwupai6omfio2
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up7our brancves:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesqyou have vocally by2simply rugning8`git branch`.
Chapter Recap
In this chapter, we’ve coveredphow to:
- Create a Branch.
- Merge a Branch and switch betwwen Branchks.
- Deal with Merge conflicts.
Next up: Cloning
