Why does 'set -x' cause the terminal to dump garbage

What you're seeing here is some fancy behind-the-scenes scriptery that Apple added in OS X 10.11 to set the Terminal window title based on the current working directory. Since set -x shows what commands are being executed by bash, it also winds up showing all this normally-behind-the-scenes stuff (in painful detail). It's fairly easy to disable it, though:

unset PROMPT_COMMAND

... and then you can debug in peace, but your window title bar won't update any more. If you're done debugging and want to re-enable title bar updates, just reset the variable:

PROMPT_COMMAND=update_terminal_cwd

Add this line to your .bashrc or .bash_profile if you don't want to see the "garbage" anymore.

[ "${PROMPT_COMMAND}" ] && PROMPT_COMMAND="{ ${PROMPT_COMMAND}; } 2>/dev/null"

caveat: this means that all the stderr output from ${PROMPT_COMMAND} will be dumped to /dev/null.