How to resolve the fatal bad object error in Git while using Google Drive?

I’m facing an issue with my Git files that are stored in Google Drive. I have made several commits without problems until recently when I attempted to switch branches.

This is what I’ve done so far. I recorded these commits in my project:

5c1bd8f (HEAD -> master) 5th commit - robots.txt
ca656ac 4th commit - robots.txt
3850e6c 3rd commit - robots.txt
bd5b39f 2nd commit - robots.txt
6f3ee36 1st commit - robots.txt
a177016 All files committed

To check a previous commit, I ran git checkout bd5b39f, but when I tried to return to the master branch by typing git checkout master, I received this message:

$ git checkout master
fatal: bad object refs/desktop.ini

It seems that Google Drive is generating some hidden files that are causing this disruption. When I attempted to execute garbage collection, I encountered this message:

$ git gc
error: bad ref for .git/logs/refs/heads/desktop.ini
fatal: bad object refs/desktop.ini  
fatal: failed to run repack

I’ve tried removing the desktop.ini file, but the issue remains. Has anyone experienced this before? What steps should I take to fix my repository while ensuring my commits are preserved?

Same thing happened to me last year. Google Drive creates system files that mess with Git’s internal structure. Here’s what fixed it for me: First, back up your whole repo somewhere else. Then completely remove the project from Google Drive sync. Go into the .git directory and clean out any desktop.ini files from the refs folder. Run git show-ref to see which references are corrupted. After cleaning, use git fsck to verify everything’s working. Don’t store Git repos directly in cloud sync folders - these services don’t understand Git’s file locking. Use proper Git hosting instead, or at least exclude the .git folder from syncing if you need your working directory in Google Drive.

I faced a similar issue with Google Drive’s interference. It generates desktop.ini files that can corrupt the Git reference structure. To resolve it, I recommend temporarily stopping syncing the folder, navigating to .git/refs/heads/, and manually deleting any desktop.ini files. Afterward, run git reflog to ensure your commit history remains intact. If there are still issues with refs, you may need to recreate the master branch using git update-ref refs/heads/master <latest_commit_hash>. In the future, consider using dedicated Git hosting like GitHub or GitLab, as cloud storage can lead to these problems.

google drive can be a pain for git. move your .git folder to a local dir, then run git fsck --full to look for any issues. if desktop.ini is messing things up, you might have to sort those refs out manually. hope that helps!