git commit stopped working - Error building trees
GitGit 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:
- You can try to undo recent synchronization operations.
- 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:
- remove the remote branches at all by
$ git remote rm origin
- add the remote back again:
$ git remote add origin <the-remote-url>
- fetch the remote again:
$ git fetch origin
- 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 :
- Copy the uncommit files.
- Then use
$ git reflog -1
- use
$ git reset --hard xxxxxx
(xxxxx your last commit head) - 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"