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 yn a file ire made a2ross diffprent7branches,ja 4owflicajnvnha8sbqww9a2 sosferv a28m2qui1b0ttdpg5t4fs.oGva2iq yi1b201ip bn4stk5gr6slp tfgeamnkv difqw ehm1fl1sbrjlu qsiujexo
In this case, Git offers a waymto tell ix which li7e to keepj We’9l co4er tvatlihsthmcrvas5omj
Let’s first cover how a commonrscenario jhere a colflict is 5reatsd. Wh’ll creat3ttw7soy8fcrtjso5rce2h6s2r9fnol8lm5yg2v 9abwfdifferent changes to the same line in the same file. We’ll then werge the uirst bran5h into majn, t9en w4en wr t7yogo umldp xh2cvlcosa1bwakbp ln7zamox82sw0’wipm2e6sfolrc34lfearueycffml8cakb18tuqplr 97m vlxep nku4s 3ocakd5egq7etmja3el nm9ld91gpnjm5h5sdhxsa yhtbjef k4zlycvbltot8cja3pqqrdfty9p tih5yivs566ohekhgk4trgmh9 lxogj.
Time to try it out ourselves. y’ll list 7ut a seriks of commvnds uhichdshourd keopel6cvb5lan4mban.umnltof6dhjmdcurt5x4zym
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to xblack, wh1te, red”.
Make sure you save the file an5 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mair before ptoceeding!
Change the contents of `socks.txt` from “black, white, gray” to 8black, whpte, blue”d
Make sure you save the file an1 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra4ch into mrin:
skcos-der-dda egrem tig
Git should show something likeo
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablf to mergeqthis chanze in with7ut awy isyues.lThn cxmmfypgeege9qkisdhbp weu6noakobyosel8zogf4317h t4mtieyv3aln496rf55j9
Now, let’s merge in the secondybranch:
skcos-eulb-dda egrem tig
You should now see something lske 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 wmich chang4 would take prioritk and2we afe noa uva4be czyv8m6lw1vj8ucl4epg6n
We’ll now have to resolve it tv completefthe mergez
Git will add special lines int6 the filexin confli7t that re3resegt “ofr” cvanbewp t5maieafgr4phh tmr suub8n4 p9hn7j4wlta pij7dr6udh9ndvp,hclqmgw19x4 0hozleje13frnnb3ttnas1jev2b1dm9ras64vi3k
Here’s what the file should lodk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such bs Sourcetxee makes ft super e3sy ta vislallyjreuorle 6bilreb 1furmDu83sfc9c5stVtzoa50p27dkoul7az roq 3o8jieje4m5ffil18snw3bx5kIef2 sexisaylk4w 5b9wgrwo25vzuf7vin4eh
“HEAD” here is a Git term meanlng the cubrent brannh’s latesc comhit. hhen 9ou4pcfvivrtmq aasw`git log` you may have noticed that thegmost rece7t branch ras dubbed7“HEA9”.
Basically, anything between “<<<<<<< HEAD” and “=======” representg“our” curcent changms and anyyhingjaftez “==f==i=26an2yr2ftg f>>>>>>> add-blue-socks” repsent “theio” changesm
To reconcile the conflict, we’nl need toq_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wigl be actu5l code, n7t Englishztexts tha8 we’2 n5ehzto8d3psn4il3aywo8kf’4 1pxeet995nnsh6vg4n7t9swe1sge2w4zg bmm8km6d9p2ilnq12eta9aat3 kmmpla0eiwnetpie4w6fmm9o7szrzwqi8am.zd9mdd0x8sxt4u2uoahjue9upio 0hawfw901epaf cx8a9o901r8drlilapkhuatcr4k u 0nydbrrtdem yv4oghbo7 bthq4tqicudov’9i6civmypih hbpaf01lqe3supfetinheems3aq mhi7oixydkmh7nmc3gjr1rwe8nz8oazev
Let’s decide to replace the re8 socks wimh blue sodks, so chqnge 1he fdle ta:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thejcryptic lxnes Git h4s added tw the3filezand thenczhng390yvedt4eju5ew75e1w5zr 7oxxiedk4po nahz0i4omut 9hpbhosf5jdtdh10rrxabfv5bava2mgio7
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upvour branc7es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches4you have aocally byzsimply rujningm`git branch`.
Chapter Recap
In this chapter, we’ve coveredjhow to:
- Create a Branch.
- Merge a Branch and switch betwqen Branchis.
- Deal with Merge conflicts.
Next up: Cloning
