The Giants overtook the Dodgers for first place in the National League West during the sweep, which was the team's third of the season. The Astros have now lost 13 of their last 14 games, including back-to-back one-run games against the Giants.
"To get a sweep is hard to do, I don't care who you're playing," manager Bruce Bochy said. "These games were hard-fought games. They played well, and they have a lot of good young players, and we didn't do a lot with their pitching. They really fought us hard these last couple games, and they really could have gone either way."
In his first start against the Astros since his June 13 perfect game against them, Cain gave up two runs (one earned) while striking out six batters in 6 1/3 innings. His 13th quality start of the season followed strong outings from Madison Bumgarner (one earned run in seven innings) and Tim Lincecum (eight shutout innings) in the series.
"He's probably one of the best pitchers I've faced this year, easily in the top three," Astros third baseman Chris Johnson said of Cain. "Not only does he have really good stuff, he competes harder than just about anybody we face. You put those things together like that, it's pretty tough."
With Melky Cabrera in Florida for the birth of his child, Posey came up big for the Giants offense. He knocked in Pablo Sandoval in the fifth to break a 2-2 tie after Sandoval had reached with a double to left field. Earlier, Posey had plated the Giants' first run with a single in the first inning that scored Gregor Blanco. A Brandon Belt walk later in the first allowed Emmanuel Burriss to score to give the Giants an early 2-0 lead.
The All-Star catcher has gone 5-for-10 with four RBIs in the three games since the break.
"I feel good," Posey said. "I'm just trying to keep a good approach and have a plan every time I go up there."
Posey had three of the Giants' seven hits on Sunday, his fifth game with three hits, and his 13th multi-RBI game gives him a team-best 47 RBIs on the season.
"We had to pitch well because we're not swinging the bats like we can," Bochy said. "Buster had a big day, thankfully."
After the Giants took a two-run lead in the first inning, the Astros got a run off Cain on his throwing error on Jordan Schafer's two-out bunt in the third inning, with Marwin Gonzalez coming in to score after Belt wasn't able to handle Cain's throw.
The Astros tied the game in the next inning when Johnson doubled home Brian Bogusevic, who had reached with a single.
Cain departed with one out in the seventh, with left-hander Jeremy Affeldt inheriting runners on first and third before coaxing a foul pop out from pinch-hitter Brian Bixler and and a flyout from Justin Maxwell, also pinch-hitting.
"Affeldt picked me up big time with guys on first and third with one out," Cain said. "Those were big outs to keep the momentum on our side."
The Giants scored all three runs off Astros starter Bud Norris in his six innings pitched, taking advantage of his six walks in the game.
Now having won 10 of their last 13 games at home, the Giants have the strong start to the second half that Bochy sought after the team's road skid cost it the division lead.
"I thought it was important for us to come out of the shoot playing well," Bochy said. "Coming off a tough road trip, we needed to get back on track. We know we have our work cut out, but it's nice to get a sweep and get some momentum. We're going to need to turn it up a notch with the offense to get this done."