“ git checkout ——”. vs git checkout

我总是用 abc0来清理我的工作目录。我想我在哪里读到过,--是为了避免 git 认为你在传递参数(或其他东西)

现在一个同事告诉我,我可以放弃 --,事实上,一个快速测试做了完全相同的。

这两个命令之间有什么区别吗?

PS: 在这里问是因为 git checkout -- .git checkout .是一种很难谷歌..。

78447 次浏览

I seem to recall that the -- is a way to tell Git to treat what follows checkout as a file and not as a branch. Suppose that you had both a file and a branch called stuff. Then the following command would seem ambiguous:

git checkout stuff

because it is not clear whether you are asking to checkout a file or a branch. By using -- you explicitly tell Git to checkout a file by that name/path. So in this case the following commands allow checking out a branch and a file called stuff:

git checkout stuff       # checkout the branch stuff
git checkout -- stuff    # checkout the file stuff

Note that git checkout <name> is really meant for branches, but Git syntax is relaxed, and if Git can't find a branch, then it will look for a file.

Closely related: Git change branch when file of same name is present

-- as a standalone argument (i.e. not part of another argument) is used by many UNIX command line programs to indicate that anything that follows it is not an argument.

Why? Well, in this case, it's being used in case you have a path whose name starts with --, which shouldn't be interpreted as its own argument.

i.e. git checkout -- --mydirectory which, without the -- would throw an error.