gpt4 book ai didi

git diff 不显示新文件

转载 作者:行者123 更新时间:2023-12-04 13:01:27 25 4
gpt4 key购买 nike

我创建了一个新文件 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com