44

Only for one file, I am getting the following error:

error: unable to write sha1 filename /opt/www/.git/objects/3f/ce3587c54a8be14c69b08c6b01f94949b11b47: Permission denied

error: wp/wp-admin/css/theme-install.dev.css: failed to insert into database
fatal: unable to index file wp/wp-admin/css/theme-install.dev.css

I checked my permissions on the file in question, the .git objects directory in question, and .git itself. I can add any other files except for this one. I could stat/r/w/touch the file, and touching did not help. The permissions are all correct.

Is this some crazy bug?

3
  • 1
    Can you touch new files in .../objects/3f, or if not, what is its mode? Does .../objects/3f/ce3... already exist, and if so, what is its mode? Could the partition where /opt lives be full?
    – Greg Bacon
    Jun 19, 2011 at 10:50
  • Touching is fine, and the file does not exist.
    – h4xnoodle
    Jun 21, 2011 at 4:04
  • also happened to me in this case : having a file named "imafile" > removing this file > create a directory named "imafile" > put files in = can't commit because "Unable to find index"
    – Flozza
    Feb 15, 2018 at 10:59

9 Answers 9

70

If you are using visual studio or something similar that is generating the mdf file, simply close VS & retry your git command again. This time it should work.

To save constantly closing & reopening, you should add references into .gitignore file in the project root. For example, if it is a database causing the issue, add the following:

# SQL Server files
*.mdf
*.ldf
4
  • This worked for me. I was having the problem by trying to commit a Unity project with VS open. Closed it and the error went away.
    – nktsamba
    Oct 2, 2018 at 12:58
  • This is extremely frustrating, because it means I cannot use Team Explorer to manage my code. I have to make the required changes in Visual Studioo, close the application, commit my changes, open up the application and resume working. There must be a way around this.
    – devklick
    Jul 20, 2019 at 14:03
  • Klicker, put whatever file is causing it in the ignore file like I suggested with *.mdf etc above. That will do the trick.
    – ShrapNull
    Jul 22, 2019 at 14:17
  • Worked for me. Just closed VS and ran the git add command and was able to go through with committing. Oct 28, 2019 at 18:54
19

Looking at the Git source code (sha1_file.c, function move_temp_to_file()) it looks like Git is failing to rename a temporary file named /opt/www/.git/objects/3f/tmp_obj_XXXXXX (where XXXXXX is six random characters) to /opt/www/.git/objects/3f/ce3587c54a8be14c69b08c6b01f94949b11b47. This can happen if you don't have permission to delete files in /opt/www/.git/objects/3f.

Some things to try:

  • If multiple users accessing the Git repository, you may need to run something like git config core.sharedRepository 0664 (see git help config for details) to ensure that newly created directories and files have proper permissions for all users of the repository.
  • Try running rm -f /opt/www/.git/objects/3f/tmp_obj_* and see if that makes the problem go away.
  • See if you can reproduce the problem outside of Git by doing the following:

    mkdir -p /opt/www/.git/objects/3f
    cd /opt/www/.git/objects/3f
    rm -f tmp_obj_* ce3587c54a8be14c69b08c6b01f94949b11b47
    echo "testing" >tmp_obj_abcdefg
    mv tmp_obj_abcdef ce3587c54a8be14c69b08c6b01f94949b11b47
    rm -f tmp_obj_abcdefg
    

    Be sure to run the above commands the same user that experienced the error.

  • Try recursively chowning and chmoding the objects directory.
1
  • For an unknown reason, I indeed had some objects own by root.
    – Gra
    Dec 19, 2016 at 15:20
18

Just close the Visual Studio (or Unity) and try to add those files again.

3
  • 2
    I've closed the nodeJs server where my angularJs app was running and the index was unlocked.
    – Radu Linu
    Oct 19, 2018 at 9:40
  • that works, but suppose I have 4 projects open at one point of time, I can't close all four of them just to add files to git. Isn't there any other solution that doesn't require closing it ?
    – mrid
    Feb 27, 2019 at 7:27
  • Yea, Its works when u do lock it with Visual Studio
    – Yahor10
    Feb 22, 2021 at 12:21
10

You don't have permission to write in /opt/www/.git/objects/3f.

The quickest solution is to use the sudo command to execute your command with root privileges.

sudo <Your git command>

Solved it for me.

1
  • 1
    worked for me... I think what caused it in my case was aborting halfway through a previous commit Feb 20, 2015 at 1:32
5

Something has gone awry in your git repository likely caused by an external process creating a file or directory which is owned by a user other than the current user.

This error is common when using Docker, and a service in your docker-compose.yml file has a locally mounted volume which was created using a different user to that of the local machine user.

If this is the first time that this error has occurred, execute the below on your working directory to change the ownership of files and folders back to the logged in user:

sudo chown -R ${USER}:${USER} .

If this is not the first time you have encountered this issue, i.e. you have already committed and pushed files and folders that are owned by another user, then the above alone will not rectify the situation, as well as executing the above command - you will need to carry out the following instructions.

By far, the quickest fix is to execute the following from your root project directory which holds your git repository:

sudo chown -R ${USER}:${USER} .git/objects

To test that everything is fixed, execute the following:

git add .

Quickly followed by executing:

git status

You will see that everything has been added to the git repository without any need to test / fiddle around with anything any further.

1
  • This is the correct solution, if you've used sudo with git commands some permissions will be changed in the .git folder, which creates this error Oct 27, 2019 at 6:41
2
error: open("3/BasicMVVM/.vs/BasicMVVM/v16/Server/sqlite3/db.lock"): Permission denied

error: unable to index file 3/BasicMVVM/.vs/BasicMVVM/v16/Server/sqlite3/db.lock fatal: adding files failed

if you have such error and you are working on visual studio.first you close the visual studio. git bash and write "git add * " with out inverted commas and on the suitable place it will work for my self

1
  • 1
    your answer is not more clear. Please check is it helpful Nov 15, 2019 at 8:23
0

I was having this problem on my bare origin repository when the permissions were root:git 770, apparently I had to change it to 771, even though my user is in the git group. I suspect perhaps git is perhaps either not acl-aware, or not so compatible with secondary groups, as the git group was one of my secondary groups in this case.

0

I stopped running my Driver.main() method in intelliJ (for java programming) and was able to add all my files to staging area with git add . command. Also try restarting intelliJ or whatever tool you are using for writing code as a second option.

0

This happens to me when I was trying to push a doc file to GIT hub when it was opened. After closing it and tried the same and successfully pushed.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 26, 2022 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.