'How can I fix the Git error "object file ... is empty"?
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?
Solution 1:[1]
The Git object files have gone corrupt (as pointed out in other answers as well). This can happen during machine crashes, etc.
I had the same thing. After reading the other top answers here I found the quickest way to fix the broken Git repository with the following commands (execute in the Git working directory that contains the .git folder):
(Be sure to back up your Git repository folder first!)
find .git/objects/ -type f -empty | xargs rm
git fetch -p
git fsck --full
This will first remove any empty object files that cause corruption of the repository as a whole, and then fetch down the missing objects (as well as latest changes) from the remote repository, and then do a full object store check. Which, at this point, should succeed without any errors (there may be still some warnings though!)
PS. This answer suggests you have a remote copy of your Git repository somewhere (e.g. on GitHub) and the broken repository is the local repository that is tied to the remote repository which is still intact. If that is not the case, then do not attempt to fix it the way I recommend.
Solution 2:[2]
This error happens to me when I am pushing my commit and my computer hangs.
This is how I've fix it.
Steps to fix
git status
Show the empty/corrupt object file
rm .git/objects/08/3834cb34d155e67a8930604d57d3d302d7ec12
remove it
git status
I got fatal: bad object HEAD message
rm .git/index
I remove the index for the reset.
git reset
fatal: Could not parse object 'HEAD'.
git status
git pull
Just to check what's happening
tail -n 2 .git/logs/refs/heads/MY-CURRENT-BRANCH
It prints the last two lines, tail -n 2, of the log branch to show my last two commit hashes.
git update-ref HEAD 7221fa02cb627470db163826da4265609aba47b2
I pick the last commit hash
git status
It shows all my file as deleted, because i removed the .git/index file
git reset
Continue to the reset
git status
Verify my fix
Solution 3:[3]
I solved this removing the various empty files that git fsck was detecting, and then running a simple Git pull.
I find it disappointing that now that even filesystems implement journaling and other "transactional" techniques to keep the file system sane, Git can get to a corrupted state (and not be able to recover by itself) because of a power failure or space on the device.
Solution 4:[4]
I just had the same issue: after pulling the distant repository, when I did a git status I got:
"error: object file (...) is empty"
"fatal: loose object (...) is corrupted"
The way I resolved this was to:
git stash- removing the Git file in error (I am not sure that it was necessary)
git stash clear
I don’t know exactly what things happened, but that instructions seemed to make everything clean.
Solution 5:[5]
Because I have to reboot my VM regularly, somehow this problem happens to me very often. After few times of it, I realized I cannot repeat the process described by Nathan Vanhoudnos every time this happens, though it always works. Then I figured out the following faster solution.
Step 1
Move your entire repository to another folder.
mv current_repository temp_repository
Step 2
Clone the repository from origin again.
git clone source_to_current_repository.git
Step 3
Remove Everything under the new repository except the .git folder.
Step 4
Move everything from the temp_repository to the new repository except the .git folder.
Step 5
Remove the temp_repository, and we are done.
After a few times, I'm sure you can do these procedures very quickly.
Solution 6:[6]
- Move your app folder to make a backup, i.e., mv app_folder app_folder_bk (it is like a git stash)
git clone your_repository- Finally, open a merge tool (I use the Meld diff viewer on Linux or WinMerge on Windows) and copy the changes from right (app_folder_bk) to left (new app_folder) (it is like a git stash apply).
That's all. Maybe it is not the best way, but I think it is so practical.
Solution 7:[7]
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://[email protected]/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.
Solution 8:[8]
Let's go simple... only the case you uploaded source to a remote Git repository
Backup your .git folder
Check your Git repository
git fsck --fullRemove empty object files (all)
rm .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68eCheck your Git repository again.
git fsck --fullPull your source from the remote Git repository
git pull origin master
Solution 9:[9]
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
Solution 10:[10]
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"
Solution 11:[11]
In my case, this error occurred because I was typing the commit message and my notebook turned off.
I did these steps to fix the error:
git checkout -b backup-branch# Create a backup branchgit reset --hard HEAD~4# Reset to the commit where everything works well. In my case, I had to back four commits in the head, that is until my head be at the point before I was typing the commit message. Before doing this step, copy the hash of the commits you will reset. In my case I copied the hash of the four last commits.git cherry-pick <commit-hash># Cherry pick the reset commits (in my case are four commits, so I did this step four times) from the old branch to the new branch.git push origin backup-branch# Push the new branch to be sure everything works wellgit branch -D your-branch# Delete the branch locally ('your-branch' is the branch with problem)git push origin :your-branch# Delete the branch from remotegit branch -m backup-branch your-branch# Rename the backup branch to have the name of the branch that had the problemgit push origin your-branch# Push the new branchgit push origin :backup-branch# Delete the backup branch from remote
Solution 12:[12]
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.
- Rename the current working directory. (
old_projectfor this example). - Clone the repository within a new directory using
git clone. - On the command line, change the working directory to the newly created project and switch to the branch you have been working on.
- Copy all files and directories within
old_project(except the.gitdirectory) to the newly created project directory. - Check your working tree status (note that there are many more changes than you expect) and then commit the changes.
I hope it helps...
Solution 13:[13]
I am assuming you have a remote with all relevant changes already pushed to it. I did not care about local changes and simply wanted to avoid deleting and recloning a large repository. If you do have important local changes you might want to be more careful.
I had the same problem after my laptop crashed.
Probably because it was a large repository I had quite a few corrupt object files, which only appeared one at a time when calling git fsck --full, so I wrote a small shell one-liner to automatically delete one of them:
$ sudo rm `git fsck --full 2>&1 | grep -oE -m 1 ".git/objects/[0-9a-f]{2}/[0-9a-f]*"`
2>&1redirects the error message to standard output to be able to grep it- grep options used:
-oonly returns the part of a line that actually matches-Eenables advanced regular expressions-m 1make sure only the first match is returned[0-9a-f]{2}matches any of the characters between 0 and 9 and a and f if two of them occur together[0-9a-f]*matches any number of the characters between 0 and 9 and a and f occurring together
It still only deletes one file at a time, so you might want to call it in a loop like:
$ while true; do sudo rm `git fsck --full 2>&1 | grep -oE -m 1 ".git/objects/[0-9a-f]{2}/[0-9a-f]*"`; done
The problem with this is, that it does not output anything useful any more, so you do not know when it is finished (it should just not do anything useful after some time).
To "fix" this I then just added a call of git fsck --full after each round like so:
$ while true; do sudo rm `git fsck --full 2>&1 | grep -oE -m 1 ".git/objects/[0-9a-f]{2}/[0-9a-f]*"`; git fsck --full; done
It now is approximately half as fast, but it does output its "state".
After this I played around some with the suggestions in the answers here and finally got to a point where I could git stash and git stash drop a lot of the broken stuff.
First problem solved
Afterwards I still had the following problem:
unable to resolve reference 'refs/remotes/origin/$branch': reference broken which could be solved by
$ rm \repo.git\refs\remotes\origin$branch
$ git fetch
I then did a
$ git gc --prune=now
$ git remote prune origin
for good measure and
git reflog expire --stale-fix --all
to get rid of error: HEAD: invalid reflog entry $blubb when running git fsck --full.
Solution 14:[14]
I fixed my git error: object file is empty by:
- Saving a copy of all files that I edited since my last successful commit/push,
- Removing and re-cloning my repository,
- Replacing the old files with my edited files.
Solution 15:[15]
Here is a really simple and quick way to deal with this problem IF you have a local repo with all the branches and commits you need, and if you're OK with creating a new repo (or deleting the server's repo and making a new one in its place):
- Create a new empty repo on the server (or delete the old repo and create a new one in its place)
- Change the remote URL of your local copy to point to the remote URL of the new repo.
- Push all branches from your local repo to the new server repo.
This preserves all the commit history and branches that you had in your local repo.
If you have collaborators on the repo, then I think in many cases all your collaborators have to do is change the remote URL of their local repo as well, and optionally push any commits they have that the server doesn't have.
This solution worked for me when I ran into this same problem. I had one collaborator. After I pushed my local repo to the new remote repo, he simply changed his local repo to point to the remote repo URL and everything worked fine.
Solution 16:[16]
In this situation I solve my issue by following this issue.
- Delete .git folder from my repository directory. (keep backup for safe)
- clone my repo in another directory.
- copy the .git folder from new cloned directory.
- 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.
Solution 17:[17]
I encountered the same problem, and I used a very simple way to fix it. I found that those missing files existed on my teammate's computer.
I copied those files one by one to a Git server (9 files total), and that fixed the problem.
Solution 18:[18]
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.
Solution 19:[19]
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/
cp projects/corrupt_git projects/backup- (optional) make a backupgit clone [repo URL] projects/clean_git- so that you getprojects/clean_gitrm -rf corrupt_git/.git/- remove the corrupted .git foldermv clean_git/.git/ corrupt_git/- move clean git tocorrupt_git/.gitgit statusinprojects/corrupt_git- to make sure it worked
Solution 20:[20]
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:
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.
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.
Solution 21:[21]
Copy everything (in the folder containing the .git folder) to a backup, delete everything, and restart. Make sure you have the Git remote handy:
git remote -v
origin [email protected]:rwldrn/idiomatic.js.git (fetch)
origin [email protected]:rwldrn/idiomatic.js.git (push)
Then
mkdir mygitfolder.backup
cp mygitfolder/* mygitfolder.backup/
cd mygitfolder
rm -r * .git*
git init
git remote add origin [email protected]:rwldrn/idiomatic.js.git
Then merge any new files manually, and try to keep your computer turned on.
Solution 22:[22]
I had the same problem after checking out master from a clean branch.
After a while I recognized a lot of modified files in master. I don't know why they have been there, after switching from a clean branch. Anyway, because the modified files made no sense to me, I just stashed them and the error was gone.
git:(master) git stash
Solution 23:[23]
The twelve-step solution covered by Nathan VanHoudnos helped get me out of a jam as well. Thanks. The key steps were to enter:
git fsck --full
and remove all empty objects
rm .git/objects/...
Then getting the two lines of the flog:
tail -n 2 .git/logs/refs/heads/master
With the returned values
git update-ref HEAD ...
At this point I had no more errors, so I made a backup of my most recent files. Then do a Git pull followed by a Git push. I copied my backups to my Git repository file and did another Git push. That got me current.
Solution 24:[24]
If you have an old backup and are in a hurry:
Make a new backup of your current, Git-broken, project path.
- move your
.gitto trash (never delete) - copy
.gitfrom the old backup git pull(will create merge conflicts)- move all your sources (everything you put in Git) to trash:
./src(never delete) - Copy all your sources (everything you put in Git) from the new backup
- accept all "merges" at
git gui, push and... clap your hands!
Solution 25:[25]
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 26:[26]
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.
Solution 27:[27]
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
a0e726cd3505a9be8dffaa78077dfe3a497eaf
Or
rm .git/objects/f1/a0e726cd3505a9be8dffaa78077dfe3a497eaf
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
