Angela Merkel has secured her fourth term in power after Germany’s Social Democratic party (SPD) agreed to form another “grand coalition” government with the conservative Christian Democratic Union (CDU).

The SPD announcement ends almost six months of uncertainty in German politics, the longest the country has been without a government in its postwar history, and puts Europe’s largest economy in a position to answer the challenges for EU reform laid down by the French president, Emmanuel Macron.

Congratulating the SPD for its “clear result”, Merkel said she was looking forward to “further cooperation for the good of our country,” according to a tweet attributed to her on her CDU party’s account. Macron’s office welcomed the decision, saying “this is good news for Europe.”

But with both Merkel’s party and the SPD facing internal calls for a programmatic reboot and the far-right Alternative für Deutschland (AfD) now the biggest opposition party in parliament, the new government’s stability will likely be tested.

When the Social Democrat treasurer, Dietmar Nietan, announced on Sunday morning that a majority of SPD members had given the green light to a new “GroKo” or “grand coalition”, it was met with quiet relief rather than enthusiastic applause.

“We now have some clarity,” said the Social Democrats’ caretaker leader, Olaf Scholz, a contender for the role of finance minister, speaking at Willy Brandt House in Berlin, the party’s headquarters. “The SPD will enter into government.”

Facebook Twitter Pinterest The SPD treasurer, Dietmar Nietan (centre left), and the temporary party leader,Olaf Scholz, announce the results of the poll at party headquarters in Berlin. Photograph: Kay Nietfeld/AFP/Getty Images

Only muffled cheers from behind the closed doors of the headquarters minutes earlier gave a hint of the weight of expectation that weeks of turmoil had piled on the centre-left party’s leadership.

A majority of 66.02% of 463,723 eligible SPD members voted in favour of renewing the constellation that has governed Germany for the last four years – a more decisive outcome than a narrow vote among delegates in January had indicated, but also a diminished achievement compared with the 76% who had endorsed a grand coalition four years ago.

In the wake of historically disappointing results at federal elections last September, the SPD leader, Martin Schulz, had initially ruled out joining Merkel in government.

But the collapse of talks to form an unorthodox “Jamaica” coalition between Merkel’s conservatives, the pro-business Free Democrats and the Green party forced the German centre-left back to the negotiating table. With its back against the wall, the SPD managed to secure a surprising victory in getting the chancellor to cede control of the influential finance ministry.

A Young Socialists youth wing led by Kevin Kühnert, an energetic 28-year-old, nevertheless put together an effective campaign that encouraged members to reject the deal that the party’s senior leadership had agreed with Merkel’s CDU.

In a German grand coalition or not, SPD’s survival is at risk Read more

Europe’s oldest social democratic party needed to reinvent itself in opposition if it wanted to avoid the fate of the French socialists, the “No GroKo” camp argued.

Watching the announcement of the membership vote result on Sunday, Kühnert expressed his disappointment but indicated that he could continue to play an active role in the debate over the German left’s future.

“Disappointment undoubtedly prevails among many Young Socialists and myself,” he said, adding that the SPD now had to rise to the difficult challenge of renewing its identity while in government. “We will keep a close eye on the government – on both of its sides.”

Following the resignation of the failed candidate for chancellor Martin Schulz, the Social Democrats are likely to next month elect the former labour minister Andrea Nahles as the first female leader in its 155-year history.

A former leader of the Young Socialists who was once seen as a rising star on the party’s left wing, Nahles will try to reverse her party’s decline by establishing a more combative and confrontational approach with the CDU.

This week, the SPD will discuss the contenders for its six ministerial post, with the interim leader, Olaf Scholz, confirming on Sunday that his party will supply three female and three male ministers. Merkel is due to be officially sworn in as chancellor on 14 March.

Facebook Twitter Pinterest Andrea Nahles, who is expected to become the next SPD leader, gives a statement after the vote result. Photograph: Kay Nietfeld/AFP/Getty Images

Scholz, a liberal centrist and the mayor of Hamburg, is mooted to replace the CDU veteran Wolfgang Schäuble in the powerful finance ministry, where he will have to pay lip service to his predecessor’s dogma of a balanced budget while simultaneously meeting a commitment to increase Germany’s contributions to the EU budget, as outlined in the coalition deal.

“We don’t want to prescribe to other European states how they should develop,” Scholz said in a recent interview with Der Spiegel. “Mistakes have certainly been made in that regard in the past. And we need to ask ourselves how to deal with the consequences of Brexit.”

Merkel, meanwhile, has to juggle more immediate challenges on the international stage with a long-term task of arranging her successorship in a way that does not reverse a legacy of moving the CDU into the centre of the political spectrum.

The new party secretary, Annegret Kramp-Karrenbauer, looks like the centrist candidate best equipped to moderate calls for a “conservative revolution” from the Bavarian Christian Social Union (CSU), which will be in charge of the interior ministry, and the ambitious new health minister, Jens Spahn.

In the short run, the various political factions that make up Germany’s government will have to learn to pull together to face a looming twin threat to the country’s important car industry. Merkel will have to draw on all of the diplomatic know-how acquired in her 13 years in power to find a response to Donald Trump’s threat to tax Europe-made cars.

Closer to home, Merkel and her ministers also have find innovative ways to avert bans on diesel cars in highly polluted cities such as Stuttgart or Düsseldorf, rendered legal by a court ruling last week, which would affect millions of drivers.