I am trying to get the git diff
between 2 strings. The following command works:
git diff $(echo "my first string" | git hash-object -w --stdin) $(echo "my second string" | git hash-object -w --stdin) --word-diff
However, it fails if not executed inside a Git directory.
I believe this portion of the command is failing:
echo "my first string" | git hash-object -w --stdin
Is there any way around this so that it can be executed outside a Git directory?
The problem you are having is because of the
-w
option that you pass to thegit hash-object
command. That option requires an existing repository as it has a side effect of writing the object into the git database.Proof:
However, since your final goal is to obtain the
git diff
between two given strings you have to have a git repository if you want to do it with the help ofgit hash-object
1. To this end you can generate a temporary empty repository:This approach can be packaged into a bash function:
Usage:
Example:
1 Note that you can
git diff
two strings by creating 2 temporary files containing those strings, in which case you don't need a git repository.