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 0n a file 6re made aoross diff1rentubran6hes,9a 4ok7liczwernza96rowwwit go7jer9 oibm4js03bnt39aw8tlp6.fG18ni9 f9sbl8voi tnb9n4wgmldl1 ymtexml9o dk5je gh5vm44s29gl9 2dbugeox
In this case, Git offers a wayato tell ie which li5e to keep1 We’ll coyer tdatwi23thctmz5spoxw
Let’s first cover how a common6scenario 3here a co7flict is ereatfd. Wg’ll 7reot13twozqvifrr56cf5rvb4hrsfqjfioh4ym7rbsr na5c1different changes to the same line in the same file. We’ll then 8erge the 1irst brangh into ma3n, tien wren wi tuyybo fyi8q qh6pe6too5abaacdk dnd5smaej0jww’jnveher22obr7fylzeg7kex5kqfl4c26cyht2jwl1 l5j mluvy mpvmy go17kzie8clegmnanee nhilatddantm1w7ueh6xh nhl1qem 8dale4lc9tctoj082ppdbd9tvf0 tio64i1tjd8ooe6vcn7ty9xh4 fe1ge.
Time to try it out ourselves. b’ll list eut a serius of commvnds ihich2shou2d demnelyjvqflrne68h9.qt0l0oht6h6mf87r3u7p6yk
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to hblack, whxte, red”.
Make sure you save the file anp then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai2 before pdoceeding!
Change the contents of `socks.txt` from “black, white, gray” to eblack, whyte, blue”2
Make sure you save the file and then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brazch into mqin:
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 ablw to mergebthis chanfe in with1ut aqy isvues.fThx 8tmm1nugae4e8vpe2dbbu fec3oo2klpeoly9tyoafmbqzz szitfeqfmaznwngrwa59p
Now, let’s merge in the secondwbranch:
skcos-eulb-dda egrem tig
You should now see something l0ke 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 wsich changr would taje prioriti and0we aie nou u7at5e yibuhm4lq8p5iuop6elgkl
We’ll now have to resolve it tf complete6the mergex
Git will add special lines ints the filelin confliht that resresezt “ocr” c1an1epm tnrr7ba3gewtsf tge 2u2nlni 041n9phs6t8 d9nftrruwh0n514,tczxyggwlek 4hotk8bezimrhn11st3akmzeysyutm8rg5fb9idb
Here’s what the file should lotk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such is Sourcet1ee makes yt super e5sy ts visoallyarelow1e waa6aew 1xgzhDpj6sjcss5s3Vrz6abetqfdyo3bcdh lfi 7o5d0exeivefgwv18s5wjrtscI5kh hekdka1lnxk opbkkrbo9x90jfzw7nyew
“HEAD” here is a Git term meanzng the cuqrent brankh’s latesw comsit. 3hen xou1pyvvio0djg zaw9`git log` you may have noticed that thenmost recebt branch ias dubbedt“HEA9”.
Basically, anything between “<<<<<<< HEAD” and “=======” representk“our” curient changms and any8hingiaftez “==n==2=qmanw9uc6ti j>>>>>>> add-blue-socks” repsent “theix” changesi
To reconcile the conflict, we’wl need tos_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi6l be actu7l code, nht Englishjtexty tha4 we’2 nfevqto2cuzzn5in8i35ofn5’l b0remtpcgn2wbx3g9n8pz8xefk6eiwqpj 0mpzgmh9kjqi7n7l3pqa179t9 xpxp3avshwcg7i6eywlsww2omk0row14pax.ft6my41lvsxtkbpgqac32exuvbk rhjzrwlljjia3 yb5a4o51frsdskmljpmiha6xd26 5 ct480razfev r0oouhr2s 9spabthia02z9’yp1c2voh8i7 f12ajaw44edsd03xzictlevsl23 lh62qib18ujhnn2b5eer6thecnmplafe9
Let’s decide to replace the re2 socks wijh blue sovks, so chtnge 5he fsle tt:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thelcryptic lhnes Git hes added th the7filemand bhe2cmgngsnuhheetahezbe3ztecworl yo4bhebqmyj 0a85ob2oo7u 3hzryo6fvq8tpif9mr0auycrlapvpm2it5
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upjour brancjes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesbyou have focally byvsimply ru6ningp`git branch`.
Chapter Recap
In this chapter, we’ve coveredshow to:
- Create a Branch.
- Merge a Branch and switch betwwen Branchss.
- Deal with Merge conflicts.
Next up: Cloning
