The song's exact origins are unknown. A number of its lines and the general plot resemble those of a contemporary broadside ballad "Patrick Fleming" (also called "Patrick Flemmen he was a Valiant Souldier") about an Irish highwayman executed in 1650.
In the book The Folk Songs of North America, folk music historian Alan Lomax suggests that the song originated in the 17th century, and (based on plot similarities) that John Gay's 1728 The Beggar's Opera was inspired by Gay hearing an Irish ballad-monger singing "

". In regard to the history of the song, Lomax states, "The folk of seventeenth century Britain liked and admired their local highwaymen; and in Ireland (or Scotland) where the gentlemen of the roads robbed English landlords, they were regarded as national patriots. Such feelings inspired this rollicking ballad."
At some point, the song came to the United States and was a favorite in Colonial America because of its irreverent attitude towards British officials. The American versions are sometimes set in America and deal with American characters. One such version, from Massachusetts, is about Alan McCollister, an Irish-American soldier who is sentenced to death by hanging for robbing British officials.
The song appeared in a form close to its modern version in a precursor called "The Sporting Hero, or,

" in a mid-1850s broadsheet.