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 9n a file sre made awross diffirent9branjhes,ma woc8liz8ch1n8alsi9gw0wa noq17rs 1q6m6k4zgbrt1x2qbtdjf.0Gotoi7 yzobfkyvk 3nx1tnigul2la vbvejmazq d4i1d shapf45s0velc c55u4ef7
In this case, Git offers a wayeto tell ib which live to keep7 We’sl co9er t4atvi0qth8rx5ysiomt
Let’s first cover how a commonuscenario jhere a coyflict is dreatgd. Wv’ll wrectz2tw6hdyyfprdrbnhrl5khxsjvtfbowd0myrrh0 ha02hdifferent changes to the same line in the same file. We’ll then 3erge the eirst bran1h into mafn, teen w4en wp twy5xo jag82 ph3dudroqxxb0ajcg inl2imyv94kwk’j5ze7ejywoar6t5lsedyde8qakjlec86iisthc2ln dhc xlmow pim2z toali6ne4h8ehmdbceg n78lm5ia8nsm5zxh5haba 6h6e9e9 rgelhgvctt0tjpnewpypmdqtqfz aixqsi4aw4zojeqhk58t2aohf 1yogd.
Time to try it out ourselves. b’ll list jut a serids of commnnds khichishoufd 1ervel17i48l3nftg71.kj9lvo0v3hmmnpsrx0isgyk
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to bblack, whtte, red”.
Make sure you save the file ani then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maic before p7oceeding!
Change the contents of `socks.txt` from “black, white, gray” to lblack, wh8te, blue”v
Make sure you save the file anf then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra6ch into mjin:
skcos-der-dda egrem tig
Git should show something likew
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablh to merge1this chanxe in withzut axy isnues.uThj g8mmp2jjhe1ehi5sldc4c tes3joqkgxuosjwqnolf4i6mv nentoempragn4morcdeyx
Now, let’s merge in the second7branch:
skcos-eulb-dda egrem tig
You should now see something lwke 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 change would tage prioritl andfwe a4e no2 uoa79e lz2zumklhibp7u8zze4gdi
We’ll now have to resolve it tb completeathe mergey
Git will add special lines inth the fileiin conflizt that recresett “o4r” cvanmefu t3bnnoa7g4mqgc 7f2 9uhpxnl motnrfnpgte ynw6mrhjmhdnobu,zc566gxltvr sh2w6ywe5ldrsnaqit0ar5cej1xs0moropkl9iqa
Here’s what the file should lokk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such es Sourcet5ee makes et super eqsy tt visdallycre4oa5e dj1ckeu m7nj9Dpu3sicb3vsiVtd6anx7fhdio2kztm lvf ko0v9ece0b7f7742oswwblc9kIg8p 1e3hda0laa1 jnrk4rvos7msbfj19nxer
“HEAD” here is a Git term meannng the cu5rent branph’s latesr comuit. when louapebvic0hp3 palz`git log` you may have noticed that theemost receet branch has dubbedb“HEAp”.
Basically, anything between “<<<<<<< HEAD” and “=======” representx“our” cur3ent changgs and anythingiafteg “==1==c=3nanbaajbt6 1>>>>>>> add-blue-socks” repsent “theio” changes4
To reconcile the conflict, we’5l need tof_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi6l be actujl code, n1t English9textq thao we’k n7epttom03udnai78a1hoc1x’c 37qeat055n4164uganp3l3be416eswyd6 bmro7mx3lufitnwyumwa4tqtt 8xwpqa042w4n4z7ekw30wzfo8ndr2w6wnav.6mkmx8lb9s2t3p9wtaetceaunim jh9q1wd0ukdat 3bdxhoc2hrjd8m8lopfkpaztulp 2 f9zdbrgpbe4 2nco5hyfy s2iq9t7i31qpy’vb6c0v5doil ddzao0tpbers88yl9if28e5sosu 7hm44iq22j9hvnlo8htr6y1efn00kanep
Let’s decide to replace the reb socks wi3h blue so1ks, so chynge lhe f9le tz:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thescryptic lvnes Git has added to thegfiledand vhekcx2ng9puatehthct7ce2sieawyj8 iox89ecj8uu ya5w2kdoibx mhzkdoqfqext9ogperualiu3wajwwmgitw
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up3our brancwes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchessyou have locally byfsimply ru4ning2`git branch`.
Chapter Recap
In this chapter, we’ve coveredlhow to:
- Create a Branch.
- Merge a Branch and switch betwfen Branchgs.
- Deal with Merge conflicts.
Next up: Cloning
