git 如何使用git比较不在repo中的两个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11561498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:10:05  来源:igfitidea点击:

How to compare two files not in repo using git

gitcomparediff

提问by Cedric Reichenbach

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

我想比较两个不在任何 git 存储库中的 css 文件。git中有这样的功能吗?

回答by Kyle Burton

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.

git的 diff 比标准的 unix 更实用diff。我经常想这样做,因为这个问题在谷歌上排名很高,我希望这个答案出现。

This question: How to use git diff --color-wordsoutside a Git repository?

这个问题:如何git diff --color-words在 Git 存储库之外使用?

Shows how to use git to diff files where at least one of them is not in the repository by using --no-index:

展示了如何使用 git 来比较文件,其中至少有一个文件不在存储库中,方法是使用--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:

git 跟踪哪个和哪个没有跟踪无关紧要 - 一个或两个都可以不被跟踪,例如:

$ 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.

此处无法显示颜色,因此我在示例中用管道分隔了更改。

回答by james

diff -u

diff -u

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

将为 git 的 diff 提供类似的统一上下文输出。

回答by edwardmp

Just use the diff command:

只需使用 diff 命令:

diff file1.ext file2.ext