Run tests only for staged files: `git stash -k -u` and `git stash pop` will raise conflicts on partial staged files
This is not a full answer—see How to recover from "git stash save --all"? for more—but while this is an appealing process, and it will work once the bug in git stash
gets fixed, it is a bit dangerous today.
If you have fixed the bug, or don't mind living slightly dangerously, :-) you can use this process:
- Run
git stash save -k -u
and make sure it saves something (e.g., compare the results fromgit rev-parse refs/stash
before and after). - Run your tests.
git reset --hard && git clean -df
(optionally, including-q
for both). Thegit reset --hard
is needed only if the tests modify committed files, and thegit clean
is needed only if the tests create untracked files.- Run
git stash pop --index
. Note that the--index
is critical here. You may wish to use-q
as well.
Instead of save
and pop --index
, you might want to use create
and apply --index
and store your stash-bag under a different reference (that you manipulate and/or delete when done, in whatever way you like). Of course, if you're going to go this far, you might want to write your own modified git stash
script that avoids the current one's bug in the first place.
There's a completely different, and to my mind simpler, approach to running tests:
- Create an empty temporary directory.
- Turn the current index into a tree, then read that tree into the temporary directory. (Or use
git checkout-index
to extract the index into the temporary directory. In either case, note the environment variablesGIT_WORK_TREE
andGIT_DIR
, or the--git-dir
and--work-tree
arguments to the front endgit
command.) - Run the tests in the temporary directory.
- Discard the temporary directory.
This avoids the git stash save
bug and, with a slight modification to step 2, lets you test any revision. There are two obvious disadvantages: you need a place to store a temporary tree, and the temporary tree is not where the work-tree is. How much of a problem those are depends on your repository and your tests.