我在Git文档中看到了这个语句:
Checking out a file is similar to using git reset with a file path, except it updates the working directory instead of the stage
链接:https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting
部分内容:“Git Checkout文件”
现在假设我有一个存储库和其中一个文件test.txt
。
首先,工作目录是干净的:
On branch master
nothing to commit, working tree clean
现在我修改了test.txt
,运行git add
之后,git status
显示如下:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
现在我运行git checkout HEAD test.txt
,并得到以下结果:
Updated 1 path from 58e7043
< p >< code > git status 的输出:
On branch master
nothing to commit, working tree clean
根据文档,
test.txt
在索引中的版本应该保持不变,在工作目录中的版本应该改回到指向HEAD的提交的版本,导致文件在工作目录和索引之间存在不同版本,那么此时不应该输出git status
吗?但是git status
没有显示这个 - 为什么呢?通常情况下,要从暂存的文件转到干净的工作树,我必须使用
git reset HEAD <filename>
,然后使用git checkout HEAD <filename>
来执行该文件,但是在这里看起来都执行了??我感到困惑。
编辑 - 还有有趣的是,如果我在暂存文件
test.txt
之后运行git checkout test.txt
而不是git checkout HEAD test.txt
,我会得到:Updated 0 paths from the index
尽管这两个表单应该是等价的,其中前者默认也是使用HEAD
方法提交的(?)
我又有些困惑了。