git:无法索引文件 - 权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6400755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git: Unable to index file - permission denied
提问by h4xnoodle
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 .gitobjects directory in question, and .gititself. I can add any other files except for this one. I could stat/r/w/touch
the file, and touching did not help. The permissionsare all correct.
我检查了我对相关文件、相关.git对象目录和.git本身的权限。除了这个文件,我可以添加任何其他文件。我可以stat/r/w/touch
文件,触摸没有帮助。该权限是正确的。
Is this some crazy bug?
这是一些疯狂的错误吗?
采纳答案by Richard Hansen
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
.
查看 Git 源代码(sha1_file.c
, function move_temp_to_file()
),看起来 Git 未能将名为/opt/www/.git/objects/3f/tmp_obj_XXXXXX
(其中XXXXXX
是六个随机字符)的临时文件重命名为/opt/www/.git/objects/3f/ce3587c54a8be14c69b08c6b01f94949b11b47
. 如果您无权删除/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
(seegit 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
chown
ing andchmod
ing the objects directory.
- 如果有多个用户访问 Git 存储库,您可能需要运行类似
git config core.sharedRepository 0664
(查看git help config
详细信息)以确保新创建的目录和文件对存储库的所有用户具有适当的权限。 - 尝试运行
rm -f /opt/www/.git/objects/3f/tmp_obj_*
,看看这是否会使问题消失。 通过执行以下操作,看看您是否可以在 Git 之外重现该问题:
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
请务必以遇到错误的同一用户运行上述命令。
- 尝试递归地
chown
ing 和chmod
ing 对象目录。
回答by ShrapNull
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.
如果您正在使用 Visual Studio 或类似的东西来生成 mdf 文件,只需关闭 VS 并再次重试您的 git 命令。这次应该可以了。
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:
为了保存不断关闭和重新打开,您应该将引用添加到项目根目录中的 .gitignore 文件中。例如,如果是导致问题的数据库,请添加以下内容:
# SQL Server files
*.mdf
*.ldf
回答by Yar
Just close the Visual Studio (or Unity) and try to add those files again.
只需关闭 Visual Studio(或 Unity)并尝试再次添加这些文件。
回答by william.eyidi
You don't have permission to write in /opt/www/.git/objects/3f
.
您无权写入/opt/www/.git/objects/3f
.
The quickest solution is to use the sudo
command to execute your command with root privileges.
最快的解决方案是使用该sudo
命令以 root 权限执行您的命令。
sudo <Your git command>
sudo <Your git command>
Solved it for me.
为我解决了。
回答by Matt G
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.
您的 git 存储库中出现问题可能是由外部进程创建的文件或目录引起的,该文件或目录归当前用户以外的用户所有。
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.
此错误在使用 Docker 时很常见,并且 docker-compose.yml 文件中的服务具有本地安装的卷,该卷是使用与本地机器用户不同的用户创建的。
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:
到目前为止,最快的解决方法是从包含 git 存储库的根项目目录中执行以下操作:
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.
您将看到所有内容都已添加到 git 存储库中,而无需进一步测试/摆弄任何东西。
回答by pamal Sahan
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
错误:无法索引文件 3/BasicMVVM/.vs/BasicMVVM/v16/Server/sqlite3/db.lock 致命:添加文件失败
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
如果您有这样的错误并且您正在使用visual studio.first 关闭visual studio。git bash 并写下“git add *”,不带引号,在合适的地方它会为我自己工作
回答by RedScourge
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.
当权限为 root:git 770 时,我在裸源存储库上遇到了这个问题,显然我不得不将其更改为 771,即使我的用户在 git 组中。我怀疑 git 可能不知道 acl,或者与次要组不太兼容,因为在这种情况下 git 组是我的次要组之一。