Spain’s Alberto Contador rides in the pack during the 12th stage of the Tour de France, with the start in Fougeres and finish in Tours, western France, on Thursday.

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.”

Saxo-Tinkoff tried to pull away with about 3 miles left, but Ian Stannard helped Froome catch them. Moments later, Norway’s Edvald Boasson Hagen was among the 20-odd riders who were sent flying backwards, upwards and sideways off their bikes. Boasson Hagen was clutching his shoulder when he got back up and his right elbow was bloodied.

“We hope he’ll be able to continue,” Sky team manager Dave Brailsford said.

Then, with the finish in sight, Froome survived a delicate moment when he appeared to be nudged by Tom Veelers.

“I must have hit 10 guys on the run-in today,” Froome said. “I don’t really think too much about it, that’s the way it is.”

A five-man breakaway surged ahead early on. The group consisted of Spanish veteran Juan Antonio Flecha, Italians Francesco Gavazzi and Manuele Mori and Frenchmen Anthony Delaplace and Romain Sicard. They built a lead of about nine minutes after about an hour of racing.

Andy Schleck, the 2010 Tour winner, who had a disappointing time trial Wednesday, had to change his bike after a puncture and scampered back to rejoin the main pack.

As for Froome, his mind was already turning toward Sunday and Mont Ventoux.