Magnus Hirschfeld and two cross-dressers, outside the Institute for Sexual Science. Courtesy Photo Hirschfeld / Voilà / Gallimard

On August 29, 1867, a forty-two-year-old lawyer named Karl Heinrich Ulrichs went before the Sixth Congress of German Jurists, in Munich, to urge the repeal of laws forbidding sex between men. He faced an audience of more than five hundred distinguished legal figures, and as he walked to the lectern he felt a pang of fear. “There is still time to keep silent,” he later remembered telling himself. “Then there will be an end to all your heart-pounding.” But Ulrichs, who had earlier disclosed his same-sex desires in letters to relatives, did not stop. He told the assembly that people with a “sexual nature opposed to common custom” were being persecuted for impulses that “nature, mysteriously governing and creating, had implanted in them.” Pandemonium erupted, and Ulrichs was forced to cut short his remarks. Still, he had an effect: a few liberal-minded colleagues accepted his notion of an innate gay identity, and a Bavarian official privately confessed to similar yearnings. In a pamphlet titled “Gladius furens,” or “Raging Sword,” Ulrichs wrote, “I am proud that I found the strength to thrust the first lance into the flank of the hydra of public contempt.”

The first chapter of Robert Beachy’s “Gay Berlin: Birthplace of a Modern Identity” (Knopf) begins with an account of Ulrichs’s audacious act. The title of the chapter, “The German Invention of Homosexuality,” telegraphs a principal argument of the book: although same-sex love is as old as love itself, the public discourse around it, and the political movement to win rights for it, arose in Germany in the late nineteenth and early twentieth centuries. This message may surprise those who believe that gay identity came of age in London and New York, sometime between the Oscar Wilde trials and the Stonewall riots. The brutal repression of gay people during the Nazi period largely erased German gay history from international consciousness, and even from German memory. Beachy, a historian who teaches at Yonsei University, in Seoul, ends his book by noting that Germans hold gay-pride celebrations each June on what is known as Christopher Street Day, in honor of the street where the Stonewall protest unfolded. Gayness is cast as an American import.

Ulrichs, essentially the first gay activist, encountered censorship and ended up going into exile, but his ideas very gradually took hold. In 1869, an Austrian littérateur named Karl Maria Kertbeny, who was also opposed to sodomy laws, coined the term “homosexuality.” In the eighteen-eighties, a Berlin police commissioner gave up prosecuting gay bars and instead instituted a policy of bemused tolerance, going so far as to lead tours of a growing demimonde. In 1896, Der Eigene (“The Self-Owning”), the first gay magazine, began publication. The next year, the physician Magnus Hirschfeld founded the Scientific-Humanitarian Committee, the first gay-rights organization. By the beginning of the twentieth century, a canon of gay literature had emerged (one early advocate used the phrase “Staying silent is death,” nearly a century before aids activists coined the slogan “Silence = Death”); activists were bemoaning negative depictions of homosexuality (Thomas Mann’s “Death in Venice” was one target); there were debates over the ethics of outing; and a schism opened between an inclusive, mainstream faction and a more riotous, anarchistic wing. In the nineteen-twenties, with gay films and pop songs in circulation, a mass movement seemed at hand. In 1929, the Reichstag moved toward the decriminalization of homosexuality, although the chaos caused by that fall’s stock-market crash prevented a final vote.

Why did all this happen in Germany? And why is the story not better known? Beachy, focussing on Berlin’s social fabric, doesn’t delve too deeply into larger philosophical questions, but the answers are hardly elusive. The inclination to read German history as an extended prelude to Nazism—the “heading for Hitler” narrative—has tended to exclude countervailing progressive forces, especially those of the Wilhelmine period, from 1871 to 1918. The towering legacy of German idealism and Romanticism, which helps to explain why the gay-rights movement took root in Germany, has itself become somewhat obscure, especially outside the German school system. And so we are surprised by the almost inevitable. Nowhere else could a figure like Ulrichs have made his speech, and nowhere else would cries of “Stop!” have been answered by shouts of “No, no! Continue, continue!”

In Leontine Sagan’s 1931 film “Mädchen in Uniform,” the first sympathetic portrayal of lesbians onscreen, a boarding-school pupil named Manuela plays the title role in a school production of Friedrich Schiller’s 1787 play “Don Carlos,” an emblematic Romantic tale of forbidden love and resistance to tyranny. “A moment passed in paradise is not too dearly bought with death,” Manuela declaims onstage, conveying Don Carlos’s love for his stepmother. Afterward, fortified by punch, Manuela announces her love for one of her teachers, precipitating a scandal. The episode suggests the degree to which the German cultural and intellectual tradition, particularly in the Romantic age, which stretched from Goethe and Schiller to Schopenhauer and Wagner, emboldened those who came to identify themselves as gay and lesbian. (“Schiller sometimes writes very freely,” an elderly woman worriedly observes in Sagan’s film.)

Close to the heart of the Romantic ethos was the idea that heroic individuals could attain the freedom to make their own laws, in defiance of society. Literary figures pursued a cult of friendship that bordered on the homoerotic, although most of the time the fervid talk of embraces and kisses remained just talk. But the poet August von Platen’s paeans to soldiers and gondoliers had a more specific import: “Youth, come! Walk with me, and arm in arm / Lay your dark cheek on your / Bosom friend’s blond head!” Platen’s leanings attracted an unwelcome spotlight in 1829, when the acidly silver-tongued poet Heinrich Heine, insulted by anti-Semitic remarks that Platen had lobbed at him, satirized his rival as a womanly man, a lover of “passive, Pythagorean character,” referring to the freed slave Pythagoras, one of Nero’s male favorites. Heine’s tone is merrily vicious, but he inserts one note of compassion: had Platen lived in Roman times, “it may be that he would have expressed these feelings more openly, and perhaps have passed for a true poet.” In other words, repression had stifled Platen’s sexuality and, thus, his creativity.

Gay urges welled up across Europe during the Romantic era; France, in particular, became a haven, since statutes forbidding sodomy had disappeared from its books during the Revolutionary period, reflecting a distaste for law based on religious belief. The Germans, though, were singularly ready to utter the unspeakable. Schopenhauer took a special interest in the complexities of sexuality; in a commentary added in 1859 to the third edition of “The World as Will and Representation,” he offered a notably mellow view of what he called “pederasty,” saying that it was present in every culture. “It arises in some way from human nature itself,” he said, and there was no point in opposing it. (He cited Horace: “Expel nature with a pitchfork, she still comes back.”) Schopenhauer proceeded to expound the dubious theory that nature promoted homosexuality in older men as a way of discouraging them from continuing to procreate.

Not surprisingly, Karl Heinrich Ulrichs seized on Schopenhauer’s curious piece of advocacy when he began his campaign; he quoted the philosopher in one of his coming-out letters to his relatives. Ulrichs might also have mentioned Wagner, who, in “Die Walküre” and “Tristan und Isolde,” depicted illicit passions that many late-nineteenth-century homosexuals saw as allegories for their own experience. Magnus Hirschfeld, in his 1914 book “The Homosexuality of Men and Women,” noted that the Wagner festival in Bayreuth had become a “favorite meeting place” for homosexuals, and quoted a classified ad, from 1894, in which a young man had sought a handsome companion for a Tyrolean bicycling expedition; it was signed “Numa 77, general delivery, Bayreuth.” Ulrichs had published his early pamphlets under the pseudonym Numa Numantius.