Linux 执行 git push 时如何修复 .git/ 目录的权限被拒绝?

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

How to fix permission denied for .git/ directory when performing git push?

linuxgitfile-permissionschmodchown

提问by xylar

I have set up a git repository on my server. Created a new user 'git'. My repos are located in /srv/git/example.git. I was able to git remote add origin git@domain/srv/git/example.gitthen I added and committed my changes.

我已经在我的服务器上设置了一个 git 存储库。创建了一个新用户“git”。我的回购位于/srv/git/example.git. git remote add origin git@domain/srv/git/example.git然后我能够添加并提交我的更改。

However when I tried git push origin masterit failed on:

但是,当我尝试git push origin master失败时:

fatal: unable to create temporary file: permission denied' and 'fatal: sha1 file write error: invalid argument'

致命:无法创建临时文件:权限被拒绝”和“致命:sha1 文件写入错误:无效参数”

On the server I ran:

在我运行的服务器上:

sudo chown -R git:git /srv/git/`

This fixed my problem but I am wondering if this was the correct thing to do?

这解决了我的问题,但我想知道这是否正确?

采纳答案by xylar

On the server I ran sudo chown -R git:git /srv/git/ - this fixed my problem but I am wondering if this was the correct thing to do?

在服务器上,我运行 sudo chown -R git:git /srv/git/ - 这解决了我的问题,但我想知道这是否正确?

Absolutely. The problem previously was that the git user, who you're logging in as via SSH, could not write to the repository.

绝对地。之前的问题是您通过 SSH 登录的 git 用户无法写入存储库。

Depending on your needs, you may consider different combinations of users and SSH keys, or one of the many additional programs (gitolite etc) that can be used to more finely control access.

根据您的需要,您可以考虑用户和 SSH 密钥的不同组合,或可用于更精细地控制访问的许多附加程序(gitolite 等)之一。

回答by kenorb

First, fix file permissions in your remote .gitdir e.g.

首先,修复远程.git目录中的文件权限,例如

sudo chmod -R ug+w /var/www/.git
sudo chown -R git:git /var/www/.git

or root:rootif you want to assign members of rootgroup for push access.

或者,root:root如果您想为root群组成员分配推送访问权限。

Then git repository on the destination host needs to be set as shared, so the following command on remote needs to be run:

然后需要将目标主机上的git仓库设置为共享,所以需要在远程运行以下命令:

git config core.sharedRepository group