Madison Bumgarner (7-11) struck out 10 and walked one in seven innings. The lefty was one strikeout short of his career high, which came against Cleveland on June 26.
Huff also had an RBI double to help chase James McDonald (7-6) after six innings. McDonald allowed three runs and four hits for the Pirates, who had snapped a 10-game losing streak a night earlier.
With Bumgarner having another superb outing, San Francisco finally gave him a rare source of power.
Stewart sent a fastball from McDonald in the fifth over the left-field wall to give the Giants a 2-0 lead. He sprinted around the bases and was mobbed by teammates in the dugout after his first home run in 140 major league at-bats.
Huff followed with a home run over the brick wall in right in the sixth to put San Francisco ahead 3-0. The homer was his 11th of the season and a big boost for a Giants team playing without injured slugger Carlos Beltran, nursing a strained right hand for the second straight night.
San Francisco's 28 home runs at AT&T Park are the lowest for any team in the majors at home.
Nate Schierholtz, Cody Ross and Orlando Cabrera also drove in a run each to highlight a two-run eighth for the Giants, and they could've had far more. The Giants were 2 for 11 with runners in scoring position.
The Pirates had few scoring chances and saw their best one snatched away.
Ronny Cedeno had a one-out double in the fifth and moved to third with two outs on McDonald's ground out. Then Giants third baseman Pablo Sandoval made a diving stop to rob Andrew McCutchen of extra bases and save a run.
That's sort of how things have gone lately for Pittsburgh, which sat in first place in the NL Central a little more than two weeks ago. Suddenly, they can put consecutive wins together.
NOTES: Giants OF
was scheduled to fly to North Carolina on Wednesday to seek a second opinion on his strained right foot. ... Giants 3B coach Tim Flannery sang the national anthem along with "Further" members Phil Lesh and Bob Weir. ... Pirates RHP
(8-6, 3.05 ERA) will try to rebound from his last start when he takes the mound Thursday opposite Giants LHP
(4-6, 4.10). Karstens allowed nine runs in 3 1-3 innings last week against San Diego. It was the first time he allowed more than three earned runs in a start since April 17.