git 致命:模棱两可的参数“来源”:未知的修订版或路径不在工作树中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45096755/
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
fatal: ambiguous argument 'origin': unknown revision or path not in the working tree
提问by guettli
I used git diff origin
often in the past.
我git diff origin
以前经常用。
In a different environment it does not work. I have no clue why.
在不同的环境中它不起作用。我不知道为什么。
user@host> git diff origin
fatal: ambiguous argument 'origin': unknown revision or path
not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Status:
地位:
user@host> git status
On branch master
nothing to commit, working directory clean
Remotes:
遥控器:
user@host> git remote -v
origin https://example.com/repos/djangotools (fetch)
origin https://example.com/repos/djangotools (push)
Version:
版本:
user@host> git --version
git version 2.7.4
With "git version 1.8.1.4" git diff origin
works.
使用“git version 1.8.1.4”git diff origin
工作。
BTW I see the same err msg if I use "git diff origin/master"
顺便说一句,如果我使用“git diff origin/master”,我会看到相同的错误消息
BTW2, I think the "/master" is redundant. The sane default is to compare the local branch with the same branch on the remote site.
顺便说一句,我认为“/master”是多余的。理智的默认是将本地分支与远程站点上的相同分支进行比较。
采纳答案by Chris
The git diff
command typically expects one or more commit hashes to generate your diff. You seem to be supplying the name of a remote.
该git diff
命令通常需要一个或多个提交哈希来生成您的差异。您似乎正在提供遥控器的名称。
If you had a branch named origin
, the commit hash at tip of the branch would have been used if you supplied origin
to the diff command, but currently (with no corresponding branch) the command will produce the error you're seeing. It may be the case that you were previously working with a branch named origin
.
如果您有一个名为 的分支origin
,如果您提供origin
给 diff 命令,则分支尖端的提交哈希将被使用,但目前(没有相应的分支)该命令将产生您看到的错误。可能是您以前使用名为origin
.
An alternative, if you're trying to view the difference between your local branch, and a branch on a remote would be something along the lines of:
另一种选择,如果您尝试查看本地分支与远程分支之间的差异,则可能是:
git diff origin/<branchname>
git diff origin/<branchname>
git diff <branchname> origin/<branchname>
git diff <branchname> origin/<branchname>
Edit:Having read further, I realise I'm slightly wrong, git diff origin
is shorthand for diffing against the head of the specified remote, so git diff origin
= git diff origin/HEAD
(compare local git branch with remote branch?, Why is "origin/HEAD" shown when running "git branch -r"?)
编辑:进一步阅读后,我意识到我有点错误,git diff origin
是针对指定远程的头部进行差异的速记,所以git diff origin
= git diff origin/HEAD
(将本地 git 分支与远程分支进行比较?,为什么在运行“git”时显示“origin/HEAD”分支 -r"?)
It sounds like your origin does not have a HEAD, in my case this is because my remote is a bare repository that has never had a HEAD set.
听起来您的来源没有 HEAD,在我的情况下,这是因为我的遥控器是一个从未设置过 HEAD 的裸存储库。
Running git branch -r
will show you if origin/HEAD
is set, and if so, which branch it points at (e.g. origin/HEAD -> origin/<branchname>
).
运行git branch -r
将显示是否origin/HEAD
已设置,如果已设置,则它指向哪个分支(例如origin/HEAD -> origin/<branchname>
)。
回答by cosmicdust
I ran into the same situation where commands such as git diff origin
or git diff origin master
produced the error reported in the question, namely Fatal: ambiguous argument...
我遇到了同样的情况,例如git diff origin
或git diff origin master
产生问题中报告的错误的命令,即Fatal: ambiguous argument...
To resolve the situation, I ran the command
为了解决这种情况,我运行了命令
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master
to set refs/remotes/origin/HEAD to point to the origin/master branch.
将 refs/remotes/origin/HEAD 设置为指向 origin/master 分支。
Before running this command, the output of git branch -a
was:
在运行此命令之前,输出git branch -a
为:
* master
remotes/origin/master
After running the command, the error no longer happened and the output of git branch -a
was:
运行命令后,错误不再发生,输出git branch -a
为:
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
(Other answers have already identified that the source of the error is HEAD not being set for origin. But I thought it helpful to provide a command which may be used to fix the error in question, although it may be obvious to some users.)
(其他答案已经确定错误的来源是未将 HEAD 设置为原点。但我认为提供一个可用于修复相关错误的命令很有帮助,尽管这对某些用户来说可能很明显。)
Additional information:
附加信息:
For anybody inclined to experiment and go back and forth between setting and unsetting refs/remotes/origin/HEAD, here are some examples.
对于任何倾向于尝试并在设置和取消设置 refs/remotes/origin/HEAD 之间来回切换的人,这里有一些例子。
To unset:git remote set-head origin --delete
取消设置:git remote set-head origin --delete
To set:
(additional ways, besides the way shown at the start of this answer)git remote set-head origin master
to set origin/head explicitly
ORgit remote set-head origin --auto
to query the remote and automatically set origin/HEAD to the remote's current branch.
要设置:(
除了本答案开头显示的方法之外的其他方法)git remote set-head origin master
显式设置 origin/head
或git remote set-head origin --auto
查询远程并自动将 origin/HEAD 设置为远程的当前分支。
References:
参考:
- This SO Answer
- This SO Commentand its associated answer
git remote --help
see set-head descriptiongit symbolic-ref --help
回答by Roma
Sometimes things might be simpler. I came here with the exact issue and tried all the suggestions. But later found that the problem was just the local file path was different and I was on a different folder. :-)
有时事情可能会更简单。我带着确切的问题来到这里并尝试了所有建议。但后来发现问题只是本地文件路径不同,我在不同的文件夹中。:-)
eg -
例如——
~/myproject/mygitrepo/app/$ git diff app/TestFile.txt
~/myproject/mygitrepo/app/$ git diff app/TestFile.txt
should have been
本来应该
~/myproject/mygitrepo/app/$ git diff TestFile.txt
~/myproject/mygitrepo/app/$ git diff TestFile.txt
回答by Thuo Ng'ang'a
For those experiencing this error on CI/CD, adding the line below worked for me on my GitHub Actions CI/CD workflow right after running pip install pyflakes diff-cover
:
对于那些在 CI/CD 上遇到此错误的人,在运行后立即在我的 GitHub Actions CI/CD 工作流程中添加以下行对我有用pip install pyflakes diff-cover
:
git fetch origin master:refs/remotes/origin/master
git fetch origin master:refs/remotes/origin/master
This is a snippet of the solution from the diff-cover github repo:
这是来自 diff-cover github repo 的解决方案的片段:
Solution: diff-cover matches source files in the coverage XML report with source files in the git diff. For this reason, it's important that the relative paths to the files match. If you are using coverage.py to generate the coverage XML report, then make sure you run diff-cover from the same working directory.
解决方案:diff-cover 将覆盖率 XML 报告中的源文件与 git diff 中的源文件进行匹配。出于这个原因,文件的相对路径匹配很重要。如果您使用coverage.py 生成覆盖XML 报告,请确保从同一工作目录运行diff-cover。
I got the solution on the links below. It is a documented diff-cover
error.
我在下面的链接上得到了解决方案。这是一个记录在案的diff-cover
错误。
https://diff-cover.readthedocs.io/en/latest//README.htmlhttps://github.com/Bachmann1234/diff_cover/blob/master/README.rst
https://diff-cover.readthedocs.io/en/latest//README.html https://github.com/Bachmann1234/diff_cover/blob/master/README.rst
Hope this helps :-).
希望这可以帮助 :-)。
回答by sfinkens
If origin
points to a bare repository on disk, this error can happen if that directory has been moved (even if you update the working copy's remotes). For example
如果origin
指向磁盘上的裸存储库,则如果该目录已被移动(即使您更新工作副本的遥控器),也会发生此错误。例如
$ mv /path/to/origin /somewhere/else
$ git remote set-url origin /somewhere/else
$ git diff origin/master
fatal: ambiguous argument 'origin': unknown revision or path not in the working tree.
Pulling once from the new origin
solves the problem:
从新拉一次origin
解决问题:
$ git stash
$ git pull origin master
$ git stash pop
回答by j-p
This worked for me on win replace REL_PATH_TO_FILE with the relative path to the file to remove Removing sensitive data from a repositoryThe docs say full path - but that errored for me -so I tried rel path and it worked.
这对我有用,用文件的相对路径替换 REL_PATH_TO_FILE 以删除从存储库中 删除敏感数据文档说完整路径 - 但对我来说是错误的 - 所以我尝试了 rel path 并且它起作用了。
<from the repo dir>git filter-branch --force --index-filter "git rm --cached --ignore-unmatch REL_PATH_TO_FILE" --prune-empty --tag-name-filter cat -- --all