I just wroteIf you are using ZSH, take a look at this small plugin I recently wrote to handle this. Take a look:
https://github.com/ericfreese/zsh-cwd-history
It stores a separate HISTFILE for every directory you've executed commands in, and adds a chpwd hook to switch between HISTFILEs when you change directories.
It also provides a zle widget you can bind to a keypress that will toggle the HISTFILE between your original "global" HISTFILE and the HISTFILE for your current working directory.
It was originally based on Jim Hester's per-directory-history plugin, but has since been rewritten from the ground up to fix many of the issues with that plugin.