git提交时dubious ownership in repository, /.git/index.lock‘:权限不够和remote: HTTP Basic: Access denied的解决
本文总结了在Linux环境下首次提交Git代码时遇到的三个典型问题及解决方法:1)检测到可疑仓库所有权,通过添加安全目录解决;2)无法创建index.lock文件,通过修改文件所有权和权限解决;3)HTTP基本认证失败,通过配置Personal Access Token鉴权。这些方案依次解决了从权限验证到代码推送全流程中的关键障碍,特别是针对企业环境下的多用户协作场景提供了实用解决方案。
·
新环境git提交时dubious ownership in repository, /.git/index.lock':权限不够和remote: HTTP Basic: Access denied的解决
Background
在某外企工作时,从一台部门的非办公电脑上,用同事的Linux账户,第一次提交代码时遇到问题。
问题描述
1. git status - fatal: detected dubious ownership in repository
查看待提交文件时遇到
$ git status
fatal: detected dubious ownership in repository at '/home/user/Projects/ProjectName'
To add an exception for this directory, call:
git config --global --add safe.directory /home/user/Projects/ProjectName
2. git add - fatal: 不能创建.git/index.lock’:权限不够
解决上述问题后,在add时遇到
$ git add .
fatal: 不能创建 '/home/user/Projects/ProjectName/.git/index.lock':权限不够
$ ls -l .git/index.lock
ls: 无法访问 '.git/index.lock': 没有那个文件或目录
3. git push - fatal: ‘https://…/project.git/’ 鉴权失败
解决上述问题后,在push时遇到
$ git push origin linux
remote: HTTP Basic: Access denied. If a password was provided for Git authentication, the password was incorrect or you're required to use a token instead of a password. If a token was provided, it was either incorrect, expired, or improperly scoped. See https://code.exaas.bxxxh.com/help/topics/git/troubleshooting_git.md#error-on-git-fetch-http-basic-access-denied
fatal: 'https://code.exaas.bxxxh.com/project.git/' 鉴权失败
解决方法
1. 解决git status - fatal: detected dubious ownership in repository
运行
$ git config --global --add safe.directory /home/zhoumeng/CLionProjects/HeatmapCalculateForMEA
之所以出现此问题是git对我当前用户是否拥有这个项目的权限存疑,先用上述git config --global --add safe.directory将目录加到可信任目录下。但这不能完全解决问题,项目在不同用户下所有权导致git出现的相应问题在下一步解决。
2. 解决git add - fatal: 不能创建.git/index.lock’:权限不够
遇到
$ git add .
fatal: 不能创建 '/home/user/Projects/ProjectName/.git/index.lock':权限不够
问题时,运行
$ sudo chown -R username:username~/home/user/Projects/ProjectName/.git
username改为当前使用的用户名。将文件所有权给自己。再运行下面命令让文件的所有者能够读取和写入指定的文件及其子目录中的所有文件。
$ chmod -R u+rw/home/user/Projects/ProjectName/.git
3. 解决git push - fatal: ‘https://…/project.git/’ 鉴权失败
先在项目的github/gitlab上的Access菜单中生成Token,勾选必要的权限,如write_repository 等。
运行下列命令配置 Git 使用 PAT(Personal Access Token)
git remote set-url origin https://yourusername:yourPAT@code.exaas.bxxxh.com/edge/apac/china/rbcd/k8s/heatmap-calculate-for-mea.git
将 yourusername 替换为你的办公电脑用户名/工号,yourPAT 替换为生成的 token。
即可完成鉴权,成功git push.
更多推荐
所有评论(0)