A. Maman
A. Maman

Reputation: 972

Git fatal: bad object refs/heads 2/master

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?

Upvotes: 11

Views: 29703

Answers (3)

Katie Lacy
Katie Lacy

Reputation: 41

For me, the solution above didn't work. When I ran

ls -l .git/refs/remotes/origin

nothing with " 2" was shown.

Additionally, running git branch also didn't list it.

Eventually, I tried

git branch -d "whatever 2"

that successfully deleted the hidden branch. I hope this helps someone!

Upvotes: 1

MoSwilam
MoSwilam

Reputation: 944

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.

Upvotes: 16

Robin Stewart
Robin Stewart

Reputation: 3883

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

Upvotes: 33

Related Questions