How did the German Empire and the Austro-Hungarian Empire become staunch allies in the years preceeding WW1 when just a few decades earlier they were the biggest contenders for German hegemony and therefore bitter rivals ?

Germany ended up allying with the Habsburg Empire prior to the 1st world as part of a package rather then their first choice!

When Otto von Bismarck was constructing his new Europe after uniting Germany he astutely recognized that the French would come back eventually seeking Alsace-Lorraine which was annexed to the German Empire following the Franco-Prussian war. He also came to the observation that France as usual would look to align itself with one of the neighboring great powers of Germany who also had a bone to pick with Germany. Bismarck had good reason to believe the French would ally either the Russians or Austrians and in response Germany would be left to give serious concessions to whichever power remained aloof so as to be allied.

A man of noted initiative and decisiveness Bismarck knew that simply couldn't do to preserve the general European peace. So he brought both Austria-Hungary and the Russian Empire into an alliance with Germany called the league of 3 Emperors. This alliance at the time was the strongest and most potent alliance in the world. It enabled Germany to isolate the French from 1873 to it's conclusion in 1887.

The alliance was simple in function as it was to keep the question of Polish independence unspoken and the French unable to take their revenge on Germany. What brought the alliance down was the same thing that brought the Empire's of Europe into a world war later. As Bismarck put it: "Some dammed foolish thing in the Balkans." The Russians and Austrians sincerely mistrusted each other over their differences in the Balkans and while the League of the Three Emperors enabled Austria to annex Bosnia the Austrian machinations in the region became something Russia could not tolerate.

Bismarck made attempts to recover his mechanism of control but by it's final collapse in 1887 Bismarck's career was nearing it's end and soon Kaiser Wilhelm II ascended to the throne. The two infamously did not get along and Wilhelm's romanticized perspective to Austria being kindred spirits while the Russians were a threat soured any prospects of rekindling their mutual alliance. Germany in response founded the Dual Alliance with Austria-Hungary which turned into the Triple Alliance of Germany-Austria-Hungary-Italy shortly before the 1st world war.

After Bismarck was dismissed what he had feared materialized with France and Russia soon becoming heavily aligned and allied against Germany. While the Germans were left with their closest culturally but weakest possible ally among the great powers in Austria-Hungary. Wilhelm would interact with the Habsburgs quite often but actually did not get along well at all.

The two ruler personalities, Franz Joseph and Wilhelm II were separated by conspicuous differences in age: Wilhelm belonged to a younger generation; he was almost the same age as Franz Joseph’s son Crown Prince Rudolf, with whom Wilhelm was often compared. The friendship between the two princes was much propagandized, purporting to reflect the political rapprochement between the two empires. But in truth their relationship was characterized by glaring differences of opinion and chilly feelings of rivalry, which was to escalate dramatically when Wilhelm came to power in 1888, while Rudolf had persevere in his waiting position.

Wilhelm had an extremely reactionary understanding of monarchic rule, while Rudolf was an advocate of liberal ideas. The Hohenzollern prince was the epitome of Prussiandom: militarist and snappish, pompous and German-nationalistic with an obtrusively chauvinist vein, he acted out of a position of bullying strength. Wilhelm’s arrogance reflected the self-assurance of Germany as the new foremost power in Central Europe; this was seen by the Habsburg Monarchy as a threat, its self-image shattered after the painful defeat in the process of German Unification. Vienna reacted with a mixture of adulation and stubborn insistence on the venerable traditions of Habsburg rule. Particularly German-nationalist circles saw their “true” ruler in Wilhelm, and when the new German emperor visited Austria in October 1888 to celebrate his accession, the Austrian authorities had their work cut out for them to curb the ovations so as to avoid embarrassing scenes for Franz Joseph.

Wilhelm’s relationship to Archduke Franz Ferdinand was distinctly better: the two men were closer in ideology and occasionally developed a real sympathy for each other, mostly thanks to Wilhelm’s gentlemanly behavior to the wife of the Austrian successor to the throne, who was not of the social rank regarded as befitting to marry a member of the Imperial House. When the German Kaiser was the first monarch to invite Sophie expressly to a state visit – until then she had never made an official appearance – the ice was broken, and Franz Ferdinand felt obliged to feel grateful towards Wilhelm, despite certain political differences.

His relationship to Austria-Hungary, Germany’s closest ally based on the Dual Alliance of 1879, was burdened by his lack of understanding for the difficult problems of the heterogeneous multi-ethnic State. Wilhelm did not mince his words when judging the situation in the dual monarchy. The German Kaiser openly criticized the military and political weakness of his ally and believed in general he had diagnosed its malady in the “sloppiness typical of the country.”

Anyway the best way to characterize it is the old adage "You can choose your friends but you can't choose your family." and this applied best to Austria-Hungary and Germany. The German nobility of both nations felt a kinship in nationalism that no other powers had, this ensured Germany would work with Austria following Bismarck's resignation to the end of the German and Austro-Hungarian Empire's themselves.

/r/AskHistorians Thread