Brian Bixler won it for the Astros with a three-run home run in the bottom of the 10th inning, long after Hernandez and Hamels had put up a string of zeroes.
The 37-year-old Hernandez, trying to nail down a spot in the Astros' rotation, pitched five shutout innings, scattering five hits, including a double by Hunter Pence.
"I threw five innings and 60-some pitches, so I'm pretty close, 85 or 90 percent," said Hernandez, baseball's active career leader with 474 starts. "One more bullpen and I could be ready."
Manager Brad Mills said Hernandez probably could have pitched another couple of inning if he'd needed to.
"I threw the ball down, had a good sinker today. Everything's going the way I want it," Hernandez said. "My curveball was breaking pretty good, too. The arm is feeling very fresh and very good right now. That's what I'm looking for."
Hamels also worked five innings in his third spring start and was satisfied with his changeup, which he used in every inning.
"I'm still trying to get all the rest of the pitches in, but it's nice getting into a situation where I can throw the changeup just reading the swings off the hitters," he said. "I'll work on throwing it to different areas and getting more of the action that I know I have. It's coming."
The fastball needs a little work. Carlos Lee hit one for a double in the fourth inning, leading to the only run off Hamels.
"Once I'm really in a good rhythm, I'm able to throw it three, four, five times in a row," said Hamels, who gave up four hits and struck out four. "But right now I'm getting a really good pitch and then I'm leaking the second one a little bit and it fades over the plate into the barrel."
He will make one more start before pitching as though it were the regular season, Hamels said.
Bixler, a non-roster infielder who entered the game as a pinch-runner in the seventh inning, said it was the first walkoff homer he could remember hitting at any level.
Phillies infielder Michael Martinez left the game with a bruised right elbow in the fifth inning after being hit with a pickoff attempt. ... The Astros optioned pitchers Jorge DeLeon and Arcenio Leon to Double-A Corpus Christi and reassigned pitcher Henry Villar, infielders Delino DeShields and Jonathan Villar and outfielder George Springer to their minor league camp, leaving 57 players, including 25 pitchers. ... Phillies right fielder Scott Podsednik made a sliding catch right behind second base in the eighth inning.