GIT 致命:模棱两可的参数“HEAD”:未知版本或路径不在工作树中

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

GIT fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree

macosgitvirtualbox

提问by David

I'm trying to initialize a new GIT repo from Debian (actually a VM on Virtualbox, installed and running on Mac OS X) :

我正在尝试从 Debian 初始化一个新的 GIT 存储库(实际上是 Virtualbox 上的 VM,在 Mac OS X 上安装并运行):

[david@server-VM-001:~ $] mkdir test
[david@server-VM-001:~ $] cd test
[david@server-VM-001:test $] git init
Initialized empty Git repository in /home/david/test/.git/
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
[david@server-VM-001:test  (master #) $] 

What's the problem?

有什么问题?

回答by Jacob Helwig

As others pointed out, this message is coming from your shell prompt. The problem is that in a freshly created repository HEAD(.git/HEAD) points to a ref that doesn't exist yet.

正如其他人指出的那样,此消息来自您的 shell 提示。问题是在新创建的存储库中HEAD( .git/HEAD) 指向尚不存在的引用。

% git init test
Initialized empty shared Git repository in /Users/jhelwig/tmp/test/.git/
% cd test
% cat .git/HEAD
ref: refs/heads/master
% ls -l .git/refs/heads
total 0
% git rev-parse HEAD
HEAD
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

It looks like rev-parseis being used without sufficient error checking before-hand. After the first commit has been created .git/refs/headslooks a bit different and git rev-parse HEADwill no longer fail.

看起来rev-parse在没有事先进行足够的错误检查的情况下使用。创建第一个提交后.git/refs/heads看起来有点不同,并且git rev-parse HEAD不会再失败。

% ls -l .git/refs/heads
total 4
-rw------- 1 jhelwig staff 41 Oct 14 16:07 master
% git rev-parse HEAD
af0f70f8962f8b88eef679a1854991cb0f337f89

In the function that updates the Git information for the rest of my shell prompt (heavily modified version of wunjo prompt theme for ZSH), I have the following to get around this:

在为我的 shell 提示的其余部分更新 Git 信息的函数中(ZSH 的 wunjo 提示主题的重大修改版本),我有以下方法来解决这个问题:

zgit_info_update() {
    zgit_info=()

    local gitdir=$(git rev-parse --git-dir 2>/dev/null)
    if [ $? -ne 0 ] || [ -z "$gitdir" ]; then
        return
    fi

    # More code ...
}

回答by J.Adler

I usually use git on my linux machine, but at work I have to use Windows. I had the same problem when trying to commit the first commit in a Windows environment.

我通常在我的 linux 机器上使用 git,但在工作中我必须使用 Windows。尝试在 Windows 环境中提交第一次提交时遇到了同样的问题。

For those still facing this problem, I was able to resolve it as follows:

对于那些仍然面临这个问题的人,我能够解决它如下:

$ git commit --allow-empty -n -m "Initial commit".

回答by KenStipek

I had this issue when having a custom display in my terminal when creating a new git project (I have my branch display before the pathname e.g. :/current/path). All I needed to do was do my initial commit to my master branch to get this message to go away.

在创建新的 git 项目时,我在终端中有自定义显示时遇到了这个问题(我的分支显示在路径名之前,例如:/current/path)。我需要做的就是对我的主分支进行初始提交,以使此消息消失。

回答by VonC

Jacob Helwigmentions in his answerthat:

Jacob Helwig在他的回答中提到:

It looks like rev-parse is being used without sufficient error checking before-hand

看起来在使用 rev-parse 之前没有进行足够的错误检查

Commit 62f162ffrom Jeff King (peff)should improve the robustness of git rev-parsein Git 1.9/2.0 (Q1 2014) (in addition of commit 1418567):

来自Jeff King ( peff) 的Commit 62f162f应该提高git rev-parseGit 1.9/2.0 (Q1 2014)的健壮性(除了commit 1418567):

For cases where we do not match (e.g., "doesnotexist..HEAD"), we would then want to try to treat the argument as a filename.
try_difference()gets this right, and always unmunges in this case.
However, try_parent_shorthand()never unmunges, leading to incorrect error messages, or even incorrect results:

对于不匹配的情况(例如,“ doesnotexist..HEAD”),我们将尝试将参数视为文件名。
try_difference()做对了,在这种情况下总是 unmunges。
然而,try_parent_shorthand()永远不要取消,导致错误的错误信息,甚至错误的结果:

$ git rev-parse foobar^@
foobar
fatal: ambiguous argument 'foobar': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

回答by Jignesh Patel

I had same issue and I solved it by "pod setup" after installing cocoapods.

我有同样的问题,我在安装 cocoapods 后通过“pod setup”解决了它。