160

I'd like to compare two css files which are not in any git repository. Is there such a functionality in git?

Cedric Reichenbach
  • 8,970
  • 6
  • 54
  • 89

3 Answers3

284

git's diff is more functional than the standard unix diff. I often want to do this and since this question ranks highly on google, I want this answer to show up.

This question: How to use git diff --color-words outside a Git repository?

Shows how to use git to diff files where at least one of them is not in the repository by using --no-index:

git diff --no-index file1.txt file2.txt

It doesn't matter which one is tracked by git and which is not - one or both can be untracked, eg:

$ date > x
$ sleep 2
$ date > y
$ git diff --color-words --no-index x y
diff --git a/x b/y
index 6b10c7c..70f036c 100644
--- a/x
+++ a/y
@@ -1 + 1 @@
Wed Jun 10 10:57:45|10:57:47 EDT 2013

The color can't be shown here so I separated the changes with a pipe in the example.

Community
  • 1
  • 1
Kyle Burton
  • 26,788
  • 9
  • 50
  • 60
  • 17
    If you wanted this behavior default, you could add an alias to your `.bashrc` file (or whatever you use): `alias diff="git diff --no-index"` – counterbeing Jun 30 '15 at 20:30
  • 1
    FYI: On Git for Windows there was a [bug (#2123)](https://github.com/git-for-windows/git/issues/2123) that "--no-index" was not respected. This was fixed in Git for Windows v2.21.0 (February 26th 2019). – StackzOfZtuff Jun 27 '19 at 11:18
  • What does row "index 6b10c7c..70f036c 100644" mean? I am comparing 2 binaries, they differ, but that is the only row with difference... – Lukas Salich Sep 10 '19 at 17:43
  • hi @LukasSalich it looks like that line format might be documented by git (eg: https://git-scm.com/docs/diff-format) git's diff doesn't attempt to diff binaries afaik, so I'm guessing that line means they differ, the two SHAs where a change was identified and then the permissions of the file. – Kyle Burton Sep 22 '19 at 17:46
  • 3
    Note that you don't have to specify `--no-index` if you're doing this outside of any Git repository. – SnakE Nov 20 '20 at 11:23
  • I use git diff on 2 files (in my working directory) where they are both version-controlled. So theoretically, i can exclude --no-index. But strange, output is empty. Once i put --no-index, it shows diff output. – Nick Wills Dec 19 '22 at 11:16
6

diff -u

will give similar unified context output to git's diff.

4b0
  • 21,981
  • 30
  • 95
  • 142
james
  • 61
  • 1
  • 1
1

Just use the diff command:

diff file1.ext file2.ext
edwardmp
  • 6,339
  • 5
  • 50
  • 77