“git checkout <filename>”和“git checkout -?- <filename>”之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6561142/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:17:21  来源:igfitidea点击:

Difference between "git checkout <filename>" and "git checkout -?- <filename>"

git

提问by Kit Ho

http://norbauer.com/notebooks/code/notes/git-revert-reset-a-single-file

http://norbauer.com/notebooks/code/notes/git-revert-reset-a-single-file

I have found a post.

我找到了一个帖子。

But still don't know what is the difference between

但是还是不知道有什么区别

  1. git checkout <filename>

  2. git checkout -- <filename>

  1. git checkout <filename>

  2. git checkout -- <filename>

In what situation I should use first one and second one respectively?

在什么情况下我应该分别使用第一个和第二个?

回答by zwol

The special "option" --means "treat every argument after this point as a file name, no matter what it looks like." This is not Git-specific, it's a general Unix command line convention. Normally you use it to clarify that an argument is a file name rather than an option, e.g.

特殊的“选项”--意味着“将这一点之后的每个参数都视为文件名,无论它是什么样的。” 这不是 Git 特定的,它是一个通用的 Unix 命令行约定。通常你用它来阐明一个参数是一个文件名而不是一个选项,例如

rm -f      # does nothing
rm -- -f   # deletes a file named "-f"

git checkout1also takes --to mean that subsequent arguments are not its optional "treeish" parameter specifying which commit you want.

git checkout1--意味着后续参数不是其可选的“treeish”参数,用于指定您想要的提交。

So in this context it's safeto use --always, but you needit when the file you want to revert has a name that begins with -, or is the same as the name of a branch. Some examples for branch/file disambiguation:

因此,在这种情况下,始终使用是安全的--,但是当您要还原的文件的名称以 开头或与分支名称相同时,您需要使用-。分支/文件消歧的一些示例:

git checkout README     # would normally discard uncommitted changes
                        # to the _file_ "README"

git checkout master     # would normally switch the working copy to
                        # the _branch_ "master"

git checkout -- master  # discard uncommitted changes to the _file_ "master"

and option/file disambiguation:

和选项/文件消歧:

git checkout -p -- README  # interactively discard uncommitted changes
                           # to the file "README"

git checkout -- -p README  # unconditionally discard all uncommitted
                           # changes to the files "-p" and "README"

I'm not sure what you do if you have a branchwhose name begins with -. Perhaps don't do that in the first place.

我不知道,如果你有一个你做什么分支,其名称开头-。也许一开始就不要这样做。



1in this mode; "checkout" can do several other things as well. I have never understood why git chose to implement "discard uncommitted changes" as a mode of the "checkout" subcommand, rather than "revert" like most other VCSes, or "reset" which I think might make more sense in git's own terms.

1在此模式下;“结帐”还可以做其他几件事。我一直不明白为什么 git 选择实现“放弃未提交的更改”作为“checkout”子命令的一种模式,而不是像大多数其他 VCS 那样“恢复”或“重置”,我认为这在 git 自己的术语中可能更有意义。

回答by jtbandes

Anything following the --is treated as a filename (not as a program argument). This is important if, for example, you have filenames which start with dashes.

后面的任何内容--都被视为文件名(而不是程序参数)。例如,如果您的文件名以破折号开头,这一点很重要。