SAN FRANCISCO -- Evan Longoria blamed himself for being overly aggressive in his first two at-bats against St. Louis pitcher Jack Flaherty, a byproduct of the spacious field at Oracle Park and Flaherty's dominance early on.
The third time around, San Francisco's slugging third baseman slowed down just enough to catch up to one of the few mistakes Flaherty made.
Longoria homered with one out in the seventh inning to break up Flaherty's bid for a no-hitter, and the Giants held on to beat the Cardinals 1-0 on Sunday.
"This ballpark has a tendency to make you feel like you have to add a little bit and be a little bit more perfect," Longoria said after his fifth home run in seven days. "I was more focused on trying to get on base any way I can. Any guy will tell you it's better off that way. Home runs are usually accidents."
The Giants managed only one baserunner against Flaherty until Longoria drove an 0-1 pitch into the left field stands. It's Longoria's 12th of the season and fifth in July. He hit four during a three-game series in San Diego earlier in the week.
San Francisco won for the seventh time in nine games despite getting only two hits off Flaherty. Alex Dickerson followed Longoria's drive with a bloop single to left that glanced off the glove of All-Star shortstop Paul DeJong.
"You're just hoping somebody gets a mistake, and that's what happened," Giants manager Bruce Bochy said. "That's a great game for us and a great way to end the first half."
Flaherty was masterful before Longoria's home run. The 23-year-old retired his first 12 batters before walking Alex Dickerson leading off the fifth. He struck out two of the next three and got Kevin Pillar to foul out.
But the Cardinals couldn't muster much offensively and Flaherty became the latest St. Louis pitcher to come up short in a no-hit bid. Adam Wainwright twice had no-hitters broken up in the sixth inning this season, against Cincinnati on April 13 and the Cubs on June 2.
Flaherty (4-6) struck out six and allowed two hits in seven innings.
"That pitch was a mistake," Flaherty said. "The wind is crazy here and he didn't miss it. We go out and crush two balls that inning before it and we just hit it to the wrong part of the park."
Jeff Samardzija (6-7) pitched seven scoreless innings and allowed four hits to beat the Cardinals for the first time in four career starts at Oracle Park.
Samardzija took a no-hitter into the fifth, pitched out of a jam in the sixth after allowing a one-out double, then got Matt Wieters to fly out with two on and two out in the seventh.
Sam Dyson retired three batters. All-Star closer Will Smith pitched the ninth and is 23-for-23 in save opportunities.
Yairo Munoz had two hits for St. Louis. The Cardinals finished 4-5 on their road trip and head into the All-Star break two games behind first-place Chicago in the NL Central.
Pillar had a pair of defensive gems in center field on back-to-back plays in the seventh inning. Pillar made a spectacular diving catch to take away a hit from Paul Goldschmidt in right-center, then ran down Dexter Fowler's deep fly at the warning track. The crowd at Oracle Park gave Pillar a standing ovation after each catch.
"The kid's going to do well. He's going to make a ton of money in this game if he keeps doing what he's doing. That was rather impressive to watch." Samardzija, talking about Flaherty's future.
Cardinals: 2B Kolten Wong was held out with left calf tightness. He's expected to be back in the lineup after the All-Star break.
Giants: Madison Bumgarner was still sore a day after getting hit near his left elbow by a 98 mph line drive but thankful the situation wasn't worse. Barring a setback, the plan is to have Bumgarner throw a bullpen Thursday then start Saturday in Milwaukee. . Stephen Vogt had X-rays taken on his left knee after the veteran catcher was hit by a foul ball. ... Catcher Buster Posey and SS Brandon Crawford were rested. . RHP Tyler Beede will throw a bullpen with Double-A Richmond and could start for the Giants next Sunday.
Cardinals: Wainwright (5-7, 4.31 ERA) will face Arizona on Friday in St. Louis. Wainwright's eight career wins against the Diamondbacks are the most by any Cardinals pitcher.
Giants: RHP Shaun Anderson (3-2, 4.23) makes his first appearance against the Brewers on Friday in Milwaukee. Anderson allowed four runs in four innings of his most recent start and has one win since June 12.
More AP MLB: https://apnews.com/tag/MLB and https://twitter.com/AP—Sports