When the two commits are the same, git diff
generates no result.
However, is there a way to make it output some text, such as "no difference in commit1 and commit2"? this is to help identify the result in script log.
In Unix/Linux shells you can use exit code to decide whether to print an additional message, but you need to ask git diff to generate it:
--exit-code
Make the program exit with codes similar to diff(1). That is, it exits with 1 if there were differences and 0 means no differences.
git diff --exit-code && echo no difference in commit1 and commit2
If you want to type it often, you'll probably want to create an alias or function.