git error: invalid object Error building trees

The solution is simple and one-line command!

Today I faced this error:

git error: invalid object Error building trees


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


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 :

Image for post
Image for post

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


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


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 more:

Written by

Web geek, Self-taught full-stack web developer, Learning Python, Laravel, Vuejs, UX/UI design, Nuclear Physicist PhD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store