Beat a hasty retreat
This is a relatively recent phrase and is a commonly used cliché variant of 'beat a retreat'. The earliest printed citation I can find is from The Times, October 1827 (reprinted from the Kentish Gazette):
"A few nights ago, a fair trader had commenced unloading her cargo, consisting of light goods, in the Pent, but in the midst of the operation was discovered by the Philistines, and obliged to beat a hasty retreat across the rope-walk to the sea..."
Many people now would say 'beat a retreat' but originally it was 'beat the retreat'. A retreat wasn't just a generalised withdrawal but a specific military procedure established by the British Army. Back in the 16th century, war may have been a brutal hand-to-hand affair but it was conducted under rules of engagement that seem now somewhat quaint. These were laid out in some detail by the British Army's Rules and Ordynaunces for the Warre, dated 1554, and also by Robert Barrett in his Theorike and Practice of Moderne Warres, dated 1598.
One of the niceties that present-day combatants don't enjoy is the custom that at sunset all hostilities ceased and the soldiery went back to their camps to bed. The signal for this was a pattern of drum beats known as The Retreat. So, a retreat wasn't a signal to fall back and give up occupied land as it is now but a signal to retire to bed. The earliest references to the retreat come from the mid-17th century, as in this command from an officer in the Army of James II in 1690:
"The generalle [the signal to get up and start fighting again] to be beate att 3 clock in ye morning. Ye retreate to beate att 9 att night...".