Git 提交停止工作-生成树时出错

我无法做出改变:

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

我试过了:

$ git fsck | grep 13da
missing blob 13da9eeff5a9150cf2135aaed4d2e337f97b8114

还有:

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

还有:

$ git fsck | grep 13da
missing blob 13da9eeff5a9150cf2135aaed4d2e337f97b8114

但什么都帮不上忙。我应该删除文件,提交和重新引入回?我愿意失去一点点历史,如果它带来了 git 承诺回来。

65417 次浏览

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

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

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>

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

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.

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

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.

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.

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.

In my case, I solved it by:

git reset --mixed

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

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

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.

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

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!

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

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