Fix git pre-commit hook

1. Only run the hook if necessary
2. Add the changed images to the commit instead of commiting the unchanged files and leaving the optimized files unstaged.