Hector Sanchez had a game-winning RBI single in the bottom of the 12th, Tim Lincecum tossed eight scoreless innings, and the Giants edged the Astros, 3-2, on Saturday at AT&T Park.
Pablo Sandoval and Angel Pagan began the 12th with consecutive singles off of Brett Myers (0-4) before Sanchez ripped a base hit past second baseman Jose Altuve, allowing Sandoval to score the deciding run.
Jeremy Affeldt (1-1) recorded the final out in the top of the 12th to earn the win for the Giants, who moved past the Dodgers and into first place in the National League West with the victory.
They secured their first series win since June 25-27, which came against the Dodgers, and will look for the sweep of this three-game set on Sunday.
Sanchez, whose ninth-inning error allowed the Astros to tie the game, also had an RBI double in the sixth.
His single in the 12th short-hopped Altuve and escaped into the outfield after the second baseman attempted to backhand the sharply hit ball.
Lincecum, who struggled to a 3-10 record before the All-Star break, scattered five hits and notched a season-high 11 strikeouts, but was robbed of the win due to an improbable Houston rally in the ninth.
Santiago Casilla entered the ninth with a 2-0 lead, searching for his 23rd save of the year. Instead he issued a walk to Scott Moore, who scored on Justin Maxwell's two-out double to deep right-center field.
The closer then struck out Chris Snyder with a curveball, but the pitch was wild. The ball escaped up the third base line where Sanchez pounced on it and threw wide to first. Snyder was safe and Maxwell took advantage, scoring the tying run from second base on the play.
"Lincecum did a good job for those first eight innings," admitted Maxwell. "We tacked two on the board in the ninth and we had a chance to win but it just shows a lot about this team and we never give up, regardless."
Although he didn't earn the win, Lincecum turned in arguably his finest performance of the season. In addition to the season-high 11 K's, he also tied a season-high in innings pitched, and scored a run on All-Star Game MVP Melky Cabrera's fifth-inning single.
Additionally, it was just the second time this year that the two-time NL Cy Young Award winner held his opponent scoreless. The other being a seven- inning start against the Dodgers on June 27.
Still, the 28-year-old righty doesn't have a victory to show for the effort. He is now 1-8 in his last 13 starts and the Giants are 5-14 this season when he takes the mound.
"He's had a tough go but it's all about being resilient and bouncing back," said Giants manager Bruce Bochy. "He did tonight and hopefully he'll carry this into the next game."
Rookie Lucas Harrell gave up just one run on five hits with four walks over a five-inning start for the Astros, who lost infielder Jed Lowrie in the third inning due to an sprained right ankle.
Lowrie appeared to be spiked by a sliding Gregor Blanco on a force attempt at second base and had to be assisted off the field.
"Right now it just feels like my whole leg, it's just really sore," said Lowrie about the injury. "He slid into me and my foot got caught underneath his and I twisted it."
Houston fell for the 12th times in its last 13 games.
Lincecum is 5-0 in nine career starts versus the Astros...Moore went 0-for-4. He had hit safely in each of his first nine games with Houston -- a franchise record...Maxwell was reinstated from the 15-day disabled list prior to Friday's contest...Lowrie was acquired from Boston in the offseason. He has appeared in 80 games for the Astros and is batting .253 with 14 homers, 36 RBI and 38 runs scored.