Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to 'git diff' 2 strings?

Tags:

git

git-diff

diff

I have 2 strings and I want the git diff between them. I could create file1 and add string1 as its contents.

Then I could create file2 and add string2 as its contents. Then I could git diff file1 and file2.

However, given that I have the strings as strings (and not as file contents) can I avoid these long-winded steps? Is there an easier way?

Something like:

git diff "my first string" "my second string" # obviously does not work
like image 830
danday74 Avatar asked Aug 24 '17 05:08

danday74


2 Answers

If you insist on the git way,

git diff $(echo "my first string" | git hash-object -w --stdin) $(echo "my second string" | git hash-object -w --stdin)  --word-diff
like image 74
ElpieKay Avatar answered Sep 20 '22 17:09

ElpieKay


You don't have to use git diff for that, Git is used to track the changes in your code base.

There is a good linux command for that

diff  <(echo "my first string" ) <(echo "my second string")

This is a good answer https://stackoverflow.com/a/454549/4620609

like image 37
Samuel Robert Avatar answered Sep 18 '22 17:09

Samuel Robert