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

天大地大妈咪最大 提交于 2019-12-18 10:28:02

问题


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 does not point to a valid object!

I don't have any other messages and the repo works fine. There's no operation that stops me from working on other branches, pushing changes, pulling...etc.

I've looked around and there's less than clear instructions on how to get around this issue. I've tried to execute git fsck --full but I see no errors. Just a load on dangling ... messages.

I've also checked my .git/config and there's no references to this branch and have also checked .git/refs/heads and there's no reference to t_1140

Any idea how to get rid of this error?

p.s I've tried to clone my repo again and it seems like the error is my Github repo too. So, the only thing I can think of right now is to ditch my repo and fork again.


回答1:


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



回答2:


Check .git/refs/remotes/origin. They are there and the upstream no longer has them. To clean out the remotes that no longer exist run

git remote prune origin

You could also see what it would to by adding --dry-run before actually doing it.




回答3:


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




回答4:


You say that you have:

also checked .git/refs/heads and there's no reference to t_1140

... which is very surprising. I can only see how this error would occur if the file .git/refs/heads/t_1140 exists. Is it possible you were mistaken about this?

Correction: Charles Bailey points out below that the refs might be packed, in which case there is no corresponding file in .git/refs/heads




回答5:


I had this issue when attempting to clone some github repositories, the system I was on was running an older version of git v1.7.4, a quick update fixed it up.

remote: Counting objects: 533, done.
remote: Compressing objects: 100% (248/248), done.
remote: Total 533 (delta 232), reused 529 (delta 230)
Receiving objects: 100% (533/533), 121.36 KiB, done.
Resolving deltas: 100% (232/232), done.
error: refs/remotes/origin/master does not point to a valid object!
verror: Trying to write ref refs/heads/master with nonexistant object 0457f3e2e9432e07a1005f0f4161dc4b8215f128
fatal: Cannot update the ref 'HEAD'.



回答6:


If it's failing with this:

error: failed to run repack

Look in .git/packed-refs for the listed branch(es) and remove those lines. I tried all the other solutions, but this finally solved it for me.




回答7:


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

Use something like grep or findstr and remove all instances.




回答8:


I'm giving my two cents for whoever is using Visual Studio. I had this issue while trying to delete a local branch and running the following command through the command line solved it:

git branch -D <branchName>



回答9:


This fixed it for me:

git push origin :refs/remotes/origin/[branch name]
git push origin :refs/heads/origin/[branch name]

WARNING: this deletes the branch from the server - any changes on that branch that have not been merged to another branch will be lost.




回答10:


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




回答11:


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


来源:https://stackoverflow.com/questions/6265502/getting-rid-of-does-not-point-to-a-valid-object-for-an-old-git-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!