The Phillies had lost four straight at home after sweeping a three-game series in Atlanta to move a season-best two games over .500.
Hamels (8-2) allowed two runs and five hits to become the NL's first eight-game winner. Relying on a sharp fastball and his trademark change-up, the second-year left-hander kept the Giants off-balance all night. He struck out five and didn't allow a walk.
With one out and a runner on in the ninth, Hamels threw his change-up five straight times to Bonds. Finally, the feared slugger struck out swinging.
"That at-bat should tell you how good that kid's change-up is," catcher Rod Barajas said. "To throw the same pitch five times in a row and have Barry do nothing with it should tell you something. Any time he wants to throw that pitch, I'm fine with it."
Hamels wasn't going to give in and throw a fastball to Bonds, who finished 2-for-4.
"I saw he was ahead of the change-up, so I kept throwing it and decided to drop it a little lower," Hamels said.
Bonds, who also dropped a fly ball in left field, certainly was impressed after facing Hamels for the first time. He remains stuck on 746 career homers, 10 shy of breaking Hank Aaron's record.
"He's good. He's a pretty good pitcher," Bonds said.
Giants starter Noah Lowry (5-5) gave up four runs and five hits in seven innings.
Kevin Frandsen homered to left in the first inning to put the Giants ahead 1-0. But the Phillies answered with a pair of runs in the fourth and fifth.
Jayson Werth reached on an infield single leading off the bottom of the fourth. Chase Utley followed with a double to right-center. Ryan Howard then hit a high drive to deep left that Bonds dropped on the warning track for his first error this season. Werth scored on the sacrifice fly, Utley went to third and Howard advanced to second.
Fans cheered wildly as Bonds looked up at the scoreboard to watch the replay. An inning earlier, they chanted at Bonds: "You took steroids." Pat Burrell's sacrifice fly scored Utley to give the Phillies a 2-1 lead.
"It just hit the palm of my glove," Bonds said. "I went back, turned my head then lost it for a split second."
Jimmy Rollins hit an RBI triple in the fifth and Werth followed with Philadelphia's third sacrifice fly to make it 4-1. The Phillies aren't known for moving runners on the bases without getting hits.
"That's how you play the game," manager Charlie Manuel said.
Bonds ripped a double to center leading off the seventh. It gave Bonds 5,863 total bases to push him past Ty Cobb for fourth place on the all-time list.
"Maybe we were a little overaggressive," Giants manager Bruce Bochy said. "(Hamels) has good stuff. He's one of the fine young pitchers in the league."
- A crowd of 45,153 was the most this season and the fourth-largest in the four-year history of Citizens Bank Park.
- The Phillies are 21-14 in Hamels' 35 career starts. He's 17-10.
- Lowry hasn't allowed more than four runs in any of his 11 starts.
- Hamels has both of Philadelphia's complete games this season.