I'd like to compare two css files which are not in any git repository. Is there such a functionality in git?
Asked
Active
Viewed 4.1k times
160
-
What if I wanted to use, say, the `--patience` flag? Do you know a **diff(1)** that can do that? – SamB Aug 05 '12 at 19:35
-
You should accept Kyle Burton's answer. – BrodieG Feb 05 '16 at 13:54
-
related: https://stackoverflow.com/questions/855767/can-i-use-git-diff-on-untracked-files?rq=1 – slm Jan 23 '18 at 03:50
3 Answers
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
-
17If 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
-
1FYI: 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
-
3Note 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
-
1
-
It does not allow a nice word diff, either (`git diff --word-diff`). – Stefan van den Akker Jul 25 '19 at 18:20
1
Just use the diff command:
diff file1.ext file2.ext

edwardmp
- 6,339
- 5
- 50
- 77
-
2Hmkay, I tried this but in my opinion, the output was just nonsense... Maybe I was wrong... – Cedric Reichenbach Jul 19 '12 at 17:49
-
you can run some GUI tool that will provide better view, like gvimdiff, kompare or similar – Eugene Sajine Jul 19 '12 at 21:41