“git checkout< filename>”之间的区别和“git checkout - - < filename>” [英] Difference between "git checkout <filename>" and "git checkout -​- <filename>"

查看:235
本文介绍了“git checkout< filename>”之间的区别和“git checkout - - < filename>”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我找到了一篇文章。

但是仍然不知道有什么区别


  1. git checkout< filename>


  2. git checkout - <文件名>


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

解决方案

特殊的选项 - 这一点作为文件名,不管它看起来如何。这不是Git特有的,它是一个通用的Unix命令行约定。通常你用它来阐明参数是文件名而不是选项,例如

  rm -f#什么也不做
rm - -f#删除一个名为-f的文件



同样需要 - 来表示后面的参数不是它的可选treeish参数指定了你想要的提交。

所以在这种情况下,它是安全的使用 - always,但是当你想要恢复的文件的名字以 - 开头时,你需要或者与分支的名称相同。一些分支/文件消除歧义的例子:

  git checkout README#通常会将未提交的更改
#丢弃到_file_ README

git checkout master#通常会将工作副本切换到
#_branch_master

git checkout - master#丢弃未提交的更改_file_master

和选项/文件消歧:

  git checkout -p  - 自述文件#交互式丢弃未提交的更改
#文件自述文件

git checkout - -p README#无条件抛弃所有未提交的
#对文件-p和README的更改

我不知道如果您有名称以 - 开头的分支,您会怎么做。也许不要这样做。






1 结帐也可以做其他几件事。我从来没有理解过为什么git选择实施放弃未提交的更改作为checkout子命令的一种模式,而不是像大多数其他VCS那样恢复,或者我认为git自己可能更有意义的重置。

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>

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

解决方案

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.

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

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

So in this context it's safe to use -- always, but you need it 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 branch whose name begins with -. Perhaps don't do that in the first place.


1 in 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.

这篇关于“git checkout&lt; filename&gt;”之间的区别和“git checkout - - &lt; filename&gt;”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆