I need git diff functionality for 2 files that I have outside of any repository. Is there a way to do it? Something like git diff --file1 /path/file1.txt --file2 /path/file2.txt If not, what may be an alternative solution?
2 Answers
The answer is right in the git diff documentation (though I admit it's easy to miss):
git diff [<options>] --no-index [--] <path> <path>
This form is to compare the given two paths on the filesystem. You can omit the
--no-indexoption when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git.
Your case might fall into the "--no-index is optional" category, but even if it's optional you can still use it, so:
git diff --no-index /path/file1.txt /path/file2.txt will use Git's diff.
3 Comments
dtc
im using v 2.21.0 and i didn't need to specify --no-index to use
git diff. I don't use diff at all but I find this more useful because it gives you color output and you can also use something like this on windows as well.torek
@dtc: it's tough to define precisely when you do need
--no-index. The point I'm making above is that you can always use it for this sort of case. If you don't need it, fine, but if you do need it for some rare case, and you don't use it, Git will gripe at you. Hence you might want to make an alias or something that supplies the flag so that you don't have to worry about it.milahu
the file paths must be absolute, so to use relative paths:
git diff --no-index "$(readlink -f "file1.txt")" "$(readlink -f "file2.txt")"If the two files are really outside of any Git repository, then Git effectively doesn't "know" anything about these files, and so git diff won't work. But, the regular Linux diff command should work. So instead of doing:
git diff /path/to/file1.txt /path/to/file2.txt do this:
diff /path/to/file1.txt /path/to/file2.txt 3 Comments
acgtyrant
diff is not git diff, so this answer is not helpful.Tim Biegeleisen
2 files that I have outside of any repository ... if I read correctly, this question has nothing to even do with Git.milahu
git diff has more features than diff, for example git diff --word-diff=color or git diff --patience