git diff 不显示新文件

标签 git git-diff

我创建了一个新文件 file.txt在我的工作树中。 git status正确显示:

$ git status
On branch mybranch
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        file.txt

nothing added to commit but untracked files present (use "git add" to track)

然而,git diff什么都不显示。
$ git diff


我发现了一些相关的问题,表明寻呼机可能有问题,所以我试过了
git --no-pager diff file.txt


GITPAGER=cat git diff file.txt

具有相同的空结果。

如果我添加文件然后差异到 HEAD,我会得到一个正确的差异。
$ git add file.txt
$ git diff --cached
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..f2d7933
--- /dev/null
+++ b/file.txt
@@ -0,0 +1 @@
+asds
\ No newline at end of file

这一切都在 Windows 10 和最新的 git 版本上。

为什么差异是空的?
通常,我不会那么在意,但我使用的另一个工具依赖于 diff 输出。

最佳答案

由于新文件在创建时没有被 git 跟踪,因此 git 无法识别发生了什么变化。当您这样做时git add file ,文件移动到 git 的暂存区,现在,它正在被跟踪。因此 git diff 将像往常一样工作,因为 git 现在拥有有关您的文件的所需信息。

git docs提供有关未跟踪文件的更多信息,如下所示 -

Remember that each file in your working directory can be in one of two states: tracked or untracked. Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged. In short, tracked files are files that Git knows about.

Untracked files are everything else — any files in your working directory that were not in your last snapshot and are not in your staging area. When you first clone a repository, all of your files will be tracked and unmodified because Git just checked them out and you haven’t edited anything.

As you edit files, Git sees them as modified, because you’ve changed them since your last commit. As you work, you selectively stage these modified files and then commit all those staged changes, and the cycle repeats.

关于git diff 不显示新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56019119/

相关文章:

GIT:有什么方法可以提交多个 repo 协议(protocol)?

git - 非递归 git log 和 diff

git - 为什么 git pull 会导致我没有做的本地更改

生成代码差异的 PHP 库(github 样式)?

git - 如何仅将一个目录级别设置为 `git ls-files`。

javascript - 如何 merge github中的代码?

git - 一次为多个文件配置 vsdiffmerge

git - 如何区分提交?

Git merge 不会第二次 merge 文件

git递归添加所有.tex文件