TOURS, France — Germany’s Marcel Kittel overtook Mark Cavendish at the line Thursday to win the 12th stage of the Tour de France, and Chris Froome avoided a late crash in the main pack to preserve his big lead.

Cavendish moved in front but Kittel gained ground and just beat him to the line to earn his second stage win in three days and third of this race.

“As we say in Germany, good things come in three,” Kittel said. “It was close. I don’t know what to say. It was a real sprint today, that’s why I’m so happy.”

German riders have won five stages so far, with Tony Martin capturing Wednesday’s time trial and Andre Greipel also winning a sprint stage.

“It’s a big achievement for me, my team, and for German sprinters as well,” Kittel said.

Froome leads Alejandro Valverde by 3 minutes, 25 seconds and is 3:54 ahead of two-time former champion Alberto Contador. Froome is on track to become the second British rider to win the race — Bradley Wiggins won it last year, with Froome runner-up.

“At the moment I’m trying to save as much energy as possible for Mont Ventoux at the end of the week and then the Alps next week,” said Froome, who dominated the first big mountain stage of the race in the Pyrenees last Saturday.

The field rolled through vineyards and alongside the Chinon forest on a 136-mile route from Fougeres to Tours in the Loire valley, a picturesque region dotted with imposing French chateaus — the spiral-towered Chateau d’Usse, which dates from the 11th century, and the 16th century Chateau d’Azay-le-Rideau, which rests on the water.

About 20 riders were caught in a crash near the end, with some stuck under the bikes of others as wheels and frames jutted out at all angles.

“It’s always like that at the end before a sprint,” Froome said. “It’s scary for everyone.”

Cavendish, the Tour’s best sprinter two years ago, has had a frustrating race. The British sprinter looked set to clinch his 25th career stage win of the Tour after his Omega Pharma QuickStep teammate Gert Steegmans got him in a great position to attack. But Cavendish did not have the legs to hold on and Kittel beat him by half a wheel length.

“I can go back and look over it but he was just faster,” Cavendish said. “(My teammates) delivered me at the right time. I was just beaten by a better guy.”

Peter Sagan finished third and still has a comfortable lead as he bids to win the contest for the sprinters’ green jersey. Sagan has 307 points and leads Cavendish by 96 points. Greipel is in third place and Kittel is fourth, but both are more than 100 points from Sagan.

Looking to keep their riders near the front of the main pack and limit the risk of them being caught in crashes, Froome’s Sky and Contador’s Saxo-Tinkoff squads took turns pushing from the front.

“That is the best position to be in because there are crashes everywhere,” Froome said. “I did hear the crash behind me.”