Another option is to not solve this as a git problem at all, but as a Python problem. You can use the PYTHONDONTWRITEBYTECODE environment variable to prevent Python from writing .pyc files in the first place. Then you won't have anything to clean up when you switch branches.