ST. LOUIS — Aledmys Diaz got the green light on 3-0. It was no big surprise, considering how much the rookie has meant to the St. Louis Cardinals.

Diaz lined a tying, three-run shot off the foul pole in the fifth inning, one of four homers in a span of nine at-bats against Jeff Samardzija, and the Cardinals rallied past the San Francisco Giants 7-4 on Saturday night.

"I just knew it was pretty close and I kept running," Diaz said. "I looked at the umpire and he said, 'fair ball,' so that's good."

Brandon Moss got the long-ball binge started with his 11th leading off the fifth. Stephen Piscotty and Matt Adams hit consecutive homers in a matter of three pitches to open the sixth for a 6-4 cushion.

The 25-year-old Diaz got the starting shortstop job after Ruben Tejada was injured at the end of spring training and will stay there when Jhonny Peralta returns next week. He's batting .324 with eight homers, 29 RBIs.

"It's not just a one-size-fits-all when you get to a 3-0 green light," manager Mike Matheny said. "It didn't look like he tried to do too much, because at times you'll see a 400-foot shot pulled foul when a guy gets too big on an advantage count."

The rally rescued Michael Wacha, who left trailing 4-0 after five seemingly on track for a seventh consecutive loss. Wacha has allowed 23 earned runs in his last 23 innings.

"I had a good feel for it," Wacha said. "I just gave up some untimely doubles and triples, but overall I know I've just got to trust the process."

Samardzija (7-4) had given up five homers in 80 innings all season, and just one in his previous five outings. He opened with four routine innings, permitting three singles and no runner past first base.

"You've got a four-run lead there, you've got to make it stick," Samardzija said. "So, that hurts."

Manager Bruce Bochy said Samardzija, who allowed 29 homers season last season to lead the American League, simply lost command.

"He was cruising there," Bochy said. "Balls started drifting toward the centre of the plate."

Tyler Lyons (2-0), Seung Hwan Oh and Kevin Siegrist each worked a hitless inning before Trevor Rosenthal set the side down in order in the ninth for his ninth save in 10 chances.

Diaz was sitting on a 3-0 pitch for his eighth homer that banged off the foul pole just below the third deck in left, a drive estimated at 426 feet. Piscotty and Adams have seven homers apiece.

"A lot of things happened there, guys keeping it moving," Matheny said. "Just a great at-bat. Diaz continues to impress us."

Joe Panik doubled twice with an RBI, Brandon Crawford doubled and tripled and Samardzija had an RBI single for the NL West-leading Giants, who are 18-5 in their last 23 games.

"You're going to have games like that," Crawford said. "It did happen pretty fast, though."

TRAINER'S ROOM

Giants: Matt Cain (hamstring) is scheduled to throw his second bullpen session Sunday. If all goes well, he could throw a simulated game Wednesday that could be the final test before returning to the rotation. Cain threw 20 pitches off a mound on Friday.

Cardinals: Peralta (thumb) remains on track to be activated from the DL on Tuesday. He started Saturday for Class A Palm Beach, but will likely primarily play at 3B when he returns due to the emergence of Diaz.

GET A HIT

Wacha had been 0 for 19 with 10 strikeouts this year before a swinging bunt single in the third.

UP NEXT

Giants: Jake Peavy (2-5, 6.34) is coming off his best start, pitching one-hit ball for seven innings against Atlanta. The right-hander will get extra rest due to off days for the team and pitch again in next Sunday's nationally televised game.

Cardinals: Carlos Martinez (5-5, 3.69) struck out eight in eight scoreless innings his last start at Milwaukee, ending a career-worst five-game losing streak.