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

Hel*_*rld 1 git git-checkout

通常我git checkout -- foo.txt用来重置我的工作树中的特定文件。今天我不小心使用了git checkout foo.txt(没有--),它似乎也做了同样的事情。

$ git checkout -- foo.txt  # no output
$ git checkout foo.txt
Updated 1 path from the index
Run Code Online (Sandbox Code Playgroud)

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

Swi*_*Men 5

简单回答:

# 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
Run Code Online (Sandbox Code Playgroud)

因此,如果您需要--它用于files,没有这两个字符,则表示分支。如果 Git 找不到分支,那么它会寻找一个文件并检出这个文件。

查看官方 Git doku 关于git checkout