`git checkout -- foo.txt` 和 `git checkout foo.txt` 之间的区别

Difference between `git checkout -- foo.txt` and `git checkout foo.txt`

通常我使用 git checkout -- foo.txt 来重置工作树中的特定文件。 今天我不小心用了git checkout foo.txt(没有--),好像也是一样。

$ git checkout -- foo.txt  # no output
$ git checkout foo.txt
Updated 1 path from the index

所以我想知道,这两者有什么区别?或者,有没有?

简单回答:

# checkout the branch foo.txt if this branch exist, otherwise the file
$ git checkout foo.txt

# checkout the file foo.txt
$ git checkout -- foo.txt

所以如果你需要--它是针对文件,没有这两个字符,它是分支的意思。如果Git找不到分支,那么它会寻找一个文件并检出这个文件。

看官方Git独库git checkout