如何将现有的 git repo 配置为由 UNIX 组共享

我有一个现有的 git repo (一个空的) ,到目前为止只有我可以写。我想将它打开到一些 UNIX 用户组 foo,这样 foo 的所有成员都可以推送到它。我知道我可以很容易地建立一个 新的 git 回购与:

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

但是我需要相当于 存在回购目录的操作。

81412 次浏览

Try this to make an existing repository in repodir work for users in group 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")

In the repo dir execute following commands:

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

Edit: To address frequent confusion, group is an actual keyword, you're not supposed to replace this with the name of the group.

This is probably not necessary, but it's worth pointing out that git init --bare --shared also sets the denyNonFastForwards option.

git config receive.denyNonFastForwards true

The meaning of this option is as follows:

receive.denyNonFastForwards

If you rebase commits that you’ve already pushed and then try to push again, or otherwise try to push a commit to a remote branch that doesn’t contain the commit that the remote branch currently points to, you’ll be denied. This is generally good policy; but in the case of the rebase, you may determine that you know what you’re doing and can force-update the remote branch with a -f flag to your push command.

(from http://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration)

Merging @David Underhill and @kixorz answers, I made my own (definitive) solution.

It is for bare repos and non-bare repos. There are only little differences between them, but in this way is clearer.

BARE REPOSITORY

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

where:

  • <repo.git> is the bare repository directory, typically on the server (e.g. my_project.git/).
  • <group-name> is the group name for git users (e.g. users).

NON-BARE REPOSITORY

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

where:

  • <project_dir> is the project directory containing the .git folder.
  • <group-name> is the group name for git users (e.g. users).

In addition to the above answers of allowing a group to read/write you also need to add the user to the group (say "foo").

sudo usermod -a -G [groupname] [username]

Note: you will have to first create user if it doesn't exist