“git diff”什么都不做

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3580608/
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 08:57:53  来源:igfitidea点击:

"git diff" does nothing

gitgit-diff

提问by Tom Lianza

I presume this is a configuration error somewhere, but I can't figure out where. Regular git commands appear to work fine, but "git diff" does nothing. To be safe, I removed external diff tools from my .gitconfig file. This was installed via MacPorts and is the lates version (1.7.2.2).

我认为这是某个地方的配置错误,但我不知道在哪里。常规 git 命令似乎工作正常,但“git diff”什么也不做。为了安全起见,我从我的 .gitconfig 文件中删除了外部差异工具。这是通过 MacPorts 安装的,是最新版本 (1.7.2.2)。

What I see is that when I run "git diff" from my workspace, it simply exits, doing nothing.

我看到的是,当我从我的工作区运行“git diff”时,它只是退出,什么也不做。

$ git --version
git version 1.7.2.2
$ git diff
$ 

If I back up one directory, out of my root workspace, typing "git diff" gives me this:

如果我备份一个目录,从我的根工作区中,输入“git diff”给我这个:

$ git diff
usage: git diff [--no-index] <path> <path>

This may be expected behavior since I'm not under a git repository.

这可能是预期的行为,因为我不在 git 存储库下。

Any ideas on what I can do to troubleshoot this?

关于我可以做些什么来解决这个问题的任何想法?

回答by Douglas

The default output for git diffis the list of changes which have not been committed / added to the index. If there are no changes, then there is no output.

的默认输出git diff是尚未提交/添加到索引的更改列表。如果没有变化,则没有输出。

git diff [--options] [--] […]

This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you couldtell git to further add to the index but you still haven't.

git diff [--options] [--] […]

此表单用于查看您相对于索引所做的更改(下一次提交的暂存区)。换句话说,不同之处在于您可以告诉 git 进一步添加到索引中,但您仍然没有。

See the documentationfor more details. In particular, scroll down to the examples, and read this section:

有关更多详细信息,请参阅文档。特别是,向下滚动到示例,并阅读本节:

$ git diff            # (1)
$ git diff --cached   # (2)
$ git diff HEAD       # (3)
  1. Diff the working copy with the index
  2. Diff the index with HEAD
  3. Diff the working copy with HEAD
  1. 用索引区分工作副本
  2. 用 HEAD 区分索引
  3. 用 HEAD 区分工作副本

Outside your workspace, as you guessed, git won't know what to diff, so you have to explicitly specify two paths to compare, hence the usage message.

在您的工作区之外,正如您猜测的那样,git 不知道要区分什么,因此您必须明确指定要比较的两个路径,因此是使用消息。

回答by VonC

Note: starting git 1.8.5 or 1.9, Q4 2013:

注意:从 git 1.8.5 或 1.9,Q4 2013 开始

When the user types "git diff" outside a working tree, thinking he is inside one, the current error message that is a single-liner:

当用户在工作树外键入 " git diff" 时,认为他在工作树内,当前的错误消息是单行:

usage: git diff --no-index <path> <path>

may not be sufficient to make him realize the mistake.

Add "Not a git repository" to the error message when we fell into the "--no-index" mode without an explicit command line option to instruct us to do so.

可能不足以让他意识到错误。

Not a git repository当我们在--no-index没有明确命令行选项指示我们这样做的情况下进入“ ”模式时,将“ ”添加到错误消息中。



See:

看:

Clarify documentation for "diff --no-index".
State that when not inside a repository, --no-indexis implied and two arguments are mandatory.

Clarify error message from diff-no-indexto inform user that CWD is not inside a repository and thus two arguments are mandatory.

澄清“ diff --no-index”的文档。
声明当不在存储库内时,--no-index是隐含的,并且两个参数是强制性的。

澄清错误消息diff-no-index以通知用户 CWD 不在存储库内,因此两个参数是必需的。

To compare two paths outside a working tree:
usage: git diff --no-index <path> <path>

回答by Jaanus

It does nothing if your working directory is clean and there are no differences from the last update. Try editing a file and then run git diff again, and it should then show the diff.

如果您的工作目录是干净的并且与上次更新没有区别,则它不会执行任何操作。尝试编辑一个文件,然后再次运行 git diff,它应该会显示差异。

回答by jweyrich

If you are using it outside of a real repository or work-copy, its behaviour is identical to the GNU diff. So you need to inform the 2 directories or files to be compared. Example:

如果您在真实存储库或工作副本之外使用它,则其行为与 GNU 差异相同。所以需要告知要比较的2个目录或文件。例子:

git diff old_dir new_dir.

git diff old_dir new_dir.

If there is any difference between them, the output will show you, as expected.

如果它们之间有任何差异,输出将按预期显示。

回答by deFreitas

Not in your case, but maybe because the file that you pass not exists

不是你的情况,但可能是因为你传递的文件不存在

$ git difftool HEAD HEAD^ -- path/that-not-exists

nothing happen

什么都没发生