git commit stopped working - Error building trees

Git

Git Problem Overview


I can not commit a change:

$ git commit
error: invalid object 100644 13da9eeff5a9150cf2135aaed4d2e337f97b8114 for 'spec/routing/splits_routing_spec.rb'
error: Error building trees

I tried so far:

$ git fsck | grep 13da
missing blob 13da9eeff5a9150cf2135aaed4d2e337f97b8114

and also:

$ git prune
error: Could not read 1394dce6fd1ad15a70b2f2623509082007dc5b6c
fatal: bad tree object 1394dce6fd1ad15a70b2f2623509082007dc5b6c

and also:

$ git fsck | grep 13da
missing blob 13da9eeff5a9150cf2135aaed4d2e337f97b8114

but nothing helped. Should I delete the file, commit and reintroduce back? I am willing to lose little bit of history if it brings git commit back.

Git Solutions


Solution 1 - Git

This error means that you have a file with hash 13da9eeff5a9150cf2135aaed4d2e337f97b8114, and this hash is not present in .git/objects/../, or it's empty. When this error occurred, I only had this hash in the error, without the file path. Then I tried to do git gc --auto and git reset --hard. After one of these commands (these commands did not fix my problem), I got the path of the file that triggers the error.

You just need to generate the object hash:

git hash-object -w spec/routing/splits_routing_spec.rb

For more information see documentation. In the documentation, there is an additional way of repairing this error.

P.S. This was the only way that was helpful for me.

Solution 2 - Git

You might have a corrupted object in your git repository.

If you have a remote, or other clones of this repository, you could grab from there the problematic file and just replace it on your local repo.

The file you want would be in:

/repo/.git/objects/13/da9eeff5a9150cf2135aaed4d2e337f97b8114

Solution 3 - Git

git reset --hard should bring your repository back to normal, but you will lose uncommitted changes.

Solution 4 - Git

If the problematic file is being added by your change you can just remove it from the index and add it again:

git reset <file> 
git add <file>

Solution 5 - Git

For me it was just permissions issue. When I run with sudo, it worked. perhaps something to do with mac environment

Solution 6 - Git

In my case, I solved it by:

git reset --mixed

Solution 7 - Git

This can be caused by some third-party synchronization APP such as Dropbox and Jianguoyun. There might be two ways based on my experience:

  1. You can try to undo recent synchronization operations.
  2. Remove the related files from the folder, commit, and then move back the files.

Solution 8 - Git

Easy work around solution, if you're not really concerned on the track of the file, you can duplicate the file and remove the original, commit first the deletion and addition, then rename to original again.

Git should build back again normally

Solution 9 - Git

In my case, it is the file in remote branch that is broken. I solved it by:

  1. remove the remote branches at all by $ git remote rm origin
  2. add the remote back again: $ git remote add origin <the-remote-url>
  3. fetch the remote again: $ git fetch origin
  4. reset-hard to the desired branch on origin (say, develop): $ git reset --hard origin/develop

Then everything goes back to normal.

Solution 10 - Git

In my case, this was due to a different version of git. I had been using my repository through the official Windows port of git and started using the MinGW port with the same version number.

I started to encounter this issue when trying to commit with MinGW git. Switching back to windows Git solved the issue.

Solution 11 - Git

it's as simple as cloning from the remote repo to a new folder, deleting all the files on this new folder keeping the .git one. And then copying all the files from the old folder to the new cloned folder without copying the .git folder..

Solution 12 - Git

well I faced this issue also, what i did is: copy changed folder or files to another project in VSCode and delete that repository and clone again and pass that file(s) or folder(s) back again. looks like long way but i think it is better to make sure u won't lose your files that u didn't commit yet

Solution 13 - Git

The easiest way to tackle this issue is :

  1. Copy the uncommit files.
  2. Then use $ git reflog -1
  3. use $ git reset --hard xxxxxx (xxxxx your last commit head)
  4. Then paste your files again.

It's worked for me. No need to clone the repo or remove the remote.

Solution 14 - Git

git status 

and then it shows you which files were modified/causing the issue... then you can either add them via git add "filename" - without the quotes or remove via git rm "filename"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongornView Question on Stackoverflow
Solution 1 - GitAlex NikulinView Answer on Stackoverflow
Solution 2 - GitMaic López SáenzView Answer on Stackoverflow
Solution 3 - GitTom MacdonaldView Answer on Stackoverflow
Solution 4 - GitArchiasView Answer on Stackoverflow
Solution 5 - GitlatvianView Answer on Stackoverflow
Solution 6 - GitSathibabu PView Answer on Stackoverflow
Solution 7 - GitYiDingView Answer on Stackoverflow
Solution 8 - GitAbd RmdnView Answer on Stackoverflow
Solution 9 - GitYang-Hsing LinView Answer on Stackoverflow
Solution 10 - GitJugheadView Answer on Stackoverflow
Solution 11 - Gityoussef ghazalyView Answer on Stackoverflow
Solution 12 - GitMarcelo CesarView Answer on Stackoverflow
Solution 13 - GitUzairView Answer on Stackoverflow
Solution 14 - GitA ParsView Answer on Stackoverflow