104

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.

gorn
  • 5,042
  • 7
  • 31
  • 46

18 Answers18

171

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.

mc_kaiser
  • 717
  • 8
  • 18
Alex Nikulin
  • 8,194
  • 4
  • 35
  • 37
23

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
Maic López Sáenz
  • 10,385
  • 4
  • 44
  • 57
  • 3
    in my case, the file referenced is no longer in the .git objects folder! >. – mix3d Jun 24 '15 at 21:50
  • Exactly: you want to look for it in other clones of the repo, where it may exist – fuzzyTew Jan 27 '17 at 16:06
  • Had the same problem due to I/O error. I wasn't able to replace the file but I removed the whole folder (in the example given it would be `/local/.git/objects/13/`) and I copied a copy of it from my remote repo. It did the trick and all is working back again! thanks – Simon C. Feb 08 '17 at 08:56
  • 8
    The answer below by @AlexNikulin is an even better solution: `git hash-object -w spec/routing/splits_routing_spec.rb` – sagannotcarl Apr 12 '18 at 21:24
  • 7
    I downvoted this answer because the `git hash-object -w` worked better (for me). – Nikos Alexandris Sep 11 '18 at 19:33
  • This was the most useful answer, and should have been at the top. I do not have a copy of my repository to recover pieces from. – Charley Ramm Feb 05 '19 at 13:09
  • in my case it helped for 1 push. Then @Alex's answer solved problem – Vit May 07 '20 at 07:12
17

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

Tom Macdonald
  • 6,433
  • 7
  • 39
  • 59
  • 11
    $ git reset --hard gives fatal: unable to read tree 2287adf1b2a974541ce5806abc23dbd79814d666 – gorn Jan 21 '13 at 22:44
  • 2
    https://git.wiki.kernel.org/index.php/GitFaq#How_to_fix_a_broken_repository.3F might help then. – Tom Macdonald Jan 22 '13 at 11:31
  • 3
    +1 reset --hard worked for me. my issue is Google Drive syncing my local code across multiple machines - you have to remember to stop and restart it to let it sync up. I think google drive must purposely ignore monitoring .git directories cause of the mass changes. – eduncan911 Mar 30 '14 at 06:26
12

A simple trick mentioned in this Medium article solved my case where I ran into similar 'invalid object' & 'error building tree' issues. The solution is pretty straightforward:

git hash-object -w <file-name-which-is-creating-problem>

After that, git will make a Sha1 for the file whose hashes were not matching and its repository will be fixed.

Now I can use git add * and git commit -m without any issues. That’s it.

Note: Create a copy (aka backup) incase if you are not sure what you are doing.

Pranav
  • 263
  • 2
  • 11
10

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>
Archias
  • 101
  • 1
  • 4
  • 3
    This doesnt work if the file is now missing from your disk drive; because there is no physical file to reset, you get a `fatal: ambiguous argument 'path/to/file.ext': unknown revision or path not in the working tree.` message – mix3d Jun 24 '15 at 21:49
7

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

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
latvian
  • 3,161
  • 9
  • 33
  • 62
  • *sudo* is a bit too permissive, but it was indeed a permission problems. Fixed it by restoring ownership of the whole folder to the proper user. – Nicolas Modrzyk Mar 01 '16 at 05:04
7

In my case, I solved it by:

git reset --mixed
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Sathibabu P
  • 649
  • 1
  • 7
  • 15
  • error: unable to read sha1 file of test.ts (52e55168ebc6e4073b5935bced17f1cde7c50fad) fatal: Could not reset index file to revision 'HEAD'. – titusfx Dec 12 '21 at 18:41
3

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.
YiDing
  • 31
  • 2
  • How do I find out which files are related? `git gc` doesn't give me any more info. – Tim Kuipers Oct 05 '21 at 08:08
  • I have Dropbox and it messed up some internal .git file somewhere, but I don't know when and it wasn't related to any already committed file, but to my uncommitted changes. I just had to change my text/code a little bit, so that the hash would be different and the error wouldn't be triggered anymore. – Tim Kuipers Oct 05 '21 at 08:11
2

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

Abd Rmdn
  • 480
  • 4
  • 11
  • 1
    worked for me -- thanks abd, emptied the folder, commited, then replaced the files and commited – David Jun 13 '16 at 03:01
1
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"

A P
  • 2,131
  • 2
  • 24
  • 36
0

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.

0

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.

Jughead
  • 799
  • 7
  • 7
0

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

0

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

Marcelo Cesar
  • 89
  • 1
  • 4
0

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.

Thomas
  • 51
  • 1
  • 6
0

If your repository is synced using OneDrive and none of the above solutions work (git commands result in more errors), it might be a bug with OneDrive app. Suggested solution (which worked for me) is to scan and fix your hard drive:

  1. Search Command Prompt in the Start menu
  2. Right click on Command Prompt > Run as administrator
  3. Enter command chkdsk \r \f
  4. If chkdsk is unable to scan and fix the drive immediately, it'll ask to perform the operation during next restart - confirm that you want to scan the drive
0

I tried the reset command, nothing worked for me except saving my new changes aside, deleting everything re-clone(it dosn't matter if it's in new area or same, I used the same) and re-copy

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 10 '22 at 14:28
-2
error: invalid object 100644 d5b87de7ffab13b0f9669abceae5c5193ac950ec for 'angular.json'
error: invalid object 100644 d5b87de7ffab13b0f9669abceae5c5193ac950ec for 'angular.json'
error: Error building trees

I faced this issue. Here’s how I fixed it.

  • I cloned the repository again (to get a “fresh” copy)
  • I copied the entire .git folder from the cloned repository
  • Deleted the .git folder from the original repository with the error
  • Pasted the new .git folder in the original repository

It works!

I hope this will be helpful to someone else!

Zearin
  • 1,474
  • 2
  • 17
  • 36
subin s
  • 47
  • 1
  • 3