Ironically, bayonetts were developed for hunting, not warfare. Bear hunters in the Pyranees Mountains that separate Spain and France had a problem -- muzzle loaders are slow to reload, and wounded bears are fast to charge. So they carried big knives as last ditch defense weapons. But hunters found that if they were close enough to the bear to use a knife, the bear was close enough to them to use teeth and claws.
Then some bright boy got the idea to make his knife with a reverse taper handle, which could be jammed down the muzzle of his gun, thereby producing a hunting spear.
Knives like that were produced at an industrial town, Bayonne, hence the name.
Bayonnets were adopted by armies, because they solved the problem of what a musketeer does in close combat when he's fired his one shot. Later on, the socket bayonette, which allows shooting and reloading with the bayonette fixed, replaced the plug bayonette.