How can I fix the Git error "object file ... is empty"?

Viewed 278023

When I try to commit changes, I get this error:

error: object file .git/objects/31/65329bb680e30595f242b7c4d8406ca63eeab0 is empty
fatal: loose object 3165329bb680e30595f242b7c4d8406ca63eeab0 (stored in .git/objects/31/65329bb680e30595f242b7c4d8406ca63eeab0) is corrupt

I tried git fsck I've got:

error: object file .git/objects/03/dfd60a4809a3ba7023cbf098eb322d08630b71 is empty
fatal: loose object 03dfd60a4809a3ba7023cbf098eb322d08630b71 (stored in .git/objects/03/dfd60a4809a3ba7023cbf098eb322d08630b71) is corrupt

How can I solve this error?

28 Answers

I run into this problem a lot with virtual machines.

For me the following works:

cd /path/to/your/project
rm -rf .git

If you want to save yourself some downloads - go in your file explorer and delete all files in the folder that are already committed and leave in your /vendor and /node_modules (I work with PHP Composer and npm) folders.

Then just create a new repository:

git init

Add your remote

git remote add origin ssh://git@github.com/YourUsername/repoName.git

And fetch the branch / all of it

git fetch origin somebranch

And check it out

git checkout somebranch

Then you should be at the point before the error.

This resolved my problem:

git stash
git checkout master
cd .git/ && find . -type f -empty -delete
git branch your-branch-name -D
git checkout -b your-branch-name
git stash pop

In one script

#! /bin/sh

# Save Git data
cp -r .git gitold

# Remove all empty Git object files
find .git -type f -empty -delete -print

# Get the current branch name
branchname=$(git branch --show-current)

# Get the latest commit hash
commit=$(tail -2 .git/logs/refs/heads/$branchname | awk '{ print $2 }' | tr -d '[:space:]')

# Set HEAD to this latest commit
git update-ref HEAD $commit

# Pull the latest changes on the current branch (considering remote is origin)
git pull origin $branchname

echo "If everything looks fine you remove the git backup running :\n\
      $ rm -rf gitold \n\
Otherwise restore it with: \n\
      $ rm -rf .git; mv gitold .git"

My colleagues and I have crossed several times with this same problem and to solve it we simply do the steps that I describe below. It is not the most elegant solution that can be found but it works without loss of data.

  1. Rename the current working directory. (old_project for this example).
  2. Clone the repository within a new directory using git clone.
  3. On the command line, change the working directory to the newly created project and switch to the branch you have been working on.
  4. Copy all files and directories within old_project (except the .git directory) to the newly created project directory.
  5. Check your working tree status (note that there are many more changes than you expect) and then commit the changes.

I hope it helps...

I fixed my git error: object file is empty by:

  1. Saving a copy of all files that I edited since my last successful commit/push,
  2. Removing and re-cloning my repository,
  3. Replacing the old files with my edited files.

In this situation I solve my issue by following this issue.

  1. Delete .git folder from my repository directory. (keep backup for safe)
  2. clone my repo in another directory.
  3. copy the .git folder from new cloned directory.
  4. paste in my previous directory where the issue arise.

check git status, hopefully you can see your all change. now you can commit and push.

Here is a way to solve the problem if your public repository on github.com is working, but your local repository is corrupt. Be aware that you will lose all the commits you've done in the local repository.

Alright, so I have one repository locally that is giving me this object empty error, and the same repository on github.com, but without this error. So what I simply did was to clone the repository that is working from GitHub, and then copied everything from the corrupt repository (except the .git folder), and paste it the cloned repository that is working.

This may not be a practical solution (since you delete the local commits), however, you maintain the code and a repaired version control.

Remember to back up before applying this approach.

In my case, it wasn't important for me to keep the local commit history. So if that applies to you too, you can do this as a quick alternative to the solutions above:

You basically just replace the corrupted .git/ directory with a clean one.

Let’s presume the following directory for your project with the corrupted Git files: projects/corrupt_git/

  1. cp projects/corrupt_git projects/backup - (optional) make a backup
  2. git clone [repo URL] projects/clean_git - so that you get projects/clean_git
  3. rm -rf corrupt_git/.git/ - remove the corrupted .git folder
  4. mv clean_git/.git/ corrupt_git/ - move clean git to corrupt_git/.git
  5. git status in projects/corrupt_git - to make sure it worked

This also happens to me almost regularly. I haven't made a protocol when this happens exactly, but I have a suspicion that it occurs whenever my virtual machine (VM) exists "unexpectedly". If I close the VM window (I am using Ubuntu 18.04 (Bionic Beaver)) and start again, things always(?) work. But if the VM window is still open when my laptop is shut down (Windows host system), then I encounter this problem rather frequently.

As to all the answers given here:

  1. thank you - they are very useful; I usually save a local copy of my code, restore the repository from remote, and move the backup copy back into the local folder.

  2. as the underlying problem is not really a Git issue, but rather a VM and/or Linux issue, I wonder if there shouldn't be a way to cure the reason rather the symptoms? Doesn't this kind of error indicate that some file system changes are not "applied" in any reasonable time, but only cached? (see for example Are file edits in Linux directly saved into disk?) -- to me it appears as if virtual Linux machines don't fsynch their stuff frequently enough. Whether this is an issue of Oracle's VirtualBox (which otherwise works very nicely) or of the guest file system, or of some settings, which we all overlook, is beyond my expertise. But I would be happy if someone could shed light on this.

I had the same issue after my VM crashed and Git files got corrupted.

First step, from the root folder of the project.

find .git/objects -type f -empty -delete

Then a prune and a fetch...

git prune
git fetch --all --prune

And some rollbacks, and it got to working.

Actually, I had the same problem.

Have a copy of your code before trying this.

I just did git reset HEAD~

And my last commit was undone. Then I committed it again, and the problem was solved!

Solution of this problem is simple

• Just locate that file

• Like in my case it was

error: object file .git/objects/f1/a0e726cd3505a9be8dffaa78077dfe3a497eaf is empty
fatal: loose object f1a0e726cd3505a9be8dffaa78077dfe3a497eaf (stored in .git/objects/f1/a0e726cd3505a9be8dffaa78077dfe3a497eaf) is corrupt

And simply delete it a0e726cd3505a9be8dffaa78077dfe3a497eafenter image description here

Or

rm .git/objects/f1/a0e726cd3505a9be8dffaa78077dfe3a497eaf
Related