git diff for custom 2 files outside of any repository?

一世执手 提交于 2019-12-20 04:58:12

问题


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?


回答1:


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-index option 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.




回答2:


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


来源:https://stackoverflow.com/questions/50889139/git-diff-for-custom-2-files-outside-of-any-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!