161

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?
    – SamB
    Aug 5, 2012 at 19:35
  • You should accept Kyle Burton's answer.
    – BrodieG
    Feb 5, 2016 at 13:54
  • related: stackoverflow.com/questions/855767/…
    – slm
    Jan 23, 2018 at 3:50

3 Answers 3

285

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.

6
  • 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" Jun 30, 2015 at 20:30
  • 1
    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). 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
  • 3
    Note that you don't have to specify --no-index if you're doing this outside of any Git repository.
    – SnakE
    Nov 20, 2020 at 11:23
6

diff -u

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

2
  • 1
    It does not do nice coloring or use a pager, however. Aug 14, 2018 at 18:47
  • It does not allow a nice word diff, either (git diff --word-diff). Jul 25, 2019 at 18:20
1

Just use the diff command:

diff file1.ext file2.ext
2
  • 2
    Hmkay, 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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.