有时恢复,有时结帐

Sometimes restore, sometimes checkout

我开始注意到,有时当我键入 git status 来检查分支时,我会看到文本:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   some_code_or_file.py

有时我会看到:

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:   some_code_or_file.py

它开始吓坏我了。这是怎么回事?

您几乎肯定会在两台不同的计算机上看到这些消息,或者当 运行 两个不同版本的不同二进制文件时。 git restore was introduced in 2.23, and the help message changed shortly after that (I can't remember exactly which version, and either it isn't mentioned in the changelogs 还是我太笨找不到了)。

git restore <file>git checkout -- <file>做同样的事情,所以除了好奇之外不需要担心差异。 git checkout 是一个混乱的命令,它执行大量不同的事情——git switchgit restore 的引入是为了在不改变功能的情况下使其对初学者更直观。