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.
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/MLBandhttps://twitter.com/AP-Sports
How a fan's viral video turned Aaron Boone's Yankees into 'savages'
It's all over social media and on T-shirts worn by players and sold in the team store. So why did the skipper's rant turn into a rallying cry? You can thank a guy called Jomboy.
Angels' Trout looks to punish White Sox again
The legend of Mike Trout continues to grow.
A's must beat Verlander, Astros to gain ground
Two veteran pitchers who have had trouble keeping the ball in the park this season seek to cool hot-hitting opponents when the Houston Astros host the Oakland Athletics on Friday night for the second time in their four-game series.
Samardzija, Giants aim to stay hot in Arizona
The San Francisco Giants' rise to contenders in the National League wild-card race has coincided with the resurgence of veteran right-hander Jeff Samardzija.
Rockies look to build off win as Marlins visit
In a season that went from playoff hopes to now looking ahead to 2020, the Colorado Rockies got a ray of sunshine Wednesday afternoon.
Royals' Montgomery tasked with slowing Mets, Syndergaard
separated by an offseason and a parade -- at the end of the 2015 season and the beginning of the 2016 season, the Kansas City Royals will host the New York Mets, beginning Friday night in the first of a three-game series.