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

30

642

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?

Orometer answered 29/7, 2012 at 2:38 Comment(11)
Did you forcibly kill a git add operation? Is your hard disk full?Sext
No, my hard disk is not full, I don't remember that I forcibly killed a git add operation, what if I did ? how can I solve this ?Orometer
no, the error is still there ...Orometer
If this repository exists on a remote repository, you could try copying that file from there to your local one if exists on your remote repository.Prog
I got this error when my permissions in the .git directory got screwed up somehow and I didn't have read access. So it can happen in cases where the files aren't empty but they just can't be written to. Fixing permissions and running git fsck took care of it.Roseanneroseate
possible duplicate of How do I deal with corrupted git object files?Narbonne
This can happen if your network is down in between a commit. Follow the accepted answer steps to resolve issueVictimize
I tried popular answers below to no avail and simply deleting my git and copying the remote git solved it for me. Found the answer hereOftentimes
One simple solution that : I have cloned the repo at a different place and repaced old .git from newly cloned .git file.Sitwell
In my case, I was getting these errors while pushing to the remote, and I somehow switched to the master branch and am now unable to switch to the corrupted branch. I thought of trying mentioned solutions here but now can't even see my old changes locally. Any resolution to this?Standley
I keep getting this error whenever I have to force reboot my virtual box because it freezes up. My git commits and pushes from before the freeze are fine though, so in my case the solution to the problem was to copy just the missing object file from a fresh clone of the repository. Thanks @ShahVipul for giving me that idea.Pastose
P
1169

I had a similar problem. My laptop ran out of battery during a Git operation. Boo.

I didn't have any backups. (N.B. Ubuntu One is not a backup solution for Git; it will helpfully overwrite your sane repository with your corrupted one.)

To the Git wizards, if this was a bad way to fix it, please leave a comment. It did, however, work for me... at least temporarily.

Step 1: Make a backup of folder .git (in fact I do this in between every step that changes something, but with a new copy-to name, e.g., .git-old-1, .git-old-2, etc.):

cd ~/workspace/mcmc-chapter
cp -a .git .git-old

Step 2: Run git fsck --full

git fsck --full

error: object file .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e is empty
fatal: loose object 8b61d0135d3195966b443f6c73fb68466264c68e (stored in .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e) is corrupt

Step 3: Remove the empty file. I figured what the heck; it's blank anyway.

rm .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e

rm: remove write-protected regular empty file `.git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e'? y

Step 3: Run git fsck again. Continue deleting the empty files. You can also cd into the .git directory and run find . -type f -empty -delete -print to remove all empty files. Eventually Git started telling me it was actually doing something with the object directories:

git fsck --full

Checking object directories: 100% (256/256), done.
error: object file .git/objects/e0/cbccee33aea970f4887194047141f79a363636 is empty
fatal: loose object e0cbccee33aea970f4887194047141f79a363636 (stored in .git/objects/e0/cbccee33aea970f4887194047141f79a363636) is corrupt

Step 4: After deleting all of the empty files, I eventually came to git fsck actually running:

git fsck --full

Checking object directories: 100% (256/256), done.
error: HEAD: invalid sha1 pointer af9fc0c5939eee40f6be2ed66381d74ec2be895f
error: refs/heads/master does not point to a valid object!
error: refs/heads/master.u1conflict does not point to a valid object!
error: 0e31469d372551bb2f51a186fa32795e39f94d5c: invalid sha1 pointer in cache-tree
dangling blob 03511c9868b5dbac4ef1343956776ac508c7c2a2
missing blob 8b61d0135d3195966b443f6c73fb68466264c68e
missing blob e89896b1282fbae6cf046bf21b62dd275aaa32f4
dangling blob dd09f7f1f033632b7ef90876d6802f5b5fede79a
missing blob caab8e3d18f2b8c8947f79af7885cdeeeae192fd
missing blob e4cf65ddf80338d50ecd4abcf1caf1de3127c229

Step 5: Try git reflog. Fail because my HEAD is broken.

git reflog

fatal: bad object HEAD

Step 6: Google. Find this. Manually get the last two lines of the reflog:

tail -n 2 .git/logs/refs/heads/master

f2d4c4868ec7719317a8fce9dc18c4f2e00ede04 9f0abf890b113a287e10d56b66dbab66adc1662d Nathan VanHoudnos <[email protected]> 1347306977 -0400    commit: up to p. 24, including correcting spelling of my name
9f0abf890b113a287e10d56b66dbab66adc1662d af9fc0c5939eee40f6be2ed66381d74ec2be895f Nathan VanHoudnos <[email protected]> 1347358589 -0400    commit: fixed up to page 28

Step 7: Note that from Step 6 we learned that the HEAD is currently pointing to the very last commit. So let's try to just look at the parent commit:

git show 9f0abf890b113a287e10d56b66dbab66adc1662d

commit 9f0abf890b113a287e10d56b66dbab66adc1662d
Author: Nathan VanHoudnos <nathanvan@XXXXXX>
Date:   Mon Sep 10 15:56:17 2012 -0400

    up to p. 24, including correcting spelling of my name

diff --git a/tex/MCMC-in-IRT.tex b/tex/MCMC-in-IRT.tex
index 86e67a1..b860686 100644
--- a/tex/MCMC-in-IRT.tex
+++ b/tex/MCMC-in-IRT.tex

It worked!

Step 8: So now we need to point HEAD to 9f0abf890b113a287e10d56b66dbab66adc1662d.

git update-ref HEAD 9f0abf890b113a287e10d56b66dbab66adc1662d

Which didn't complain.

Step 9: See what fsck says:

git fsck --full

Checking object directories: 100% (256/256), done.
error: refs/heads/master.u1conflict does not point to a valid object!
error: 0e31469d372551bb2f51a186fa32795e39f94d5c: invalid sha1 pointer in cache-tree
dangling blob 03511c9868b5dbac4ef1343956776ac508c7c2a2
missing blob 8b61d0135d3195966b443f6c73fb68466264c68e
missing blob e89896b1282fbae6cf046bf21b62dd275aaa32f4
dangling blob dd09f7f1f033632b7ef90876d6802f5b5fede79a
missing blob caab8e3d18f2b8c8947f79af7885cdeeeae192fd
missing blob e4cf65ddf80338d50ecd4abcf1caf1de3127c229

Step 10: The invalid sha1 pointer in cache-tree seemed like it was from a (now outdated) index file (source). So I killed it and reset the repository.

rm .git/index
git reset

Unstaged changes after reset:
M    tex/MCMC-in-IRT.tex
M    tex/recipe-example/build-example-plots.R
M    tex/recipe-example/build-failure-plots.R

Step 11: Looking at the fsck again...

git fsck --full

Checking object directories: 100% (256/256), done.
error: refs/heads/master.u1conflict does not point to a valid object!
dangling blob 03511c9868b5dbac4ef1343956776ac508c7c2a2
dangling blob dd09f7f1f033632b7ef90876d6802f5b5fede79a

The dangling blobs are not errors. I'm not concerned with master.u1conflict, and now that it is working I don't want to touch it anymore!

Step 12: Catching up with my local edits:

git status

# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   tex/MCMC-in-IRT.tex
#    modified:   tex/recipe-example/build-example-plots.R
#    modified:   tex/recipe-example/build-failure-plots.R
#
< ... snip ... >
no changes added to commit (use "git add" and/or "git commit -a")


git commit -a -m "recovering from the git fiasco"

[master 7922876] recovering from the git fiasco
 3 files changed, 12 insertions(+), 94 deletions(-)

git add tex/sept2012_code/example-code-testing.R
git commit -a -m "adding in the example code"

[master 385c023] adding in the example code
 1 file changed, 331 insertions(+)
 create mode 100644 tex/sept2012_code/example-code-testing.R
Pires answered 11/9, 2012 at 13:53 Comment(22)
Hmm, my laptop died during a git operation (SparkleShare was trying to commit my notes as it died) and after that the repo was corrupted in this way. I followed your steps up till 6 but it seems like the last several commits were actually supposed to be part of the empty object files that I deleted? In fact the last 3 commits were basically completely broken so I guess there was nothing I could do. Luckily I don't actually need the individual commits from SparkleShare and I can just copy over the dirty file from one machine to another and merge.Flummox
@Flummox I don't think that I understand your issue well enough to comment. If it turns out that your merge solution doesn't work, feel free to post a question and I'll (and people more experienced with git than I!) will take a look at it. Good luck!Pires
..and that seems to have been fixed by manually putting the hash for HEAD in .git/refs/heads/masterDotti
Thanks, I learned a lot about some of the git functionality whilst also saving my bacon on an important commit! Coincidentally it was also because of low battery on the laptop.Scapular
In point number 6, do we need to go to /heads/master or /heads/<current_branch>? & did I said that you made my day?Acrostic
Another thanks from me! My laptop battery gave in on the road even though my desktop showed it was still half full (thanks KDE..). So after I plugged it in and tried to complete the commit I got the error that led me to this post. I followed your instructions until all the empty object files were gone. Luckily my head was not corrupted, so I could do a mixed reset and re-start the commit.Baumgartner
I encountered this error because I was trying to kill "git add"Atonement
can we give this man a Nobel Prize or something? I made a mess when my removable device got removed unexpectedly, and I'm pleasantly astonished that I was able to clean it up with this recipe. you ROCK!Adelric
Not that you should ever do this... but: $ git fsck 2>&1 | head -n1 | tee /dev/tty | cut -d' ' -f4 | xargs rm -fFlagelliform
I had a different error at step #9 - invalid reflog entry - which was covered in this seperate question (that refers back to this) #39409752Erastatus
I'll add that this can happen on Google Cloud Platform (GCP)'s Compute Engine instances when creating a snapshot of a persistent disk while it is running. In three years it has happened twice. If those repositories are responsible for any aspect of your app/site, then it will fail in bizarre ways.Bedtime
My colleague managed to corrupt something he had already pushed. We just did step 3, then git pull, git fsck, and pull had replaced it.Cierracig
If the error persists after step 11 (for git fsck --name-object) like git broken link from tree 61d013* to blob, it might be required to follow the instruction on https://mcmap.net/q/12582/-fixing-the-git-error-quot-error-unable-to-find-quot - it solved the issue for me.Izaak
May I propose a step 13 with cd ~; mkdir recovered; cd recovered; git init; git remote add damaged path/to/damaged; git fetch damaged. After these step, some of the power loss during git-rebase related damage was still causing issues. But with a cleaner repository, you lose dangling objects, which may or may not be important (they are common for the Gerrit workflow), but at least all the local branches are restored. Doing an extra step to copy the untracked and ignored files from the damaged repository to the new one is also potentially helpful. But really, thanks for this!Micrometry
I'm getting Segmentation fault (core dumped)% (39/256) at step 2. Any ideas?Isatin
I was able to resolve my case with steps 1-4 and git reflog works well! Thank you!Heterogamete
For me the solution was: git fsck, remove all empty files, tail -n 2 .git/logs/HEAD | strings, git update-ref HEAD SHA1, where SHA1 is the last SHA-1 from the output of the previous command.Bainter
I used this to fix a WSL Ubuntu installation I couldn't get to upgrade to 20.04 LTS from 18.04 LTS.Occasional
Dangerous move in step3 => find . -type f -empty -delete -print Angular project need some empty .css file to work.Unmeriting
I don't understand why but this git error happens to me regularly. No crashes, no dead battery, just completely random. The repos exist on WSL2, I wonder if that has something to do with it.Ideograph
@Nathan VanHoudnos Thanks for the explanation! at step 6 ("google: find this") link is broken, could you please re-link it or explain what it was pointing to?Initiate
@GaryLiao you need to step into .git folder and then enter command without delete option and see if it only prints the empty objects , then use with delete optionBula
B
496

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.

Butterandeggs answered 29/6, 2015 at 7:35 Comment(17)
Thanks for this, I pretty religiously push to my remote branches and your solution worked for me. I tried @mCorr's below first but after I committed the new files the repo reverted back to being corrupted. This approach solved itRegarding
as most of have a remote. This solution is really clean and sufficient.Neanderthal
Had this problem after shutting down a VM in which I was working with a git repo. This solution worked perfectly.Moxley
Thanks Martin, excellent and compact solution. Although I might put that PS first, with the warning in bold, to prevent naive users trying the same on a just-local setup. Like Giscard, this arises for me when shutting down a VM... will update if I find a more permanent solution than doing this each time.Halle
A VM crush did this to my local git repo aswell, i can confirm that this solution is 100% working in that caseVivianviviana
Thank you so much! This happened to me before and without your fix it took me awhile to get everything working again. With your fix it took me 30 seconds. Thank you.Courses
myuser@osboxes:~/IdeaProjects/myproject$ find .git/objects/ -type f -empty | xargs rm returns rm: missing operand Try 'rm --help' for more information.Landlady
my bad, there were 2 git repositories that got fetched (used intellij) and I was in the wrong oneLandlady
This did not work for me and I have a recently pushed to remote repository. Had to revert to Nathan VanHoudnos's answer which worked great.Translucid
If you are using windows, forfiles /S /M * /C "cmd /c if @isdir==FALSE if @fsize EQU 0 del @path" is equivalent to find .git/objects/ -type f -empty | xargs rmTierell
perfect! who knows what caused this!?!Erfurt
I didn't try the rest but got this on the first go and is definitely far less involved than the other solutions.Tacita
In case you receive a permission error: find .git/objects/ -type f -empty | sudo xargs rmCoax
I am left with four "dangling blob" messages. According to this, they are harmless and will go away.Disturbance
There ıs nothıng scarıer than a broken Gıt reposıtory.Photogrammetry
the first command could simply be find .git/objects/ -type f -empty -delete ;)Pasha
Worked like a charm!Noiseless
L
65

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

Lawerencelawes answered 5/5, 2015 at 2:34 Comment(2)
Man, you are the guy. Saved my day. Thank you so much!Shinberg
I got the error actually wıth git status.Photogrammetry
I
38

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.

Intersex answered 7/5, 2013 at 14:10 Comment(5)
I'm sure that the answer above is technically better, but it stopped working at step 6 and was way above my head technically. The expedient approach is git pullImpropriety
I encountered a situation where, after doing steps 1-11 of the instructions from Nathan's answer (which worked great!), I had an error that was saying refs/origin/master and refs/origin/head were not defined (or something like that). git pull fixed that. So I think both solutions work together.Mongolian
I'm aware that the filesystems used are normally journaling the meta-data only. You can turn on journaling for the data as well, but I guess its not default due to the overhead(?) That's probably why the files were empty.... and filesystems usually observe transactions per file, whereas git has multiple files being modified per transaction, thus even if the fs keeps consistency per file, if git doesn't, then I guess git will result in inconsistent states...Turoff
This answer worked for me, others are way to complicated.Pritchett
Way faster solution than the accepted answer. To everyone who has scrolled this far, follow this answer if you are in a hurry.Acrostic
U
12

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:

  1. git stash
  2. removing the Git file in error (I am not sure that it was necessary)
  3. git stash clear

I don’t know exactly what things happened, but that instructions seemed to make everything clean.

Uird answered 13/2, 2013 at 9:55 Comment(1)
I always like the simpler answers :) For Step 2 here I used the command provided in @Nathan VanHoudnos 's answer: cd .git/ && find . -type f -empty -deleteDebbiedebbra
T
11

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.

Trull answered 2/2, 2020 at 5:37 Comment(0)
D
8

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.

Domain answered 19/12, 2015 at 0:58 Comment(3)
Or don't move your current repo, 1) create a new clone git clone source_to_current_repo.git clean_repo, 2) backup the old .git folder, 3) copy over the clean .git folder.Hebrew
You are right. I already did it now. Will edit the answer later.Domain
@haoqiang: You wrote "Because I have to reboot my VM regularly, so somehow this problem happens to me very often." We experience the same. Have you made any progress on the root cause - VM settings that makes the problem happen less often?Holiness
C
6
  1. Move your app folder to make a backup, i.e., mv app_folder app_folder_bk (it is like a git stash)
  2. git clone your_repository
  3. 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.

Cormick answered 20/2, 2014 at 15:15 Comment(1)
This is what you should do when you have all local changes pushed to an upstream, or the changes are minimal, so cloning is faster than recovery.Casavant
S
5

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"
Stare answered 30/1, 2021 at 21:11 Comment(1)
Even though the fifth command threw an error (fatal: 1390b51e0218e3b2bf87df572410533c368871fe985807939acae90b65dc5d53cec4225477bd2c3a: not a valid SHA1 this fixed the issue for me!Outpour
H
4

Let's go simple... only the case you uploaded source to a remote Git repository

  1. Backup your .git folder

  2. Check your Git repository

     git fsck --full
    
  3. Remove empty object files (all)

     rm .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e
    
  4. Check your Git repository again.

     git fsck --full
    
  5. Pull your source from the remote Git repository

     git pull origin master
    
Hyperactive answered 13/9, 2017 at 9:48 Comment(0)
E
4

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
Endoblast answered 13/7, 2018 at 4:2 Comment(2)
this helped. Thank you.Suave
If the repo is clean, you just need to "cd .git/ && find . -type f -empty -delete && cd - && git pull", you may need to checkout some files in git status since some of the are empty.Alidaalidade
H
3

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 branch
  • git 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 well
  • git branch -D your-branch # Delete the branch locally ('your-branch' is the branch with problem)
  • git push origin :your-branch # Delete the branch from remote
  • git branch -m backup-branch your-branch # Rename the backup branch to have the name of the branch that had the problem
  • git push origin your-branch # Push the new branch
  • git push origin :backup-branch # Delete the backup branch from remote
Haplo answered 27/4, 2014 at 18:37 Comment(1)
That is a lot of steps!Apprehensible
S
3

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>&1 redirects the error message to standard output to be able to grep it
  • grep options used:
  • -o only returns the part of a line that actually matches
  • -E enables advanced regular expressions
  • -m 1 make 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.

Suck answered 8/9, 2017 at 13:45 Comment(0)
H
3

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.

Hotchkiss answered 7/3, 2020 at 16:17 Comment(0)
B
3

fix git object error solution Just Run below Command

find .git/objects/ -size 0 -exec rm -f {} \;
git fetch origin



Refresh index: 100% (5322/5322), done.
error: object file .git/objects/d4/f989f0e5f4613a3cbba836b3c77dfb71a7a56c is empty
error: object file .git/objects/d4/f989f0e5f4613a3cbba836b3c77dfb71a7a56c is empty
error: object file .git/objects/d4/f989f0e5f4613a3cbba836b3c77dfb71a7a56c is empty
fatal: loose object d4f989f0e5f4613a3cbba836b3c77dfb71a7a56c (stored in .git/objects/d4/f989f0e5f4613a3cbba836b3c77dfb71a7a56c) is corrupt
Beaker answered 1/12, 2022 at 13:7 Comment(1)
Worked for me, when I appended a git reset --hard && git pullPorterfield
B
2

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):

  1. Create a new empty repo on the server (or delete the old repo and create a new one in its place)
  2. Change the remote URL of your local copy to point to the remote URL of the new repo.
  3. 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.

Bloc answered 30/10, 2014 at 16:8 Comment(0)
T
2

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...

Tiruchirapalli answered 11/6, 2018 at 13:58 Comment(0)
W
2

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.
Warty answered 25/2, 2019 at 21:39 Comment(0)
R
2

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!

Radburn answered 1/5, 2020 at 11:40 Comment(0)
U
2

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.

Utoaztecan answered 26/5, 2021 at 6:12 Comment(0)
R
2

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.

Radiography answered 10/10, 2021 at 5:29 Comment(1)
i got this error in my android studio and i don't know how to check git status, but i just rebuild project in android studio and the error is gone..Thank you!!Kilauea
R
1

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.

Rossini answered 10/7, 2016 at 5:43 Comment(0)
B
1

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.

Bendix answered 14/3, 2018 at 2:24 Comment(0)
C
1

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.

Cocainize answered 12/6, 2018 at 7:22 Comment(0)
A
1

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
Acceptant answered 1/11, 2019 at 13:17 Comment(0)
S
1

In my case I have a remote Copy I followed this simple steps and its solved :

1 - clone your remote repo

git clone YOUR_REPO

2 - Open the cloned repo copy the .git file

3 - Replace the .git file in the corrupted file with .git file of cloned_repo

4 - Restart your editor the problem is gone

Stinkhorn answered 14/3, 2023 at 11:13 Comment(0)
T
0

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.

Thigpen answered 1/5, 2013 at 4:41 Comment(2)
rm -rf * might have very bad consequences. Did you really mean that ?Ingrowth
@Ingrowth hence the copy to the backup first. I guess the force isn't necessary.Thigpen
K
0

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

Kizer answered 1/7, 2014 at 14:40 Comment(0)
S
0

If you have an old backup and are in a hurry:

Make a new backup of your current, Git-broken, project path.

  1. move your .git to trash (never delete)
  2. copy .git from the old backup
  3. git pull (will create merge conflicts)
  4. move all your sources (everything you put in Git) to trash: ./src (never delete)
  5. Copy all your sources (everything you put in Git) from the new backup
  6. accept all "merges" at git gui, push and... clap your hands!
Skilken answered 23/9, 2016 at 4:15 Comment(0)
R
-2

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
Rattoon answered 6/5, 2022 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.