Summary
I am working on a script. As a part of this script, I need to programmatically
- Checkout some git revision
- Do some read-only operations from this revision
- Restore the repo state to how it was before the program started (i.e., check out whatever revision I was on originally).
I'm imagine each of these steps would involve some set of git commands. I ideally want there to be no side affects from these three operations -- i.e., step 3 should restore all local changes and the repository is in the same state.
What I've Tried So Far
This almost works:
git stash --include-untracked
git name-rev --name-only HEAD # Save to <HEAD_REV>
git checkout <NEW_REV>
# Do stuff
git checkout <HEAD_REV>
git stash pop
This mostly works, but I'm not sure if there are some edge states where it will fail to restore the repository state. Can anyone more experienced in git tell me if there's a gold-standard way to do this?