84

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.

4
  • By the way, I'm getting this error while for example, pushing master git push origin master and I know for sure I'm in master: Commented Jun 7, 2011 at 13:28
  • This is getting worse, it now affects my newly created branches too as shown in gist.github.com/1012439. After pushing the newly created t_1144 branch, I suddenly get error messages for this branch anytime I try to push another branch. And from that point, any try to sync up with an upstream repo shows that error and the t_1140 ones: gist.github.com/1012452 Commented Jun 7, 2011 at 15:20
  • I've wiped out my fork and forked again. It all looks good now. Commented Jun 8, 2011 at 8:26
  • Possibly related: stackoverflow.com/q/20663239/5419599 Commented Jul 22, 2017 at 2:19

15 Answers 15

69

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 
Sign up to request clarification or add additional context in comments.

2 Comments

This safely deletes the local ref without doing any destructive operation on the remote. Removing the --quiet flag and 2>/dev/null for verbose output also helps.
This saved my repo! thanks a lot.
37

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.

4 Comments

Hmmm, that did not work. I see no reference to t_1140 there and I still have the same error. Thanks for help!
@GalderZamarreño: What does git show-ref show? The branch ref may have been packed and won't necessarily correspond to a file under .git/refs.
The prune didn't work for me either. What I did was navigating to the specified folder ".git/refs/remotes/origin" and then I simply deleted the file matching my error message. That made the error go away.
This does not solve the OP's problem. See stackoverflow.com/a/59162332/2761869
28

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 

3 Comments

This script solved it for me. In my case I was using git clone --reference path/to/existing/clone.git [email protected]:remote-repo to re-use already existing local clone over a slow connection when cloning the same repository multiple times. Probably some refs got deleted in one clone and the other did not know about it.
See stackoverflow.com/a/59162332/2761869 for a git one-liner that does the same.
I face this issue because I accidentally disconnected the hard drive from my laptop. This script solved the issue. Thank you!
16

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

Comments

6
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

4 Comments

I ended up having to do this method, too. I used find ./.git -name '*NAME*' and used rm on the result. In my case it was a ./.git/refs/tags entry. After manually removing the offending item, I was able to update my repo.
Delete the Tag, it works
Yep rm ref/heads/<my corrupt branch> worked for me too. Thankfully I hadn't done much work on that branch.
Give this man a coockie.
3

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

2 Comments

You should never assume that all refs corresponded to files in .git/refs as they may have been packed. It is much better to use git show-ref which will show packed and unpacked refs.
@Charles Bailey: thanks for the correction, I'd quite forgotten about packed refs. I'll leave this undeleted anyway in case it's helpful to someone else making the same mistake.
3

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

3 Comments

How is this relevant?
How is it not relevant @Buffalo ? "I had this issue" referring to the OP's question and my output shows the same error message the OP refers to "does not point to a valid object!", my resolution was to update the Git binary.
I had this issue in AWS CodeBuild: changing image from aws/codebuild/standard:4.0 to aws/codebuild/standard:5.0 resolved it (Ubuntu 18.04 to 20.04).
3

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.

Comments

2

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.

1 Comment

Yes I think this is the actual answer to the question. Remove the sha1 from packed-refs or if you feel brake remove .git/packed-refs entirely. It will require a git fetch <remote> after this step.
1

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

Use something like grep or findstr and remove all instances.

1 Comment

This worked for me. Edited both .git/packed-refs and .git/info/refs
0

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.

Comments

0

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

Comments

0

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 

Comments

0

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> 

Comments

-1

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> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.