12

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
halfer
  • 19,824
  • 17
  • 99
  • 186
danday74
  • 52,471
  • 49
  • 232
  • 283

3 Answers3

16

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
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • how would i handle new line chars? I tried \n but no joy - thanks - needs to work for windows and linux – danday74 Aug 24 '17 at 09:51
  • 2
    @danday74 Plus `-e` after `echo`. – ElpieKay Aug 24 '17 at 11:12
  • 2
    does not seem to work anymore: git version 2.30.2 – Booker B May 02 '22 at 08:19
  • @BookerB Did you run it in a git repository? What was the error like? – ElpieKay May 02 '22 at 14:11
  • 2
    isnt the purpose of string compare to NOT run into git repo folder?error START: fatal: not a git repository (or any of the parent directories): .git fatal: not a git repository (or any of the parent directories): .git warning: Not a git repository. Use --no-index to compare two paths outside a working tree usage: git diff --no-index [] ;error END; i was using this command for quite some time, until git pushed an update, it worked very well. – Booker B May 03 '22 at 09:22
  • @BookerB This method is equal to writing the two strings into files first and then comparing the two files. `git hash-object -w` converts the string into a blob object, which needs a git repository database. Although `git diff --no-index` can compare two files outside a git repository, `git diff --no-index <(echo string1) <(echo string2)` raises an error (at least in Windows git bash). So in order to write and read the git database, this method needs a gitdir, run in a git repository or specifying `GITDIR=/path/to/repo/.git` or `--git-dir=/path/to/repo/.git`. – ElpieKay May 03 '22 at 16:53
  • Not wokring for me - git version 2.41.0 – Nam G VU Aug 21 '23 at 19:03
  • @NamGVU What does the error say? I tried with git version 2.41.0 on Windows and it worked. – ElpieKay Aug 22 '23 at 01:52
4

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

Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
3

In the implementation level, I guess, git diff use a certain diff utility (such as diff) to generate patch, and redirect to a pager(default it is less) for viewing. So you need not call git diff to compare two string.

If you want patch file format as that output by git diff, the flowing command will be a help.

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

-u is appended to tell diff to output patch with unified context.

By the way, git provide a configuration item diff.external to allow you generate diff not by the internal diff machinery, but using the given command. environment variable GIT_EXTERNAL_DIFF has the same effect.

gzh
  • 3,507
  • 2
  • 19
  • 23