Search code examples
gitgit-diff

Make "git diff" output a remark when there is no difference


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.


Solution

  • You could use the --name-only option to list the files that have changed between the two compared objects, and then pipe the result to wc --lines to count the number of lines (files) that are different.

    If there is no difference, it won't print a specific message like you wanted, but it will at least print a 0, showing some type of output.

    git diff --name-only HEAD~ HEAD | wc --lines
    

    Also, if you wish not to type this lengthy command at each use, you could define an alias:

    # define the alias locally in your repo
    git config --local alias.my-diff '!sh -c "git diff --name-only $1 $2 | wc --lines"'
    
    # invoking the alias
    git my-diff HEAD~ HEAD