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

GitVersion ControlGithub

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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.

Solution 3 - Git

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 https://stackoverflow.com/questions/4127967/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

Solution 4 - Git

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

Solution 5 - Git

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

Solution 6 - Git

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

Solution 7 - Git

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

Solution 8 - Git

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.

Solution 9 - Git

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

Use something like grep or findstr and remove all instances.

Solution 10 - Git

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.

Solution 11 - Git

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

Solution 12 - Git

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.

Solution 13 - Git

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>

Solution 14 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGalder Zamarre&#241;oView Question on Stackoverflow
Solution 1 - GitTor ArneView Answer on Stackoverflow
Solution 2 - GitAdam DymitrukView Answer on Stackoverflow
Solution 3 - GitTdVView Answer on Stackoverflow
Solution 4 - GitDan BerindeiView Answer on Stackoverflow
Solution 5 - GitMark LongairView Answer on Stackoverflow
Solution 6 - GitcodemonkeeView Answer on Stackoverflow
Solution 7 - GitjaisonDavisView Answer on Stackoverflow
Solution 8 - GitTarkaView Answer on Stackoverflow
Solution 9 - GitCordellView Answer on Stackoverflow
Solution 10 - GitFrank ForteView Answer on Stackoverflow
Solution 11 - Gituser13263866View Answer on Stackoverflow
Solution 12 - GitStefanoView Answer on Stackoverflow
Solution 13 - GitMari FaleirosView Answer on Stackoverflow
Solution 14 - GitEddyView Answer on Stackoverflow