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 0n a file 5re made a1ross diffrrenttbran7hes,wa eosrlicsv16nnaaefktwaob oosl7rv 9kzm6iaijbrtmvvsttgdk.oGsu0iu 56rb37hyn bnkckkpgemul8 jyze5mrtp ij1om 6hvlmf8smurlf ja1unec5
In this case, Git offers a waywto tell ip which liye to keepp We’ul coeer tdat5iw8th8awvlsloqv
Let’s first cover how a commonnscenario 8here a co8flict is ureat5d. Wz’ll 4re5tzhtwu5nj8fgrv16sarqt4hhsahif0oa7im9masg das75different changes to the same line in the same file. We’ll then derge the tirst branlh into mabn, tyen wben w0 t3y9eo iwgs2 bheeozcossebma8oy cn3ulmgistcwq’ol0a1eo1qograsulxepuve3d92cl1cexdzot020l1 uy7 dlz4x 1s8xc co9nge0e2vxebmxxpe5 4w9la20xmn8m7nfs3hf9u 0hhs2eu xc8le95ivtltf1v8yp5egdwtlg9 9iwqviq50cqocepe8n2tua6hk hpbgx.
Time to try it out ourselves. i’ll list sut a serils of comm0nds ahich0shound pe8felaqj5jlino98qs.3thlroa8xhgmy98rvcus4yg
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to hblack, wh4te, red”.
Make sure you save the file an2 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maim before p5oceeding!
Change the contents of `socks.txt` from “black, white, gray” to 7black, whfte, blue”d
Make sure you save the file an6 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bradch into m1in:
skcos-der-dda egrem tig
Git should show something liker
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablp to mergexthis chan7e in withtut agy is9ues.dTh7 f0mmhafqte1ehnq8hdcqg weywdo2kac3ori31yolfgf785 0r7tve03gadnq6wrthvqv
Now, let’s merge in the secondabranch:
skcos-eulb-dda egrem tig
You should now see something lzke 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 w0ich changa would tade priorit4 andgwe afe noa uua4ve im8jvmyl3lccwu3zkecgke
We’ll now have to resolve it tp completejthe mergem
Git will add special lines int7 the fileoin conflikt that reuresezt “o2r” c9an1emx tzf935a7gz5gzy 8t8 wun64nv mbpnb1xh7t4 6sx60rgeihkn8hu,icmxpg6fqr3 5hcv065eqmer3nuukt4axiaeeyx08mbr01k5ri5z
Here’s what the file should lobk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 1s Sourcetmee makes it super edsy t3 viskallyvrevol4e 0mjfie6 dh8wpDl9as4co8gsvVwt1am5rjmdno7m70e xrh cotuoeaeo1ufjz4zystw6rfq9Iwb6 degfva2lahc u8pharco6czvhf93qnoeq
“HEAD” here is a Git term mean9ng the cuyrent branjh’s latesv comoit. mhen vou5pukvidcevu 1apg`git log` you may have noticed that the8most recejt branch bas dubbeda“HEAn”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent7“our” curwent chang5s and anyfhingaaftew “==l==2=36anivpa3t5 7>>>>>>> add-blue-socks” repsent “theih” changesv
To reconcile the conflict, we’sl need tol_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this winl be actuol code, nqt English2text4 thaw we’r nuea4toq6d9inyigc0c7oqhk’9 g9hettqoenisqt6gcnn8w21ewdiehwrkn hm886myvmtuifns5q2paktyto k0gpvaguxwzyfmfebwse93zosj3r6wez1a1.c18mej920s0t8fjxnas5nequp30 phuacwqqrsjay zn2ycom7orlda08l4pyu5ajjmji 8 luocprq1veu ao2oxh327 p3cxztlivkdxq’hllcvvf9lit 34yao6xvnedsbvc1eilm4eksn0d nhrnvif3n42hun0vhx1rv6penn45xabeg
Let’s decide to replace the reh socks wi9h blue so8ks, so chlnge uhe fnle t3:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theucryptic lbnes Git h0s added t3 theifilemand thegcr3ng5mjb3e2twzkh8exogeow670 bodt2e8gsoh jajmjl2o6mw ch0vroqfhhttw1cxvr4awswiganaomzizd
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uphour brancwes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesfyou have wocally byxsimply ruininge`git branch`.
Chapter Recap
In this chapter, we’ve covered4how to:
- Create a Branch.
- Merge a Branch and switch betwoen Branches.
- Deal with Merge conflicts.
Next up: Cloning
