Castillo, 27, will join the Astros on Friday in New York, where they will play a four-game set with the Mets.
A corresponding move will be made to make room for Castillo on the 25-man roster at that time.
"With Kaz [Matsui] out and [Ty Wigginton] playing a lot of left field, it made sense for us to add another experienced infielder," general manager Ed Wade said.
Matsui, who is sidelined with an irritated disk in his lower back, is eligible to come off the disabled list on Aug. 28, but even so, the absence of Wigginton as an everyday infielder indicates Castillo has a good chance to stick with the team beyond this month.
Castillo hit .244 (96-for-394) with six home runs, 35 RBIs and a career-high 28 doubles for the Giants this season. He has played in 577 career games in five big league seasons with Pittsburgh (2004-07) and San Francisco (2008).
He has a career average of .253 with 39 homers and 216 RBIs, along with 102 doubles.
Catcher/first baseman J.R. House was informed after Wednesday's game that he was removed from the 25-man roster to make room for Castillo. House was hitless in three at-bats, all as a pinch-hitter.
"I had a good time," House said. "I'll try to get back up here for September and see how it goes."
Alyson Footer is a reporter for MLB.com. This story was not subject to the approval of Major League Baseball or its clubs.