"git checkout --" 。vs git 结帐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41101998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"git checkout --" . vs git checkout
提问by Laoujin
I always used git checkout -- .
to clear my working directory. I thought I read somewhere that the --
was required to avoid git thinking you are passing parameters (or something)
我总是用来git checkout -- .
清除我的工作目录。我以为我在某处读到--
需要避免 git 认为您正在传递参数(或其他东西)
Now a colleague told me I could drop the --
, and indeed, a quick test did exactly the same.
现在一位同事告诉我,我可以放弃--
,而且确实,快速测试完全相同。
Is there are any difference between those two commands?
这两个命令有什么区别吗?
PS: Asking here because git checkout -- .
vs git checkout .
is kind of hard to google...
PS:在这里问是因为git checkout -- .
vsgit checkout .
很难用谷歌搜索......
回答by Tim Biegeleisen
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
. 那么下面的命令似乎不明确:
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 按该名称/路径检出文件。因此,在这种情况下,以下命令允许检出一个分支和一个名为 的文件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.
请注意,这git checkout <name>
实际上是针对分支的,但是 Git 语法很宽松,如果 Git 找不到分支,那么它会查找文件。
Closely related: Git change branch when file of same name is present
密切相关:当存在同名文件时,Git 更改分支
回答by Powerlord
--
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.
--
作为一个独立的参数(即不是另一个参数的一部分),许多 UNIX 命令行程序使用它来表示它后面的任何内容都不是参数。
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.
即git checkout -- --mydirectory
,没有--
将抛出错误。