Use git archive to generate a fresh checkout of a specific commit into a temp dir, then do all work in that temp dir.
A sample implementation: https://github.com/moredip/blog.thepete.net/blob/fea96051bf4dbaaf46b1d5ef203353edbf7ef36b/scripts/deployer.rb