git error: invalid object Error building trees

The solution is simple and one-line command!

Panjeh
3 min readSep 2, 2018

Today I faced this error:

git error: invalid object Error building trees

Reason:

Because I accidentally disconnected the hard drive from my laptop. I also faced some other problems in my other git repositories and after a while I could fix most of those issues. You can read about one of them (Git status fatal: bad object HEAD) here.

Problem:

When I did:

git status 

in a git repository, I saw there was one untracked file which I had created already but had not been committed. Everything was okay until I started to add the file to the repository with:

git add . 

and then

git commit -m “message”

Suddenly I saw this error after git commit :

make_file.txt in the Collection directory (as shown in the above picture) is one of the old files that I’ve recently committed it! So there is a problem in the .git directory for this file: make_file.txt

Solution:

As you see above in my case we see (file and its directory) in the terminal:

‘Collection/make-file.txt’

So grab it and put it in the next command without quotation mark. Like what I did: (Certainly you should put your specific file name and its directory)

git hash-object -w Collection/make-file.txt

After that, git will make a Sha1 for the make-file.txt and it’s repository will be fixed.

So the problem is solved! Then we could go forward and git add . and git commit like a normal condition. That’s it.

If you want to run all the git commands just in one command read this.

read more: here

I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.

https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs

--

--

Panjeh
Panjeh

Written by Panjeh

Posting about Python and Laravel

Responses (4)