8

After a few month not working on some static website, I came back and tried to pull changes from a GitHub repo.

The following fatal: bad object refs/heads 2/master error occured.

(base) ➜  github_repo git:(master) ✗ git pull
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 21 (delta 3), reused 3 (delta 3), pack-reused 18
Unpacking objects: 100% (21/21), 6.95 KiB | 790.00 KiB/s, done.
fatal: bad object refs/heads 2/master
error: https://github.com/asafmaman101/asafmaman101.github.io.git did not send all necessary objects

Tried some solutions for similar problems here from StackOverflow and tried also to update Git version on my mac.

Nothing helped. I'm trying to prevent from deleting local copy and re-cloning the repo because I have local changes that I don't want to lose. Any other ideas?

3
  • Run git ls-remote to see if there is such a ref refs/heads 2/master. If there is, it should be removed.
    – ElpieKay
    Jun 6, 2022 at 10:18
  • Hi, there is refs/heads/master but no 2/master. I suppose that might be the problem - how should I fix it?
    – A. Maman
    Jun 6, 2022 at 10:22
  • Look inside your .git/refs directory. Is there a heads and also a heads 2? If so, this looks like something done by Dropbox or iCloud. You're going to have to resolve it manually, and also stop storing .git directories in cloud-managed spaces.
    – torek
    Jun 6, 2022 at 16:13

2 Answers 2

27

I had a similar problem with a " 2" suffix being added to a filename within the .git directory. The git repository is in a directory synced by iCloud Drive, so presumably iCloud in its infinite wisdom added the suffix during a sync operation.

I originally encountered the issue via a cryptic error message in SourceTree. Running git gc on the command line helped me narrow down the issue:

> git gc
fatal: bad object refs/heads/1.2 2
fatal: failed to run repack

I was able to fix the issue by removing the " 2" suffix:

> mv .git/refs/heads/1.2\ 2 .git/refs/heads/1.2
4
  • 4
    Yup, worked for me. I think it was Google Drive in my case. Thank you! Oct 31, 2022 at 20:24
  • 9
    Worked for me too, thanks! I'm on a Mac, so I hit cmd+shift+period to show hidden files, then just navigated to the hidden .git folder in my directory, drilled down, and got rid of the offending "(1)" suffix on my problem file. Yep, I think was Google Drive in my case as well. Feb 3 at 1:04
  • worked for a similar case for me twice today. I think iCloud messed up Mar 23 at 1:53
  • In my case the branch name is develop_abc (2). So you ned to run command refs/head/develop_abc\ * .git/refs/heads/develop_abc
    – CauCuKien
    Jul 8 at 4:36
9

to elaborate on @Robin's answer here is what worked for me:

first, check the content of the origin directory

ls -l .git/refs/remotes/origin

result was HEAD HEAD 2 ..

so, what i did is I removed the duplicate HEAD 2 file using

rm '.git/refs/remotes/origin/HEAD 2'

then I fetched the remotes branches using

git fetch

then set local branch to track remote branch

git branch --set-upstream-to=origin/{YOUR_REMOTE_BRANCH}

that's it.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.