I thought I would share a few of my favorite commands while using git. I highly recommend that you back up what you are doing before running these. It is also wise to have no outstanding work open in the repo you would like to demo this in. Without further ado, the first command is:
git read-tree $COMMIT_HASH
The read-tree command loads all files from a commit point into git’s index. This is not terribly useful on its own.
The second command is:
git checkout-index -a -f --prefix="$(dirname "$(pwd)")/temp/"
This checkout-index will checkout all the listed files in the index and forcibly write them to a new directory, in the code snippet above it happens to be a temp directory one level higher than the git repo we are executing the command from.
When we are done we are going to want to set our repo back to a clean start with this command:
git reset --hard HEAD
This command should set the repo back to its starting state.
So you might be wonder why in the world this is useful. Alone these commands are not terribly helpful but if you combine them together like so:
git read-tree $COMMIT_HASH && git checkout-index -a -f --prefix="$(dirname "$(pwd)")/temp/" && git reset --hard HEAD
You end up with a way to checkout anything from a repo at a given commit point … or tag, I did mention that tags right? Imagine you check code into a distribution repo where each build is tagged with a version number and build number. Now that repo just turned into a catalog of code that can be checked out from for deployments and/or rollbacks. But thats a discussion for another time.