6

I was attempting to perform a git commit using Git Gui. I staged a few files, and then my computer suddenly shut off due to low battery. I plugged it in, turned it back on, and committed the files I wanted to. Then I ran git push, like I've done a million times before with no issues, and this time I got the following error:

Counting objects: 8, done.
error: object file .git/objects/b5/60c934f6bad40f4f246973afc0139ed91a2d32 is empty
Compressing objects: 100% (4/4), done.
error: object file .git/objects/b5/60c934f6bad40f4f246973afc0139ed91a2d32 is empty
fatal: loose object b560c934f6bad40f4f246973afc0139ed91a2d32 (stored in .git/objects/b5/60c934f6bad40f4f246973afc0139ed91a2d32) is corrupt
error: failed to push some refs to 'git@bitbucket.org:joemorano/app.git'

Could this have been caused by the computer shutting off before I could perform the first commit?

Right before all this happened, I apparently corrupted the production version of my app by running bundle install as root on my server, and now everything on the server is messed up, but I don't see how that could affect the local version. I never did git pull or anything like that.

Anyone encounter this error before?

Scott Swezey
  • 2,147
  • 2
  • 18
  • 28
Joe Morano
  • 1,715
  • 10
  • 50
  • 114

4 Answers4

10

Step 1: Make a backup of .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.):

cp -a .git .git-old

Step 2: Run

git fsck --full

you will get this error message

For example: error: object file .git/objects/0A/dsdadadadaaeer4r3434343434334f is empty

Step 3: Remove the above empty file this is in your .git/objects/ folder. Continue deleting the empty files.

Step 4: After deleting all of the empty files, now run

git fsck --full

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

Step 6: Manually get the reflog:

git log origin/master..HEAD

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 commit-id 

Step 8: So now we need to point HEAD to commit-id

git update-ref HEAD commit-id

Hope this will work for you.

Akshay Borade
  • 2,442
  • 12
  • 26
  • Where do I find the empty files in Step 3? – Joe Morano May 21 '16 at 21:18
  • @JoeMorano : I have updated my answer. Please have a look. – Akshay Borade May 23 '16 at 09:24
  • @JoeMorano: Let me know if you are getting any issue. – Akshay Borade May 23 '16 at 09:25
  • 3
    Thanks, worked. :) I did `git update-ref HEAD origin/thebranch` after deleting the corrupted object. There were a lot, I was a bit scared there would be hundreds, or all of them, and there is no visibility (just tell me all the empty ones ffs! :p). – Yann Dìnendal Oct 26 '16 at 15:43
  • tks, What can I do if I have hundreds of empty files? Do I have to delete them one by one? – Luca Monno Sep 05 '17 at 12:48
  • This solution worked for me, i just set HEAD to the desired branch – Bernie Dec 19 '17 at 10:24
  • @Bernie Okay Thanks! :) – Akshay Borade Jan 09 '18 at 10:35
  • This was helpful but didn't get me all the way there. I was missing a blob object (empty file) in a recent commit. I removed the empty file, but git fsck kept complaining about it, even after I'd committed onto a new branch from before the problem commit, and done variations of `git gc` and `git prune` (including more forceful versions). Doing a local clone of the repo and then doing `git prune` inside of _that_ got me to where `git fsck` would report all was ok again. FWIW. – lindes Dec 12 '21 at 02:50
6
... failed to push some refs to 'git@bitbucket.org:joemorano/app.git'

Could this have been caused by the computer shutting off before I could perform the first commit?

Definitely, yes.

Anyone encounter this error before?

Repair corrupted git repository

See also: How to fix corrupted git repository?

In this case, since you (presumably) have a non-corrupt backup on bitbucket, I'd start by cloning that to a fresh (good) clone, then see if I could recover any intermediate commits and files from the existing (bad) clone and/or its work-tree, putting them into the new (good) clone. Once you have recovered whatever is recoverable from the bad clone, you can abandon or delete it. (Depending on your computer and its file system checking and repair tools, you might want to run those as well, perhaps before doing anything else at all.)

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • Couldn't I just revert to a previous commit? – Joe Morano May 19 '16 at 03:27
  • Git is thin (well, fairly thick) wrapper around an object-database file system. The errors you are seeing are telling you that the database is corrupt. Picking a previous commit leaves the database corrupt, which is not a good idea, even if it temporarily gives you a useful work tree. It's like sticking a band-aid on after getting a deep stab wound: fine for getting you to a real doctor, but not a cure, and any infection could spread. (This analogy falls apart if you push on it too hard but the general idea is to avoid leaving ticking time bombs ticking.) – torek May 19 '16 at 03:40
  • Oh, it's the database that's the problem? In that case, what if I erase and repopulate the database? – Joe Morano May 19 '16 at 06:46
  • That's what re-cloning would do. (If you need to preserve your work tree, make the clone elsewhere, then `mv .git` out of the work-tree and `mv` the corrected `.git` to slide it into place, for instance.) – torek May 19 '16 at 17:34
  • So simply deleting all the records of all my models should have the same effect as your solution? – Joe Morano May 19 '16 at 18:39
  • I don't know what you mean by "records of models", so I imagine this is a RoR thing, and I suspect it is unrelated to *Git's* database. – torek May 19 '16 at 21:29
  • Oh, right. It's a postgreSQL database using a RoR framework. I'm afraid I don't know anything about Git's database. Could what you're talking about still be the problem? – Joe Morano May 20 '16 at 05:24
  • 1
    Yes, that's why I'm telling you to look at the two other questions (you asked "Anyone encounter this error before?", these are two such "anyone"s) and their answers, and to consider a slightly easier alternative as long as you have a good backup copy of your repository (i.e., Git database). – torek May 20 '16 at 09:09
  • Thanks for your help! – Joe Morano May 20 '16 at 19:15
1

It is because of corrupted head object files residing in local .git folder.

Following the below commands enabled to view previous refs and track back the commit.

git symbolic-ref HEAD refs/remotes/origin/master // resetting to branch refs inside .git folder manually
git status // now we can use git log to find the latest commit. which will be clean to master.
git reflog  // to track back lost commit.
git reset HEAD@{5} // resetting hard to refs with the changes.

It is found that sometimes the diff changes are present by resetting with the commit, where changes are not present in the physically before.

So, we did a soft reset with the commit ID and used difftool to manually set the changes that were lost.

Vimalraj
  • 6,153
  • 1
  • 7
  • 6
0

**

A SIMPLE SOLUTION after struggling a bit

**

I faced the similar issue.

> $ git push error: object file
> .git/objects/65/05c1876d9c4f89b9f65949052af3495ae039cd is empty error:
> object file .git/objects/65/05c1876d9c4f89b9f65949052af3495ae039cd is
> empty error: object file
> .git/objects/65/05c1876d9c4f89b9f65949052af3495ae039cd is empty fatal:
> loose object 6505c1876d9c4f89b9f65949052af3495ae039cd (stored in
> .git/objects/65/05c1876d9c4f89b9f65949052af3495ae039cd) is corrupt
> error: remote unpack failed: eof before pack header was fully read

Then I just removed that empty object file

$ rm .git/objects/65/05c1876d9c4f89b9f65949052af3495ae039cd

Boom I can do git push now.

$ git push
Enumerating objects: 41, done.
Counting objects: 100% (41/41), done.
Delta compression using up to 4 threads
Compressing objects: 100% (21/21), done.
Writing objects: 100% (22/22), 3.82 KiB | 1.91 MiB/s, done.
Total 22 (delta 15), reused 0 (delta 0)
remote:
remote: View pull request for feature/*BranchName*
> error: failed to push some refs to 'ssh://git@stash.xxx.com

I tried $git gc though which made me remove some file -- do not do this