186

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

3
  • What if I wanted to use, say, the --patience flag? Do you know a diff(1) that can do that? Commented Aug 5, 2012 at 19:35
  • 1
    You should accept Kyle Burton's answer. Commented Feb 5, 2016 at 13:54
  • related: stackoverflow.com/questions/855767/… Commented Jan 23, 2018 at 3:50

3 Answers 3

325

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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"
FYI: 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).
What does row "index 6b10c7c..70f036c 100644" mean? I am comparing 2 binaries, they differ, but that is the only row with difference...
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.
Note that you don't have to specify --no-index if you're doing this outside of any Git repository.
|
4

diff -u

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

2 Comments

It does not do nice coloring or use a pager, however.
It does not allow a nice word diff, either (git diff --word-diff).
0

Just use the diff command:

diff file1.ext file2.ext

2 Comments

Hmkay, I tried this but in my opinion, the output was just nonsense... Maybe I was wrong...
you can run some GUI tool that will provide better view, like gvimdiff, kompare or similar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.