Git - 为什么在删除的文件上运行命令时需要双破折号? [英] Git - why are double dashes needed when running a command on a deleted file?

查看:132
本文介绍了Git - 为什么在删除的文件上运行命令时需要双破折号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  git rm path / to / file 
git commit -a -mtesting

好的,现在我想看看 git log 作为文件,但我收到了经典的错误消息:

  git log path / to / file 
fatal:模糊参数'path / to / file':未知版本或路径不在工作树中。
使用' - '分隔路径与修订

解决方法很简单 - 添加 -

  git log  -  path / to / file 

但是...为什么?为什么这需要?这里的基本原理是什么?不可能做一个有教养的猜测,这可能是一个文件一次?我明白这个模棱两可的问题 - 但是从未有过这个名字的标签。如果文件一旦被删除,并且没有标签存在,那么选择文件解释永远是不错的选择。

另一方面,有可能拥有一个名为与文件相同的标签, git log 处理得很好:

  fatal:模糊参数'path / to / file':修订版本和文件名称
使用' - '将文件名与修订版分开

这种行为似乎不一致。任何人都可以解释一下git开发人员的想法吗?

假设你有一个名为 a / b / c的文件夹,可以在文件,分支,标签等等上使用它。 ,您将使用

  git获取此文件夹的提交日志a / b / c 

没关系。



您也可以有一个名为 d / e / f 的分支。您将使用

  git log d / e / f $ b获取此分支的提交$ b  

没关系。



事情开始得到如果其中 git log 应该工作的项目无法清晰确定,则会很复杂。如果你很愚蠢并且打电话给你的分支 a / b / c ,那么git不会打印其日志应该被打印的内容: em>分支 a / b / c 或您的目录的记录 a / b / c ?因此,您必须详细说明您希望收到的信息:


  • 显示分支的日志强> a / b / c

    git log a / b / c -

  • 显示当前分支中文件夹 a / b / c 的日志:

    git log - a / b / c

  • 显示文件夹的日志 a / b / c 分支中的 a / b / c

    git log a / b / c - a / b / c



文件,你有一个类似的问题:在工作副本中既没有一个名为 path / to / file 的文件,也没有名为路径的分支/到/文件。这就是为什么你必须指定你想要的东西。



当然,git可以知道一个名为<$ c的文件$ c> path / to / file > 20.000版本之前,但这需要(最糟糕的情况下)搜索项目的整个历史记录,无论这个文件是否存在。



通过在 - 之后显式指定文件路径,您可以告诉git:


对该文件进行搜索更加困难,即使需要花费数小时






<结论(回答你的问题):
在你的情况下,需要 - ,否则 git log 通常工作会比较慢。


Consider a git repository, where a file was once deleted.

git rm path/to/file
git commit -a -m"testing"

Ok, now I want to see the git log for the file, but I receive the classic error message:

git log path/to/file
fatal: ambiguous argument 'path/to/file': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

The solution is simple - add --:

git log -- path/to/file

But... why? Why is this needed? What is the rationale here? Can't git do an educated guess, that this might have been a file once? I understand the "ambiguity" problem - but there never was a tag by that name. If the file was once deleted, and no tag is present, then choosing "the file interpretation" is always the good choice.

On the other hand, it's possible to have a tag named the same as a file, which git log handles pretty well:

fatal: ambiguous argument 'path/to/file': both revision and filename
Use '--' to separate filenames from revisions

This behavior seems inconsistent. Could anyone explain what the developers of git had in mind?

解决方案

git log could be used on files as well as on branches, tags and so on.

Assume you have a folder called a/b/c, you'll get the commits for this folder using

git log a/b/c

That's fine.

You could also have a branch called d/e/f. You'll get the commits for this branch using

git log d/e/f

That's fine too.

Things start to get complicated if the item where git log should work on could not be clearly determined. If you're stupid and call your branch a/b/c too, git has no clue whose log shall be printed: that of the branch a/b/c or the log of your directory a/b/c? Therefore, you have to tell a bit more about the information you want to receive:

  • show the log of the branch a/b/c:
    git log a/b/c --
  • show the log of the folder a/b/c in the current branch:
    git log -- a/b/c
  • show the log of the folder a/b/c in the a/b/c branch:
    git log a/b/c -- a/b/c

With the deleted file, you have a similar problem: there's neither a file called path/to/file present in the working copy, nor is there a branch called path/to/file. This is the reason why you have to specify what you want.

Of course, git could know that there was a file called path/to/file 20.000 revisions ago but this would require (worst case) to search the entire history of your project whether such a file existed or not.

By explicitly specifying the file path after the --, you tell git:

search harder for that file, even if it takes hours


Conclusion (answering your question):
in your case, the -- is needed because otherwise git log would work slower in general.

这篇关于Git - 为什么在删除的文件上运行命令时需要双破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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