gpt4 book ai didi

git - 如果我重置它,为什么我在 git diff 中看不到文件?

转载 作者:太空狗 更新时间:2023-10-29 14:39:19 27 4
gpt4 key购买 nike

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

如果我在 git 存储库中创建一个文件 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的帮助命令行选项:

This form is to view the changes you staged for the next commit relative to the named <commit>. Typically you would want comparison with the latest commit, so if you do not give <commit>, it defaults to HEAD. If HEAD does not exist (e.g. unborned branches) and <commit> is not given, it shows all staged changes. --staged is a synonym of --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 重置 hello.txt 重置后未暂存的更改: M你好.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'sGit Cheatsheet它向您展示了命令如何在 git 的各个阶段之间移动内容。

关于git - 如果我重置它,为什么我在 git diff 中看不到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17254852/

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