繁体   English   中英

重置后,为什么看不到git diff中的文件?

[英]Why can't I see a file in git diff if I reset it?

我知道git reset myFile.txt取消跟踪myFile.txt。

如果我在git repo中制作了一个文件myFile.txt,然后调用git diff ,我会看到myFile.txt被列为区别。 添加myFile.txt后,看不到差异。 这是预期的,因为我现在正在跟踪文件。

但是,如果我使用git reset myFile.txt跟踪myFile.txt,则看不到git diff列出的myFile.txt。 这是为什么? git diff是否不应该向我显示我的未跟踪更改?

git reset并不总是取消跟踪文件。 如果以前没有对其进行跟踪,则只会这样做。 git diff仅显示您未进行的更改。 如果该文件未跟踪,则git会忽略它,但状态输出中除外,它会告知您可能要跟踪的文件。 让我们从一个干净的仓库开始:

$ git init practice
$ cd practice

现在,让我们添加一个文件:

$ echo "Hello World" > hello.txt

在这一点上, git status表示文件未跟踪:

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   hello.txt
nothing added to commit but untracked files present (use "git add" to track)

因此,在这里执行git diff不会显示任何内容,因为git现在实际上忽略了该文件:

$ git diff

现在,让我们将hello.txt添加到索引中:

$ git add hello.txt
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   hello.txt
#

git diff仍然没有显示任何内容:

$ git diff

这样做的原因是因为git diff会告诉您索引和工作树之间的区别。 既然我们已经将hello.txt添加到索引中,则工作树将匹配,因此git diff不再显示任何内容。

如果您想查看准备提交的内容,请尝试git diff --cached

$ git diff --cached
diff --git a/hello.txt b/hello.txt
new file mode 100644
index 0000000..557db03
--- /dev/null
+++ b/hello.txt
@@ -0,0 +1 @@
+Hello World

仅供参考,以下是--cached命令行选项的帮助:

此表单用于查看相对于命名的<commit>为下一次提交所做的更改。 通常,您需要与最新提交进行比较,因此,如果您不提供<commit> ,则默认为HEAD 如果HEAD不存在(例如,未出生的分支)并且未给出<commit> ,则它将显示所有已分阶段的更改。 --staged--cached的同义词。

当您在此时进行重置时,您并不仅仅是在取消暂存文件的内容,git会回到将文件视为未跟踪状态:

$ git reset hello.txt
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   hello.txt
nothing added to commit but untracked files present (use "git add" to track)

结果, git diff不会显示任何内容。 即使您执行了git commit -a ,该文件也不会被提交,因为它是未跟踪的。

跟踪文件后,故事就会改变。 因此,我们将其添加并提交:

$ git add hello.txt
$ git commit -m "Initial commit."
[master (root-commit) 67cb14e] Initial commit.
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt
$ git status
# On branch master
nothing to commit, working directory clean

现在,让我们添加更多文本:

$ echo "Hello again" >> hello.txt
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   hello.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

git status的输出已更改。 现在,我们将hello.txt视为已修改,而之前未对其进行跟踪。 现在git diff起作用了,因为该文件存在于索引中并且正在被跟踪。 索引版本是您已暂存的版本,或者与上次提交( HEAD )相匹配(如果尚未暂存):

$ git diff
diff --git a/hello.txt b/hello.txt
index 557db03..49e9db5 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
 Hello World
+Hello again

让我们上演它,看看差异:

$ git add hello.txt
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   hello.txt
#
$ git diff

现在,我们再也看不到任何区别,因为索引中的内容与工作树匹配。 让我们重置:$ git reset hello.txt重置后的暂存更改:M hello.txt

现在我们可以再次看到更改:

$ git diff
diff --git a/hello.txt b/hello.txt
index 557db03..49e9db5 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
 Hello World
+Hello again

Git并没有在这里取消跟踪文件,它只是重置了索引的内容。 该文件仍在跟踪中。 让我们将hello.txt添加到索引并进行更改,以演示与索引比较的最后一部分:

$ git add hello.txt
$ echo "One final change" >> hello.txt
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   hello.txt
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   hello.txt
#

因此,现在我们看到hello.txt进行了一些提交阶段的更改,而有些则没有:

$ git diff
diff --git a/hello.txt b/hello.txt
index 49e9db5..97849b8 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,2 +1,3 @@
 Hello World
 Hello again
+One final change

请注意,它仅显示正在添加的“一个最终更改”行。 这是因为git diff正在将工作树与索引进行比较,并且当我们执行最后一个git add时,我们在索引中添加了“ Hello again”行。 但是,这没有显示要进行提交的更改。 为此,我们回到git diff --cached

$ git diff --cached
diff --git a/hello.txt b/hello.txt
index 557db03..49e9db5 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
 Hello World
+Hello again

此时,让我们重置索引并将其恢复为HEAD的版本:

$ git reset hello.txt

由于我们不再上演任何更改,因此git diff --cached会恢复干净:

$ git diff --cached

git diff将显示两个更改:

$ git diff
diff --git a/hello.txt b/hello.txt
index 557db03..97849b8 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,3 @@
 Hello World
+Hello again
+One final change

希望这可以帮助您了解如何在工作树和索引之间来回传输更改,基本上忽略未跟踪的文件,以及如何查看已暂存的更改。 我所见过的最好的可视化内容是NDP Software的Git备忘单 ,它向您展示了命令如何在git的各个阶段之间移动内容。

git diff对存储库索引中的文件运行diff。 如果尚未跟踪文件,则该文件不在索引中,因此您不会在git diff的输出中看到该文件。

当您运行git add ,git将文件添加到索引中。 这意味着随后的git diff将在输出中显示文件。

要查看未跟踪的文件,请改为运行git status

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM