If you’ve had multiple files with varying owners in a git repo, and have used sudo git <anything>, then the owners of certain folders in .git/objects may also have root ownership.

$ git add .
error: insufficient permission for adding an object to repository database .git/objects
error: <file> failed to insert into database
error: failed to index file '<file>'
fatal: adding files failed

To fix this, normalize the ownership and permissions of .git/objects. The order of these commands is particular; all files and folders under that dir should have the user / group IDs set, but all the folders should have drwxr-xr-x permissions and all the files should have -r--r--r-- permissions.

$ sudo chown -R misha:misha .git/objects
$ sudo chown 755 .git/objects/
$ sudo chown 444 .git/objects/**/*
$ sudo chown 755 .git/objects/*

(if you are not misha, substitute misha with your username)

git