Getting rid of '… does not point to a valid object' for an old git branch

前端 未结 13 2001
不知归路
不知归路 2020-12-13 03:51

I have a fork of a Git repo and my clone appears to have an issue with an old, no longer existant, branch. I keep seeing this message:

error: refs/heads/t_1140

相关标签:
13条回答
  • 2020-12-13 04:16

    This will clean out any missing refs:

    git for-each-ref --format="%(refname)" | while read ref; do
        git show-ref --quiet --verify $ref 2>/dev/null || git update-ref -d $ref
    done
    
    0 讨论(0)
  • 2020-12-13 04:17

    Do a text search through your .git directory for your branch

    Use something like grep or findstr and remove all instances.

    0 讨论(0)
  • 2020-12-13 04:18

    I had this problem and none of the remedies suggested above worked. So I edited .git/packed-refs and removed the line that mentioned the non-existent branch. All was suddenly well.

    Gotta love those human-readable file formats ...

    0 讨论(0)
  • 2020-12-13 04:19

    I run into this error on a regular basis. git remote prune origin does not work for me.

    [ Update. AFAIU, I'm running into this problem because of using git alternate. Say I've got repo A, registered as alternate for repo B. When I create a new branch br in repo A, and fetch repo A as remote in repo B, git will create a remote ref .git/refs/remotes/A/br for the new branch. When I delete the branch in repo A, and after the corresponding object is garbage-collected, I get the 'error: refs/remotes/A/br does not point to a valid object!' ]

    I've written this script (updated to deal with packed refs) to remove the dangling refs (using the info in Validate if commit exists).

    #!/bin/sh
    
    set -e
    
    if [ $# -eq 0 ]; then
        dir="."
    else
        dir="$1"
    fi
    
    if [ ! -d "$dir" ]; then
        echo "not a dir: $dir"
        exit 1
    fi
    
    if [ ! -d "$dir/.git" ]; then
        echo "not a git repo: $dir"
        exit 1
    fi
    
    cd "$dir"
    
    files=$(find .git/refs -type f)
    
    for f in $files; do
        id=$(cat "$f")
        if ! git rev-parse --quiet "$id" \
        >/dev/null 2>&1; then
        continue
        fi
        if ! git rev-parse --quiet --verify "$id^{commit}" \
        >/dev/null 2>&1; then
        echo "Removing ref $f with missing commit $id"
        rm "$f"
        fi
    done
    
    if [ ! -f .git/packed-refs ]; then
        exit 0
    fi
    
    packfiles=$(cat .git/packed-refs \
        | grep -v '#' \
        | awk '{print $2}')
    
    for f in $packfiles; do
        if ! git rev-parse --quiet --verify "$f" \
        >/dev/null 2>&1; then
        continue
        fi
        id=$(git rev-parse "$f")
        if ! git rev-parse --quiet --verify "$id" \
        >/dev/null 2>&1; then
        continue
        fi
        if ! git rev-parse --quiet --verify "$id^{commit}" \
        >/dev/null 2>&1; then
        echo "Removing packed ref $f with missing commit $id"
        git update-ref -d $f
        fi
    done
    
    0 讨论(0)
  • 2020-12-13 04:19
    error: refs/heads/t_1140 does not point to a valid object!
    

    So let's say you've tried pruning with this:

    git remote prune origin

    and you still CAN'T GET IT TO WORK, as a last resort, try deleting t_1140

    Basically,

    1. cd refs/heads

    2. rm -r t_1140

    0 讨论(0)
  • 2020-12-13 04:24

    Your local clone is probably fine, the problem is that the t_1140 branch objects are missing from your GitHub repository.

    I had this problem too and GitHub support fixed it, I think by deleting refs/heads/t_1140 on their end.

    Update: I got the error again with another branch and I was able to fix it by running this command:

    git push origin :refs/heads/t_ispn982_master
    

    You should get a warning message like this:

    remote: warning: Allowing deletion of corrupt ref.
    

    but the corrupted branch will be deleted

    0 讨论(0)
提交回复
热议问题