I'd like to compare two css files which are not in any git repository. Is there such a functionality in git?
3 Answers
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.
-
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"
Jun 30, 2015 at 20:30 -
1FYI: On Git for Windows there was a bug (#2123) that "--no-index" was not respected. This was fixed in Git for Windows v2.21.0 (February 26th 2019). Jun 27, 2019 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... Sep 10, 2019 at 17:43
-
hi @LukasSalich it looks like that line format might be documented by git (eg: 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. Sep 22, 2019 at 17:46
-
3Note that you don't have to specify
--no-index
if you're doing this outside of any Git repository.– SnakENov 20, 2020 at 11:23
diff -u
will give similar unified context output to git's diff.
-
1
-
It does not allow a nice word diff, either (
git diff --word-diff
). Jul 25, 2019 at 18:20
Just use the diff command:
diff file1.ext file2.ext
-
2Hmkay, I tried this but in my opinion, the output was just nonsense... Maybe I was wrong... Jul 19, 2012 at 17:49
-
you can run some GUI tool that will provide better view, like gvimdiff, kompare or similar Jul 19, 2012 at 21:41
--patience
flag? Do you know a diff(1) that can do that?