如何配置现有的git仓库以便UNIX组共享

108

我有一个已存在的 Git 仓库(一个裸仓库),到目前为止只有我自己有写入权限。 我想将其开放给一个 UNIX 用户组 foo,以便 foo 的所有成员都可以推送到它。 我知道可以通过以下方式轻松设置一个 Git 仓库:

git init --bare --shared=group repodir
chgrp -R foo repodir

但我需要现有的仓库目录进行相同的操作。


4
这个问题在ServerFault上有一个优秀的答案(ServerFault是StackOverflow的另一个站点)。 - Zearin
5个回答

125

尝试这个方法,让已经存在于repodir中的存储库可以供foo组的用户使用:

chgrp -R foo repodir                 # set the group
chmod -R g+rw repodir                # allow the group to read/write
chmod g+s `find repodir -type d`     # new files get group id of directory
git init --bare --shared=all repodir # sets some important variables in repodir/config ("core.sharedRepository=2" and "receive.denyNonFastforwards=true")

14
我建议您在仓库的配置中添加config.sharedRepository = true。详情请参考:http://www.kernel.org/pub/software/scm/git/docs/git-config.html - Pistos
2
这与我自己所做的非常接近,但我想得到一些外部确认。谢谢 :) 我还希望有类似于git clone --shared=group的东西,但clone的--shared选项完全不同。 - Pistos
5
你可以在现有的代码库上使用 git init --shared 命令来设置配置值。你还需要执行 chmod 命令来获取文件权限。 - Spencer
1
确认这一点也有助于您解决混乱的情况,因为某人已经以root身份而不是以“www-data”或其他所有者的身份进行了git pull等操作,结果您会收到“error: insufficient permission for adding an object to repository database .git/objects”的错误提示。我曾尝试使用find-type d/type -f修复所有错误的文件/目录的所有权,但只有这种方法才能消除错误(可能是因为某个子目录中的文件没有组写权限?) - William Turrell
2
用户的umask似乎仍然适用于新创建的文件。这是您所期望的吗?我认为core.sharedRepository的文档应该提到这一点——如果用户没有使所有文件组可写,则它似乎是无用的。 - Sam Brightman
显示剩余3条评论

53

合并@David Underhill@kixorz的答案,我制作了自己(最终)的解决方案。

这适用于裸库和非裸库。它们之间只有一些小差异,但这种方式更清晰。

裸库

cd <repo.git>/                            # Enter inside the git repo
git config core.sharedRepository group    # Update the git's config
chgrp -R <group-name> .                   # Change files and directories' group
chmod -R g+w .                            # Change permissions
chmod g-w objects/pack/*                  # Git pack files should be immutable
find -type d -exec chmod g+s {} +         # New files get directory's group id

其中:

  • <repo.git>是裸仓库目录,通常位于服务器上(例如my_project.git/)。
  • <group-name>是git用户组的名称(例如users)。

非裸仓库

cd <project_dir>/                         # Enter inside the project directory
git config core.sharedRepository group    # Update the git's config
chgrp -R <group-name> .                   # Change files and directories' group
chmod -R g+w .                            # Change permissions
chmod g-w .git/objects/pack/*             # Git pack files should be immutable
find -type d -exec chmod g+s {} +         # New files get directory's group id

其中:

  • <project_dir>是包含.git文件夹的项目目录。
  • <group-name>是Git用户组的名称(例如users)。

正如Charles所说,还需要执行以下命令:chmod g-w objects/pack/*(如果不是裸库,请在前面加上.git/)。 - Wernight
在这里,我们如何找到群组名称或如何创建群组名称? - Sujithrao
正如@Dr.X所指出的那样,chmod g+s \find . -type d`并不具有可扩展性。请使用find -type d -exec chmod g+s {} +`。 - hagello
我认为所有松散的对象也应该基于预共享状态是只读的。也许可以像 chmod g-w objects/*/* 这样做。但是,由于此存储库的 info 子目录为空,因此我不确定。 - Eric
3
运行 chmod g=u 可以省去执行 chmod g+w -Rchmod g+w .git/objects/pack/* 的麻烦,因为它将用户的权限应用于组的权限,无论它们是否可写。如果仓库包含只读文件或Git将来可能创建其他只读文件,这种方法更加合适。 - Daniel Böhmer
显示剩余2条评论

50

在repo目录中执行以下命令:

git config core.sharedRepository group
chgrp -R foo repodir
chmod -R g+w repodir

编辑:为了解决经常出现的混淆,group是一个实际的关键字,您不应该用组的名称替换它。


26
group 不是指团体的名称 :) - Pierre de LESPINAY
7
对象和包文件应该是不可变的;它们应该具有权限 444 / r--r--r--。 - CB Bailey
3
在尝试使用git config core.sharedRepository dev并输入git config后,我在git version 1.7.0.4(以及可能的更新版本)中收到了以下消息:fatal: bad config value for 'core.sharedrepository' in .git/config - Kzqai
3
git config core.sharedRepository group “group” 不是组名,而是实际的值! - adamkonrad
1
如果您错误地使用了组名而不是“group”,只需在文本编辑器中打开.git/config文件,编辑core.sharedRepository一行,将其更改为“group”。 - Tom

3

这可能并不是必要的,但值得指出的是,git init --bare --shared 命令还会设置 denyNonFastForwards 选项。

git config receive.denyNonFastForwards true

这个选项的含义如下:

receive.denyNonFastForwards

如果您重新基于已经推送的提交并尝试再次推送,或者尝试将提交推送到不包含远程分支当前指向的提交的远程分支,则会被拒绝。这通常是一个好策略;但在rebase的情况下,您可能确定自己知道自己在做什么,并可以使用push命令的-f标志强制更新远程分支。

(来自http://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

1
除了允许一个组读/写之外,您还需要将用户添加到该组(例如“foo”)。
sudo usermod -a -G [groupname] [username]

注意:如果用户不存在,您将需要先创建用户。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接