SAN DIEGO -- Madison Bumgarner struggled in the first inning, allowing two runs on three hits.
Bumgarner, a first-time All-Star, allowed just four hits in seven innings, and Pablo Sandoval hit his first home run since May 21 as the Giants beat the Padres 4-2 Thursday night in a matchup of the worst teams in the weak NL West.
It was the third straight start in which Bumgarner snapped a four-game losing streak.
"That's why he's on the All-Star team, too," said manager Bruce Bochy, who will lead the NL team on Tuesday night. "We needed a lift. Inauspicious start there, but that's what good pitchers do. They limit the damage. He got out of that. What was impressive about Madison he was flat the first inning until he made a nice adjustment and got in a great groove there and pitched great.
"Early, he was getting under the ball and leaving pitches up. But he made a nice adjustment and gave us seven solid innings in a much, much-needed win."
Bumgarner (10-5) struck out six and walked one.
Bumgarner didn't think he made much of an adjustment.
"I felt I made some pretty good pitches there in the first and they just happened to hit them," he said. "I just kept making pitches and throwing strikes, getting ahead. They still hit some balls hard. Just happened to be right at the defenders."
Sergio Romo pitched the ninth for his 21st save in 24 chances.
Romo agreed with Bochy about how much the Giants needed the performance from Bumgarner.
"It's one of those things where we've been spoiled in the past with the caliber of starting pitching," Romo said. "To be able to do that tonight is big for us. We really, really needed it."
The defending World Series champion Giants, who snapped a four-game losing streak, have been in a free fall since June 15. They had lost 19 of 24 games coming in and were a season-high 10 games under .500. They started the night two percentage points behind the Padres and ended it by jumping past San Diego into fourth place.
The Giants are 2-8 in July, with Bumgarner getting both wins.
Everth Cabrera, San Diego's only All-Star, said Bumgarner started throwing his backdoor cutter better after the first.
"He's got such good pitches and he makes you think about his cutter and he fools you with something else," Cabrera said. "He's really tough, probably as tough of a pitcher that we face."
The Padres have lost 16 of 19.
Sandoval got San Francisco's weak offense going in the fourth with a leadoff homer to right-center off Jason Marquis, his ninth.
It was Sandoval's first homer in 101 at-bats. Sandoval, who was on the disabled list from June 9-24 with a strained left foot, was 6 of 55 (.109) before that at-bat.
"I thought I executed the way I wanted to, but what can you say?" Marquis said. "The guy, over the years, has hit some pitches that shouldn't be hit."
The Giants had the bases loaded three times in the sixth and seventh innings but scored only one run.
Marquis took a 2-1 lead into the sixth and struck out Buster Posey leading off the inning before loading the bases and allowing Kensuke Tanaka's RBI single up the middle. Marquis made way for Joe Thatcher, who got Brandon Crawford to hit into a force at the plate. Dale Thayer came on and struck out Bumgarner to end the inning.
The Giants loaded the bases again in the seventh on two singles and a hit batsman, and Thayer again got out of trouble by striking out Belt to end it.
San Francisco finally broke through in the eighth with two runs and three hits off Luke Gregerson (4-5), including an RBI double by Blanco that bounced over the fence in left, and Posey's RBI single to left.
Bumgarner settled down after a rough first inning in which he allowed two runs on three hits. Carlos Quentin hit an RBI double and, after Jesus Guzman was intentionally walked to load the bases, Kyle Blanks drove in a run on a fielder's choice. Bumgarner got Logan Forsythe to line out to second to end the inning.
Marquis failed for the fifth time to get his 10th victory. He allowed two runs and seven hits in 5 1/3 innings, struck out three and walked two.
The Giants have won 10 of Bumgarner's last 11 starts against the Padres. ... The four-game series continues Friday night when right-hander Sean O'Sullivan makes his debut for his hometown Padres against San Francisco RHP Chad Gaudin (2-1, 2.44 ERA). O'Sullivan, who graduated from Valhalla High in suburban El Cajon in 2005, was signed by the Padres as a minor league free agent in December. It will be his first big league start since Sept. 27, 2011, with the Royals. ... Padres 1B Yonder Alonso is expected to be in the starting lineup for Friday's game He has been on the disabled list since May 31 with a broken right hand.
Flashback: Ripken becomes MLB's iron man
On Sept. 6, 1995, Cal Ripken Jr. played in his 2,131st straight game, breaking Lou Gehrig's all-time consecutive games played streak.
Untold tales of Cal Ripken Jr., the Iron Man who saved baseball
Cal Ripken Jr. brought baseball back after the strike with his record-breaking run in 1995. Here are some stories of the summer of 2,131 you might not have heard.
Viewers guide: The night Cal Ripken Jr. became baseball's Iron Man
The sport was still stinging from the 1994 strike, but all was forgotten when the Orioles shortstop broke Lou Gehrig's unthinkable record for consecutive games played.
Tim Kurkjian's baseball fix: Adrian Beltre was so confident, he didn't wear a cup
Playing third base in the majors and not wearing a cup seems like a bad idea. But Adrian Beltre, a future Hall of Famer, believed his defense was good enough to save him.
Second-Chance World Series, Round 2: Which clubs advance to our Elite 8?
Are the 1993 Phillies making the most of their second chance? Are the 2001 Mariners unstoppable? The field narrows in our runoff sim of MLB's best non-champs.
Why Stephen A. thinks MLB's plan to start the season is implausible
Stephen A. Smith reacts to the idea of all 30 teams being relocated to the greater Phoenix area to start the MLB season in May.