尝试 git pull 时出现错误:无法打开 .git/FETCH_HEAD:权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13195814/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:51:48  来源:igfitidea点击:

Trying to git pull with error: cannot open .git/FETCH_HEAD: Permission denied

gitgithubpull

提问by Arthur Yakovlev

Help me please, I am trying to run this in my terminal:

请帮助我,我正在尝试在我的终端中运行它:

asgard@asgard-A7N8X2-0:~/CollegePortal$ git pull
error: cannot open .git/FETCH_HEAD: Permission denied

Then I try this one

然后我试试这个

asgard@asgard-A7N8X2-0:~/CollegePortal$ sudo git pull
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Help me, I don't understand this problem.

帮帮我,我不明白这个问题。

采纳答案by abyx

It seems like the first one isn't working because your user doesn't have the permissions for changing that directory, and the second because your root user doesn't have the right SSH keys for accessing that git repository.

第一个似乎不起作用,因为您的用户没有更改该目录的权限,第二个是因为您的 root 用户没有正确的 SSH 密钥来访问该 git 存储库。

Depending on what you're trying to do, it might be better to clone the repository to a different directory, or maybe chownthe current directory to have full access for your user

根据您尝试执行的操作,最好将存储库克隆到不同的目录,或者chown将当前目录克隆到对您的用户具有完全访问权限

回答by René H?hle

Check if you have enough permissions on the .git/directory. You should have write permissions. You can set them with the following command.

检查您是否对该.git/目录有足够的权限。你应该有写权限。您可以使用以下命令设置它们。

Go to your project folder:

转到您的项目文件夹:

chown -R youruser:yourgroup .git/

回答by Won Jun Bae

If you want to give the permission to the group,

如果你想给组权限,

sudo chmod g+w .git -R

worked best for me.

对我来说效果最好。

For MacOS

对于 MacOS

sudo chmod -R g+w .git 

回答by Johannes Thoma

This is a UNIX permission problem. Do not use sudofor cloning the repository. You don't have the same ssh keys as root and you shouldn't work as root anyway. Try ls -lato find the permissions on the files and use chmod(or sudo chown) to fix them. Hope that helps.

这是一个 UNIX 权限问题。不要sudo用于克隆存储库。您没有与 root 相同的 ssh 密钥,无论如何您都不应该以 root 身份工作。尝试ls -la查找文件的权限并使用chmod(或sudo chown)修复它们。希望有帮助。

回答by shilovk

In my case work fine after it:

在我的情况下,它之后工作正常:

rm -f .git/FETCH_HEAD

git branch -u

回答by pg2286

The answer to this issue make sure .git/FETCH_HEAD has write privileges and you will be all set.

此问题的答案确保 .git/FETCH_HEAD 具有写入权限,您将一切就绪。

I had this issue on Windows and it was resolved by giving write permissions.

我在 Windows 上遇到了这个问题,并通过授予写权限解决了这个问题。

In unix one can run chmod a+rw .git/FETCH_HEADfrom the project repository after which it should work.

在 unix 中,可以chmod a+rw .git/FETCH_HEAD从项目存储库运行,然后它应该可以工作。

回答by Always Sunny

Try like this way,

试试这个方法

Step 1:First check who you are? it will return current user name e.g ubuntu

第1步:首先检查你是谁?它将返回当前用户名,例如ubuntu

$ whoami 

Step 2:Then set permission to your current user, in that case, ubuntuby

第 2 步:然后为您当前的用户设置权限,在这种情况下,通过ubuntu

sudo chown -R ubuntu .git/

回答by Ray

In my case, I only had read access to the .git/FETCH_HEAD file. I had to do "sudo chmod g+w .git/FETCH_HEAD" in order to be able to do a pull request.

就我而言,我只有对 .git/FETCH_HEAD 文件的读取权限。我必须执行“sudo chmod g+w .git/FETCH_HEAD”才能执行拉取请求。

回答by Nicholas Westby

I was having the first issue (FETCH_HEAD permission denied) on Windows.

我在 Windows 上遇到了第一个问题(FETCH_HEAD 权限被拒绝)。

I fixed it by running Git Bash as an administrator (right click, run as administrator).

我通过以管理员身份运行 Git Bash 来修复它(右键单击,以管理员身份运行)。

回答by hobs

If you haven't added yourself to the group that owns .git/, then you should.

如果您尚未将自己添加到拥有 的组中.git/,那么您应该这样做。

sudo usermod -a -G $(stat -c '%G' .git) $USER
sudo chmod g+u .git -R
sudo chmod g+u .gitignore
su - $USER

What this does:

这是做什么的:

  1. finds out which group owns .git/and adds your user to that group.
  2. makes sure group members have the same permissions as the owner for .git/.
  3. repeats this for .gitignore, which you'll probably need
  4. logs you out and back in to refresh your group membership file permissions
  1. 找出哪个组拥有.git/并将您的用户添加到该组。
  2. 确保组成员与所有者拥有相同的权限.git/
  3. 重复这个 for .gitignore,你可能需要
  4. 将您注销并重新登录以刷新您的组成员文件权限

If you just recently did something like this (added yourself to the group that owns .git/), then you need to log out and back in before you'll be able to write to .git/FETCH_HEADduring your git pull.

如果您最近刚刚做了这样的事情(将自己添加到拥有 的组.git/),那么您需要先注销并重新登录,然后才能.git/FETCH_HEADgit pull.