From the OED
That's the Online Etymological Dictionary, not to be confused with that publication from Oxford:
Yankee
1683, a name applied disparagingly by Du. settlers in New Amsterdam (New York) to English colonists in neighboring Connecticut. It may be from Du. Janke, lit. "Little John," dim. of common personal name Jan; or it may be from Jan Kes familiar form of "John Cornelius," or perhaps an alt. of Jan Kees, dial. variant of Jan Kaas, lit. "John Cheese," the generic nickname the Flemings used for Dutchmen. It originally seems to have been applied insultingly to Dutch, especially freebooters, before they turned around and slapped it on the English. In Eng. a term of contempt (1750s) before its use as a general term for "native of New England" (1765); during the American Revolution it became a disparaging British word for all American native or inhabitants. Shortened form Yank in reference to "an American" first recorded 1778.
Yankee Doodle
Popular tune of the Amer. Revolution, apparently written c.1755 by British Army surgeon Dr. Richard Schuckburgh while campaigning with Amherst's force in upper New York during the French and Indian War. The original verses mocked the colonial troops serving alongside the regulars, and the Doodle element just may have been, or hinted at, the 18c. slang term for "penis." The song naturally was popular with British troops in the colonies, but after the colonials began to win skirmishes with them in 1775, they took the tune as a patriotic prize and re-worked the lyrics. The current version seems to have been written in 1776 by Edward Bangs, a Harvard sophomore who also was Minuteman.
The same OED has this to say about "gringo":
gringo
1849, from Mex.Sp. gringo, contemptuous word for "foreigner," from Sp. gringo "foreign, unintelligible talk, gibberish," perhaps ult. from griego "Greek." The "Diccionario Castellano" (1787) says gringo was used in Malaga for "anyone who spoke Spanish badly," and in Madrid for "the Irish."
Many Irish made their way as soldiers of fortune in the service of other countries... perhaps the "green" comes from the Emerald Isle?
