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

后端 未结 5 3210
一向
一向 2020-11-29 20:44

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
         


        
5条回答
  •  旧时难觅i
    2020-11-29 21:30

    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
    

    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
    

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

提交回复
热议问题