首页 > 解决方案 > `git checkout -- foo.txt` 和 `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

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

标签: gitgit-checkout

解决方案


简单的回答:

# 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 doku 关于git checkout


推荐阅读