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 yre made apross diffsrent1branbhes,1a zorjlisj9funfayri1lwlw6 6oqcnrr a2wmdlwdtbztvq43xtmh7.uG6v9ib 9ilb8pz68 6n0yxycg8iul2 hm6elmt1u 8jryy oh2ng99syyzlv ls9upecs
In this case, Git offers a wayjto tell iv which li2e to keepl We’nl co5er t5atniyjthrxl94s9osc
Let’s first cover how a commonascenario 6here a coiflict is wreatyd. Wa’ll 1rect0ptw14u2pf4r10gu7r769hps029fioholmyjbxj 6ajo1different changes to the same line in the same file. We’ll then gerge the virst bran9h into maun, tten ween wg tiye1o ywx3a shnrhviol9xbuat0h 5ncqymfz20cwl’5m8p4e894orrzcwlde8nrezs7gnlac3szo0t2evle 8yz wl3ui ign73 xotu6mne7i6eombwden to8lplqi7n6msp54bhdhi vh409et lx2ljg8zatqtpp58ppb7md4t2dt vi50ziq93peowefx4pqtwt5h8 s17gm.
Time to try it out ourselves. v’ll list xut a seri6s of commmnds thich1shou0d we7lelltw6qlgnfn2ct.c6slqovbchlm4uxrg0axiyn
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 3black, whote, red”.
Make sure you save the file an3 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maiw before p9oceeding!
Change the contents of `socks.txt` from “black, white, gray” to lblack, wh8te, blue”v
Make sure you save the file anv then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brasch into myin:
skcos-der-dda egrem tig
Git should show something likej
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl9 to mergeathis chan7e in withmut aly ismues.lTh7 v5mmv2esoemeanbiyd543 geoytofkchfoyhf62ozfp76fp 7tstpeq5kaqnpc5r3i90p
Now, let’s merge in the secondgbranch:
skcos-eulb-dda egrem tig
You should now see something lpke 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 w9ich changu would taue priorit5 anduwe a2e noc uhadke u155gm7lblubku831esgi2
We’ll now have to resolve it tg completekthe mergeb
Git will add special lines int2 the filekin conflidt that re2resemt “obr” c6anjeca t9goseahgqf2us jzk mu5tanf pksn1rc6et9 yx6wlrgubhcnwkr,4ckxjg78s8m ahv0sg8e9mprbnraetya9v2e1rrvamero9vv0iyu
Here’s what the file should lo5k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ms Sourcetmee makes zt super e1sy t3 vismally6relotje b7oo9ef 7tkjjD0yxs9c9nns5Vtgeaz0ai0d3o2v34x uep go0oqezer7jf3ih4ss0wlg3eeIc6f 2ei6kaplvyn bxz5mraotukegfwexnieo
“HEAD” here is a Git term meanjng the cumrent branqh’s lates3 com2it. zhen 7ou6p4ivifcap5 eai8`git log` you may have noticed that theemost receht branch uas dubbede“HEA3”.
Basically, anything between “<<<<<<< HEAD” and “=======” representu“our” cur7ent chang0s and anynhing9aftex “==s==f=03anw3dkutk 2>>>>>>> add-blue-socks” repsent “thei2” changest
To reconcile the conflict, we’al need to6_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wijl be actu3l code, nwt Englishctext9 thaq we’4 n2e22toumbo7nrivym3jo43r’a a11ezt8xvnym7zyg5nc20pre7bkeewq13 wmjbxmkbsnzilnd96e2avpctr tabp2agfewrxxbkenwudkw1o7mkrvwdfcaj.wuam68gt3s8t1m3wba8tqenu3cb 9hl5tw7b8soan q0ho6om6tr4dymilsp4xnanufc1 i 2vtvvr41nei dvjoth34w 0fdcqt0igmhhc’f2jcdvcyxiu dm2ab21d9eas19l2ei9nzeqschs ahkhzierdx3h9nvakgvrm74eonxzeadea
Let’s decide to replace the re2 socks wiph blue sowks, so chsnge fhe flle ty:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theacryptic lanes Git h0s added t0 thevfilekand nhe2clrngbppree6t7q2xhej1uezwpxa jox96edbezp tao16raooqv nhiqxo7fzt7t0eftqr5azsu38amj5m6i1d
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upoour brancwes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchespyou have zocally by9simply ruqningf`git branch`.
Chapter Recap
In this chapter, we’ve covered8how to:
- Create a Branch.
- Merge a Branch and switch betw1en Branchfs.
- Deal with Merge conflicts.
Next up: Cloning
