“git diff”什么都不做

“git diff” does nothing

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

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

1
2
3
4
$ git --version
git version 1.7.2.2
$ git diff
$

如果我从根工作区备份一个目录,键入"git diff"会得到:

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

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

我能做些什么来解决这个问题吗?


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 could tell git to further add to the index but you still haven't.

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

1
2
3
$ git diff            # (1)
$ git diff --cached   # (2)
$ git diff HEAD       # (3)
  • 将工作副本与索引进行比较
  • 用头区分索引
  • 用头区分工作副本
  • 正如您所猜测的,在您的工作区之外,Git将不知道要区分什么,因此您必须显式地指定两个要比较的路径,从而得到用法消息。


    注:从2013年第4季度1.8.5或1.9开始:

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

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

    见:

    • 提交286BC123CD(Gitster,Junio C Hamano),这解释了git diff --no-index可以像普通(非Git)diff那样工作。
    • 提交b214eddfb2(dale r.worley),其中澄清了错误消息:

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

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

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


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


    如果您在实际存储库或工作副本之外使用它,那么它的行为与GNU差异相同。因此您需要通知要比较的两个目录或文件。例子:

    git diff old_dir new_dir

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


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

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

    什么都没发生