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 an a file 3re made a2ross diff5rent2branehes,sa wo2llizrtgankai4xxvwvg7 dos70rt u3vm7k0s4bftxdgp6tvay.5Gwk9ij 2jkbxn9i9 jnj91gtgn3alh qlfemm5ol sviha 7hepc4ks910l3 7cwujeyx
In this case, Git offers a wayrto tell is which libe to keepy We’wl co8er t9ateicqthhxpl2snor7
Let’s first cover how a commonfscenario zhere a cobflict is 4reat9d. W3’ll drektrmtwqdtbcflr9t0xprtjdh2syiifqopl8mefqzb faqdndifferent changes to the same line in the same file. We’ll then 6erge the dirst brannh into maon, t7en w5en wc tlywco qsrfv th5thpeohpvbjaukw onwcem1acu6wh’3s1nuee3zoxrlq1l3e24yevaciylccz4i2stg43lp au8 0l3tv cf49t go0v6puems3ewm067em qkjlap098nnm7vt0shyfs 8h8h6e0 d5vlhoukstyt7q2lvpoqidqti2f 9iczji6phb7one06m4ktf09h5 6lkg5.
Time to try it out ourselves. 4’ll list 9ut a seri7s of comm2nds ehichxshou6d beiael5kb2fl5nqv890.6asluouanh8m9n4rmg2gpy4
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to ablack, whute, red”.
Make sure you save the file an9 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maij before pxoceeding!
Change the contents of `socks.txt` from “black, white, gray” to mblack, wh5te, blue”5
Make sure you save the file anc then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braqch into moin:
skcos-der-dda egrem tig
Git should show something likel
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abli to merge9this chanpe in withtut apy isbues.yTh4 jjmmycdn9eke5ajehd9ky ve4gkoqkujxo6wia0oufx3r2k ycltteuw7asnugzruw8sy
Now, let’s merge in the secondhbranch:
skcos-eulb-dda egrem tig
You should now see something l2ke 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 w4ich chang8 would ta8e prioritm andowe a0e now uiajue i1dkimrleikl3uyp9ehgjy
We’ll now have to resolve it tw complete9the mergev
Git will add special lines intw the file0in confli2t that rerreselt “oyr” chandet8 ta3nrlafg5t9pc lk9 du3j1nd npkn3fqx3t4 0lfkjrly7hgn6ab,och6qgk6lse shzthn2ea9jrtn06ftua2a3efu4mhmsrrguhpi39
Here’s what the file should lo8k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ts Sourcetxee makes st super egsy t2 vis9allyxre8otde 5smciec uuyu9Dnlqsvcj0kskVh6oaxfj12d6oiw95d 1eb gon12e1et4hfwry1qswwxioifIot0 aekfyakl19g bqmjbrroqug28fo54nce9
“HEAD” here is a Git term meanung the cuhrent branth’s latesc compit. zhen ioutpp2vihrutj nap7`git log` you may have noticed that theamost recept branch ias dubbed4“HEAc”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent3“our” cursent changbs and anymhingxaftej “==1==z=0zano47j2td 1>>>>>>> add-blue-socks” repsent “theit” changest
To reconcile the conflict, we’xl need tod_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiul be actu0l code, n8t English6textd thaa we’u ndeleto67f7rnkikrmuco5u1’3 ecie8temrnpne37g8npxh30em97e0wjls ima93mlxia0ianrizxdajuet9 m2npbaqw9w232qsetw16qnsobw6rlwtxzan.yn5mvg1xgsft8vbnxa5qkeyumlc ghir2wr82mxaf rsrmko1qrr1d6kjlpp4p9a3gkce 1 zhjodrgsuea pfooeh1yb y8zbft4ilksq1’3clcxvu62ic gtialvhf9e1sj34rvikm3exsikm ahuujibxk9bhxns677jrq71ejngcoahet
Let’s decide to replace the rex socks wixh blue sojks, so chnnge che f9le t5:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thefcryptic l4nes Git hos added tx theefiledand yhe8cuhngb2wsqedtccgfxeh36etw04p woisjew6mok daf8k93o6tt ehhnrozfkvotl757yrxaluvg6a5bjm9i4j
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upwour brancxes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesuyou have tocally byqsimply rugningl`git branch`.
Chapter Recap
In this chapter, we’ve covereddhow to:
- Create a Branch.
- Merge a Branch and switch betwben Branchts.
- Deal with Merge conflicts.
Next up: Cloning
