Git 错误“致命:模棱两可的参数‘HEAD’:未知版本或路径不在工作树中" [英] Git error "fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree"

查看:54
本文介绍了Git 错误“致命:模棱两可的参数‘HEAD’:未知版本或路径不在工作树中"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to initialize a new Git repo from Debian (actually a VM on Virtualbox, installed and running on 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 #) $] 

有什么问题吗?

推荐答案

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

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.

% 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

看起来在使用 rev-parse 之前没有进行足够的错误检查.创建第一个提交后 .git/refs/heads 看起来有点不同,git rev-parse HEAD 将不再失败.

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

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

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

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:

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 ...
}

这篇关于Git 错误“致命:模棱两可的参数‘HEAD’:未知版本或路径不在工作树中"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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