Home > Enterprise >  How to solve error bad object refs/heads/main (1) after running git pull
How to solve error bad object refs/heads/main (1) after running git pull

Time:01-13

How can I solve this error when I run git pull in Terminal on a Mac?

remote: Enumerating objects: 115, done.
remote: Counting objects: 100% (115/115), done.
remote: Compressing objects: 100% (66/66), done.
remote: Total 104 (delta 71), reused 63 (delta 30), pack-reused 0
Receiving objects: 100% (104/104), 3.92 MiB | 3.16 MiB/s, done.
Resolving deltas: 100% (71/71), completed with 9 local objects.
fatal: bad object refs/heads/main (1)
error: https://github.com/[...domain name...]/website.git did not send all necessary objects

The background is that I have a website that usually works with no problem. I created it in RStudio and sync it with github. Netlify goes from there. Here is what the setup is based on: https://www.apreshill.com/blog/2020-12-new-year-new-blogdown/.

After I make changes in RStudio, from within RStudio, I can usually just commit and then push the changes. But this time, it says this:

 ! [rejected]        HEAD -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/[domain name]/website.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again

So I go to Terminal and change the working directory to where the files are stored and run git pull. I then get the fatal: bad object refs/heads/main (1) error mentioned above. Based on How to handle git gc fatal: bad object refs/remotes/origin/HEAD error?, I tried git gc and get this error:

error: bad ref for .git/logs/HEAD (1)
error: bad ref for .git/logs/refs/heads/main (1)
fatal: bad object refs/heads/main (1)
fatal: failed to run repack

I also tried git remote set-head origin --auto and then git gc and get the same error as directly above.

If I run cat .git/refs/remotes/origin/HEAD, it says: refs/remotes/origin/main.

Finally, I tried git reset --hard before git pull and this gives me the original error.

I am not sure what to do at this point. I am totally fine to get everything from the remote server and redo my local changes.

CodePudding user response:

In your repository a file named .git/refs/heads/main (1) exists, which also means that local branch main (1) exists. The name contains a space which is not allowed in branch names (see here), which is probably the cause of error.

The (1) suffix probably emerged accidentally. Try to remove it from file names first and then run other operations.

  • Related