How to handle git gc fatal: bad object refs/remotes/origin/HEAD error?

后端 未结 8 1439
庸人自扰
庸人自扰 2021-01-30 02:36

I randomly hit this today while trying to run Git garbage collect:

$ git gc
fatal: bad object refs/remotes/origin/HEAD
error: failed to run repack


        
相关标签:
8条回答
  • 2021-01-30 03:16
    git update-ref -d [wrong reference here]
    

    This will fix this issue.

    For above issue use following code:

    git update-ref -d 'refs/remotes/origin/HEAD'
    

    In case you are getting error with .git like below:

    error: bad ref for .git/logs/refs/remotes/origin/Dec/session-dynatrace-logs 6
    

    You can copy the path starting from refs like below:

    git update-ref -d 'refs/remotes/origin/Dec/session-dynatrace-logs 6'
    
    0 讨论(0)
  • 2021-01-30 03:20

    Looks like your symbolic-refs might be broken... Try the replacing it with your default branch like this: For example, my default branch is master

    $ git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master
    $ git fetch --prune
    $ git gc
    

    That should fix it.

    0 讨论(0)
  • 2021-01-30 03:20

    If you're using git worktrees, make sure you're doing a

    git worktree prune
    

    before running

    git gc
    

    I had a worktree get corrupted and this seemed to do the trick after removing the corrupted worktree. git prune by itself didn't seem to work.

    0 讨论(0)
  • 2021-01-30 03:27

    The cause of this for me was working in a compressed folder in Windows. When the folder was uncompressed, it corrupted the pack files, cascading other odd issues, such as not being able to prune nonexistent branches.

    The only fix was to wipe out the working directory and clone the repo remote(s) again. Luckily, I could still push and pull updates to ensure nothing was lost. All is well now.

    0 讨论(0)
  • 2021-01-30 03:27

    When all other answers fail, just restart your computer.

    I encountered OP's error and also had Permission denied on some .gitattribute file in unrelated directory (no matter if run by admin or not). This helped me.

    0 讨论(0)
  • 2021-01-30 03:28

    The problem that I ran into (which is the same problem that @Stavarengo mentioned in this comment above) is that the default remote branch (develop in my case) had been deleted, but was still referenced in .git/refs/remotes/origin/HEAD.

    Opening .git/refs/remotes/origin/HEAD in my editor showed this:

    ref: refs/remotes/origin/develop
    

    I carefully edited it to point at my new default branch and all was well:

    ref: refs/remotes/origin/master
    

    The clue that tipped me off was that running git prune showed this error:

    > git prune
    warning: symbolic ref is dangling: refs/remotes/origin/HEAD
    
    0 讨论(0)
提交回复
热议问题