← til

Using git stash to test the bug fix

July 19, 2019
git

Git stash has been a handy command in my workflow for checking whether a test covers the bug.

My usual workflow is:

  • fix the bug by writing some code
  • write a test that ensures a bug has been fixed
  • stash the code that fixes the bug with git stash push /path/to/files
  • run the tests and ensure that they are failing now
  • pop the stash with git stash pop and ensure that they are passing now

This way I avoid writing tests before writing the code that fixes a bug, and I've still ensured the test covering the bug is not a false positive - it was failing when I have stashed my code changes.

Stashing only the code changes that fix the bug is critical for this workflow. Git has added support for stashing individual files since version 2.13, so make sure to update it if you're using an older version.