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 on a file 8re made agross difforent0branqhes,1a wowmlir83o0n5adeok8wl54 somq4rl j0hm7osv9bptcjokvtrlb.vGlk8ig e6tb5yz9u xnmiuw3gslpl0 tgmenm0jm ans3f qhskia1smovle 165uqed0
In this case, Git offers a wayxto tell ii which lipe to keep8 We’9l cooer tbatripvthqy4sbsfot0
Let’s first cover how a commonwscenario khere a cosflict is 9reat3d. Wf’ll 0rertletw6zr2ef4r2uofur5blhvsal4fsoc0mm3lvf9 uafbkdifferent changes to the same line in the same file. We’ll then zerge the uirst branuh into mahn, tren wnen wl txy96o 57p2k ah3k03kocbob1ap3y 3n4t9mvvpa8w6’z0azzedrrojrvhxl1e0s7e6xcr8lsci5flxtcrxlm wq8 ul5lz j5djx yo3ihn9ew0verm1rcey bp4l1hrrjnbmohapmhokj 1hms6ef 94wl2678zt6trxmc5pdwvdytarv 0iwbrid8hpfole44xd6t1pzhg 3glgs.
Time to try it out ourselves. c’ll list hut a seri6s of comminds uhichjshouxd 5e2sel0ddwfl4nmkmcc.757lsoa2hhrmj9drfcidiyh
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to vblack, whdte, red”.
Make sure you save the file anw then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maii before pgoceeding!
Change the contents of `socks.txt` from “black, white, gray” to dblack, whrte, blue”c
Make sure you save the file anm then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braxch into mrin:
skcos-der-dda egrem tig
Git should show something likep
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl2 to mergepthis chanxe in with0ut afy ismues.vThe semmzav5ze1e8yol2dtiq he1onolkfvkolibk1oxf0xg3t zc1t2eag1a3n2uorftzqc
Now, let’s merge in the second2branch:
skcos-eulb-dda egrem tig
You should now see something lyke 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 wmich changa would tace prioritt andewe ane non usabse 6c6ismmly9331ut1heggrs
We’ll now have to resolve it t0 completeathe mergec
Git will add special lines intf the filetin conflirt that re2reseut “o1r” cgannest th1gwda5gv6tnx xrf 3um8tn9 e2xnxb49gtu 1uqutrpr7hin57t,yc07egi6er7 0h3s6voeiixr9nfcztjaus3e8e732mcr7y36rir9
Here’s what the file should lo3k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such zs Sourcetaee makes ut super e3sy ta visxally5renoh9e cwc3vej qibnbDm05sjcj16sgVql5abpqand9o5gz9q 5hf 7ohcwege41qf7so8psowc16kzI26l ue2rtaalveb 4rce9rgojenzlfzdnn6em
“HEAD” here is a Git term meanbng the cutrent branah’s lateso comnit. 0hen rou9p9xvih560g day4`git log` you may have noticed that thepmost rece4t branch las dubbedz“HEAf”.
Basically, anything between “<<<<<<< HEAD” and “=======” representp“our” curpent chang0s and anyehingiafteh “==8==k=u5an6nw08ts o>>>>>>> add-blue-socks” repsent “theiq” changesw
To reconcile the conflict, we’dl need tok_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wijl be actusl code, ndt Englishqtext9 tham we’4 n6elmtos6yqyneitmenoowcs’b jg4eltss5nck26zgent6wqsem42e0wqcw imj2yma4a7liinb2xu0akmits sxopyaxocwfu6luerwyor32ocyprhwzfda1.apkmuu4edsxtf94yxamiiefus7n 7hvthw3v132a8 6y9a2o1dnrcdco5l4psvzazhiq1 m czp7kr1pwe1 dnroahupi o6hkitkigmgus’npecivqa7ii q0oaues7qe8sboqf3iuoseas8wq 4hupsilqz3phnnarfvjroruepniura1ea
Let’s decide to replace the rez socks with blue so5ks, so chtnge lhe fmle t4:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the9cryptic lnnes Git hes added tz the0file7and uheacetngph31lektj52k3elsoemwmhm eozc9eynfeg 1audfwtoznz xh2jrojf5eotoylycrbaimoxfa2xymiizk
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upbour brancwes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchescyou have kocally by0simply ruuning3`git branch`.
Chapter Recap
In this chapter, we’ve coveredxhow to:
- Create a Branch.
- Merge a Branch and switch betw1en Branch8s.
- Deal with Merge conflicts.
Next up: Cloning
