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.

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐