A gift horse is a horse that was a gift, quite simply. When given a horse, it would be bad manners to inspect the horse's mouth to see if it has bad teeth. This can be applied as an analogy to any gift: Don't inspect it to make sure it matches some standard you have, just be grateful!
The New Partridge Dictionary of Slang and Unconventional English (2008) says:
have a cow to become emotionally overwrought; to lose control US, 1966.
Speaking of Animals: A Dictionary of Animal Metaphors (1995) by Robert Allen Palmatier says:
HAVE A COW to have a cow. To have an anxiety attack. Source: COW. WNNCD: O.E. On the TV show "The Simpsons," Bart Simpson says "Don't have a cow, man!" meaning "Don't get all upset about it." Bart is likening an anxiety attack to giving birth to a cow - a frightening thought. Normally cows are the ones that give birth to cows - i.e., bull calves and heifer calves. Compare Have Kittens.
WNNCD is Webster's Ninth New Collegiate Dictionary (1983) and O.E. means Old English, but the O.E. must apply to the plain word cow rather than the phrase. (The OED dates cow to Old English.)
This Yahoo Voices article - Idioms Unpacked: "Don't Have a Cow" - also claims it means to (not) give birth to a cow, which would be distressing for a human to do. It lists a number of references at the end, but I've not followed them.
A quick search of Google Books shows this snippet dated 1962 from Field and Stream, Volume 67:
"Oh, don't have a cow," Chip said confidently. "They just haven't begun to fly yet."
"If they don't fly soon," Andy insisted, "they're going to need landing lights."
(Care must be taken with Google Books' snippets as they're often mislabelled, but following the story text we find an advert for a "NEW 1963 book of homes", so it's likely from 1962 or 1963.)
Searching Subzin.com, the first film I found to use the phrase was Sixteen Candles (1984):
00:39:00 I don't know, Jake.
00:39:02 I'm getting strange signals. Well, they're not comin' from me.
00:39:05 Everything's fine. Don't have a cow.
00:39:08 Okay.
00:39:10 Just remember one thing.
Edit: Good timing, as the OED have just released an update to the dictionary containing the phrase for the first time. The first quotation is from a 1959 newspaper:
1959 Denton (Texas) Record-Chron. 26 Mar. 3/2
He won't let me watch rock 'n roll shows... He'd
have a cow if he knew I watched 77 Sunset Strip.
Best Answer
By far the earliest match for "punch a gift horse" in Google Books search results is from a 1972 issue of National Lampoon, a U.S. satirical magazine that grew out of The Harvard Lampoon, a satirical university student publication. Here is the snippet result that Google Books reports:
Unfortunately, the snippet window that Google Books provides for this excerpt doesn't show any text at all. Still, given even the very limited context in which the phrase appears, it seems clear that this instance is an intentionally garbled play on the much older "never [or do not] look a gift horse in the mouth," which Christine Ammer, American Heritage Dictionary of Idioms, second edition (2013), says reached English around the year 1500:
In the National Lampoon excerpt, the author leads with "never punch a gift horse in the mouth," garbling the proverb with a common descriptive phrase ("punch [someone] in the mouth"), then mashes together two unrelated idiomatic phrases ("ships passing in the night" and "things that go bump in the night") as "ships that go bump in the night," and then descends into complete incoherence ("gaga babruuuu," etc.). Ha ha.
The next-earliest instance I've been able to find is from an Elephind newspaper database search, which yields this instance from "Without Leonard, the Spotlight Shines Marvelously on Hagler," in the Columbia [Missouri] Missourian (February 8, 1983):
The Missourian is the student newspaper of the University of Missouri School of Journalism, one of the most highly regarded Journalism programs in the United States.
The expression next reappears as an "honorable mention" entry in the results of "New York Magazine Competition Number 709" in New York Magazine (January 19, 1991), "in which you were asked for Near Misses. Titles, names, phrases, and the like":
It is possible that these two and all or most subsequent instances of "don't punch a gift horse in the mouth" ultimately derive from people exposed to the 1972 magazine occurrence. A considerable number of college- and high-school-age young people did read National Lampoon back in the day. More likely, though, the periodic recurrences of the expression during the last three decades of the twentieth century are instances of absurdist minds thinking alike.