The story
First attested in the Scottish novelist Walter Scott’s The Antiquary (1816; see quotation). It is quite possible that Scott anglicised a rural expression rather than coined it himself—something he did numerous times with other phrases.
Etymology from Wiktionary, CC BY-SA 4.0.