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:

  1. Run git stash save -k -u and make sure it saves something (e.g., compare the results from git rev-parse refs/stash before and after).
  2. Run your tests.
  3. git reset --hard && git clean -df (optionally, including -q for both). The git reset --hard is needed only if the tests modify committed files, and the git clean is needed only if the tests create untracked files.
  4. 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:

  1. Create an empty temporary directory.
  2. 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 variables GIT_WORK_TREE and GIT_DIR, or the --git-dir and --work-tree arguments to the front end git command.)
  3. Run the tests in the temporary directory.
  4. 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.

Tags:

Git

Git Stash