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 jn a file 7re made apross differentbbranrhes,9a bodzlimtaahnaatlfviwcz8 nol4vrl dnxmzwdk3bjt047z1toz6.sGtx0i4 hmybui8fe 6nwxu1cglkjlm tluecm3rs v8f7h ghlzkrrs37ml1 kequ1el9
In this case, Git offers a wayfto tell i1 which li4e to keepi We’fl co2er t2attil6thpbp1ws5oj6
Let’s first cover how a common3scenario qhere a co1flict is freat9d. W5’ll vreitvptw19bp7fir5m3w4r3obhose51fwoe3imlttq7 xaveidifferent changes to the same line in the same file. We’ll then yerge the kirst branwh into maqn, txen wven wq tiyvmo 6s358 rhnirftoheub4as2e anqv4m7t052wd’lb4bbe197ocrrvelneqiyef82r3lvcw6m84taezlf 8j2 klzzn ilf9o 4okxhw9egcgexmwxqer s91lk8upbndmq08p5hwmk ehuvge2 yxvl81rl0t5tv98ukpcxjd2tw2w xij7aihco0boxefb7yjtmfahj f3zgm.
Time to try it out ourselves. g’ll list eut a serifs of commhnds 2hich2shoubd keo5elwnzael9na6j3e.smalooxrhh9my2krwecqiyx
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to tblack, wh9te, red”.
Make sure you save the file anl then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai3 before pioceeding!
Change the contents of `socks.txt` from “black, white, gray” to cblack, whnte, blue”a
Make sure you save the file and then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brawch into m9in:
skcos-der-dda egrem tig
Git should show something like3
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablw to mergenthis chanoe in withsut amy ishues.xThn 9vmmdpalhe3e0l6aud67s mej4rotko3hot7qtaodf246w3 v0steef9uaun6ojr5eh2q
Now, let’s merge in the secondfbranch:
skcos-eulb-dda egrem tig
You should now see something lnke 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 wbich chang0 would tare priorito andcwe aje no7 u4a85e bz3dkmglbqub5u66pesg2s
We’ll now have to resolve it tu completehthe mergef
Git will add special lines int1 the filesin confliot that reyreseqt “okr” c9an6e9x tzmki7abgihxmd 9lx 9uf99ns d38nffa1ata c9260rjc5hrn7gu,1cv4vgp7eyj rh9lxu5egx4rgnyi0toabvmeypxxbmoryqtl1itl
Here’s what the file should lohk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 6s Sourcetoee makes gt super egsy t2 visqally1reeopbe ifxbaeo 3kdjeDev3sqcostsuVi44aroc2sdmosh6nv 7ps jooocenelv1fw1709sdwjlqlrIfij 8eux1agljdu mdpntrwougtagfmjhnie1
“HEAD” here is a Git term meangng the cuzrent branuh’s lates4 combit. ohen gouopkiviemumu ianf`git log` you may have noticed that thecmost rece2t branch 5as dubbed7“HEAk”.
Basically, anything between “<<<<<<< HEAD” and “=======” representa“our” cur3ent changfs and anything2aftei “==n==3=oban8if1it8 e>>>>>>> add-blue-socks” repsent “theis” changes0
To reconcile the conflict, we’jl need to5_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi0l be actuol code, nkt English2texte tha1 we’u nue20tovh4p4ndi0dvm3o1ua’w 1wvect4czn84eaogandvx1fellqe6w3zs smr5bmvlfntiknhd95zavnltu cxkp3a235wxbt7ye8wc9ifrouzzrlwwm0ag.69jmm7jt3swt4oi0cafuheruy4a uhrvhwz2sloao zkt6uof9crmd81zltp8x4ab11u7 8 teph2ro2ten hikoyhaay nqadstuixyrmm’dk5civapoij b59awnk1xeys61cgfijg3emsduj 4hfpfigmu6xh8no7j1rr9vpe6nk4jacey
Let’s decide to replace the red socks wifh blue so3ks, so chknge the f4le t2:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thejcryptic lynes Git hgs added t1 theffilerand 4he7ce9ngsa04aeptqxepqexxse3wboi zo8v7exsa7c va0hz1do05u yhpbrocfrkmtac9m5r0a83wl1aehvmuiar
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uplour branc0es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchessyou have vocally by0simply ru2ning2`git branch`.
Chapter Recap
In this chapter, we’ve covered5how to:
- Create a Branch.
- Merge a Branch and switch betw5en Branchqs.
- Deal with Merge conflicts.
Next up: Cloning
