开发者

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

开发者 https://www.devze.com 2023-03-12 00:53 出处:网络
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:

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 hav开发者_如何学Goe 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.


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


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.


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


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


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


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


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


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.


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

Use something like grep or findstr and remove all instances.


Had this issue after rewriting the history and deleting many branches at the same time.

rm -rf .git/refs/remotes/origin/
git fetch

solves the issue by removing all remote references, and fetching them again.


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.


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


After trying various solutions, I finally got the references cleaned up on Windows command prompt with the following:

for /f %i in ('git for-each-ref --format="%(refname)"') do git show-ref --quiet --verify %i || git update-ref -d %i


Apart from what others have mentioned, this issue can occur if you have some problem with git mirrors which you are currently using, try using other mirror images available.

  1. Get the working mirrors link
  2. Remove the existing one which is having issue

git remote -v

git remove remote origin

  1. update with working one.
git remote add origin <New mirror URL>


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

精彩评论

暂无评论...
验证码 换一张
取 消