Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git difftool report identical content?

I often use the following command to compare git commits:

git difftool -t meld --dir-diff 456e86fbe 040564acf

Today, for the first time, I experienced that it did not start meld. It did not say anything and just exited.

After a bit of surprise, I started guessing this means that the files in the two commits are identical - but I can't be sure.

Is there a way to persuade git difftool to verbosely inform you if the files in the two commits are the same? I know diff has --report-identical-files for this purpose, but I couldn't find anything similar in man git difftool.

like image 669
sdbbs Avatar asked May 25 '26 15:05

sdbbs


1 Answers

Unfortunately, git difftool doesn't provide any option to know or print whether the given commits have identical content. However, you could use git diff --quiet --exit-code to print a message if the commits are identical. If they're not, you can launch git difftool.

The command would look something like this:

git diff --quiet --exit-code <commit1> <commit2> \ 
&& echo "No differences between commits" \ 
|| git difftool -t meld --dir-diff -y <commit1> <commit2>

However, this is quite a hairy command to type each time and not well parameterized. What you could do instead is to define an alias for the previous command that accepts two commits:

git config --local alias.my-difftool '!sh -c '\''git diff --quiet --exit-code "$1" "$2" && echo "No differences between commits $1 and $2" || git difftool -t meld --dir-diff -y "$1" "$2"'\'' -'

An example of its usage would be

git my-difftool HEAD HEAD~2
like image 153
dani-vta Avatar answered May 27 '26 04:05

dani-vta