Be a god. That's really what it boils down to.
Easily G&W's second worst MU. I personally throw out Mewtwo for this to get some kind of zoning and kill throw game on Sonic.
However, if you are really serious about taking this on as G&W, dash attack is the main asset you have. It will beat out spin dash if you don't challenge the invincible startup, it will punish missed homing attacks, and it is okay at preventing Sonic's usual shield rushing tactics if it is not expected.
All aerials are unsafe in this matchup. Even a campy back air is unsafe if you do not have a solid read on an active spindash movement. You will get dash grabbed.
The best thing you can really do is test the Sonic player's patience and wait for him to **** up, honestly. The defensive tactics you have to this end are good, and he has no real ability to kill you until very high percents from fthrow if you play correctly in this sense. DI away from up throws to decrease the chance of good players doing a dair followup from uthrow, and focus on air dodging a potential uair in this situation. Abuse invincibility on Up B offstage if he tries to dair spike you from the ledge (hard in practice though, and you can die if you don't have a good sense of the timing for this... but if you land it, a stage spike is inevitable and you're back on stage).
Really, anything you do can be punished off reaction oos in theory in this matchup. Just terrible. Strongly advise a secondary switch unless you are very confident about knowing your opponent's habits.