创建git仓库和使用git访问权限的问题

3

我在UNIX机器上创建了一个代码库,

$sudo git --bare init /var/repos/git/repo00001

此外,我已经为每个UNIX用户执行了以下命令:

$ git config --global user.email "MyfullName@mindtree.com"
$ git config --global user.name "Anand Sadasivam"

在哪个目录下我应该创建git仓库?我在提交文件时遇到了问题。对于目录/var/repos/git,需要设置什么样的权限、用户和组来创建git仓库,在当前情况下它是在root权限下完成的。我需要为git专门创建一个UNIX用户,还是在使用git init创建存储库之前需要遵循特定的步骤或git命令来先创建用户?
我注意到init的--bare选项会在git存储库下创建一些基本结构。但当我用git clone命令克隆我的存储库时,发现它是空的。
由于我使用了--bare选项,我能看到以下文件夹结构被创建:
# ls
HEAD  branches  config  description  hooks  info  objects  refs

以下是我在Windows的git bash客户端上针对一些文件(Readme.txt)进行初始检入所执行的命令:
$ git clone anand@xx.xx.xx.xxx:/var/repos/git/repo00001
Cloning into 'repo00001'...
anand@xx.xx.xx.xxx's password:
warning: You appear to have cloned an empty repository.

<<I had created Readme.txt file under the present directory>>

$ git add Readme.txt

$ git status
On branch master

$ git commit -a
[master (root-commit) 8201309] Initial commit
 Committer: Anand Sadasivam <MMyEmpID@mindtree.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 Readme.txt

$ git push origin master
anand@xx.xx.xx.xxx's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 240 bytes | 120.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
error: remote unpack failed: unable to create temporary object directory
To xx.xx.xx.xxx:/var/repos/git/repo00001
 ! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to 'anand@xx.xx.xx.xxx:/var/repos/git/repo00001'

$ git show-ref
82013098f6801ea32bb620b612f779c8dade6d83 refs/heads/master

$ git push origin HEAD:master
anand@xx.xx.xx.xxx's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 240 bytes | 120.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
error: remote unpack failed: unable to create temporary object directory
To xx.xx.xx.xxx:/var/repos/git/repo00001
 ! [remote rejected] HEAD -> master (unpacker error)
error: failed to push some refs to 'anand@xx.xx.xx.xxx:/var/repos/git/repo00001'

这种情况是否是由于在安装git bash/gui客户端时遵循了Windows设置引起的。

还是完全是UNIX仓库权限问题或仓库创建用户问题。


1
当你使用sudo时,你的问题就开始了。sudo是一个系统工具,只应该用于修改系统配置。Git不是系统配置。使用Git(或任何其他通常用于开发的工具)与sudo一起使用是没有必要的,而且会导致(大部分)问题。 - axiac
现在我已经在UNIX机器上创建了名为“git”的用户。 在该帐户上,我执行了“git init --bare”。 存储库文件夹的“组和用户”已设置为“git”。 对于其他人,没有“写入”权限。 因此,由于我正在使用不同的用户进行“git clone”,所以这些问题仍然存在。 - Dev Anand Sadasivam
对于“group”,通过使用选项“--bare”创建的存储库具有“rw”访问权限。基于此,我已将需要访问该存储库的“UNIX用户”添加到该“group”中。这使得我的git存储库功能正常。 - Dev Anand Sadasivam
这确实是解决方案。将其作为答案发布。 - axiac
不仅如此,--shared 必须和 --bare init 一起使用。 - Dev Anand Sadasivam
否则,如果 git push 出现问题,chmod -R g+wX 也无法解决我们尝试用 --bare init 创建的 repository 文件夹。 - Dev Anand Sadasivam
1个回答

3

应使用--bare init --shared创建仓库,我是指shared选项应该是额外的,并且应该在git登录下完成,不应使用sudo命令。一旦仓库创建完成,它将对组用户具有rw访问权限。

git@xx-xx-xx-xxx~/repos/repo00001$ ls -l
total 32
-rw-rw-r--  1 git git   23 Apr 20 06:44 HEAD
drwxrwxr-x  2 git git 4096 Apr 20 06:44 branches
-rw-rw-r--  1 git git   66 Apr 20 06:44 config
-rw-rw-r--  1 git git   73 Apr 20 06:44 description
drwxrwxr-x  2 git git 4096 Apr 20 06:44 hooks
drwxrwxr-x  2 git git 4096 Apr 20 06:44 info
drwxrwxr-x 62 git git 4096 Apr 25 08:22 objects
drwxrwxr-x  4 git git 4096 Apr 20 06:44 refs

因此,我已将所有需要使用Git存储库的用户添加到组git中。正如您所看到的,git:git是创建的存储库,组git在大多数文件夹下具有rwx权限,这些文件夹将用于维护修订版本。
我已添加并验证了用户是否已添加到组git中。
anand@capsimt00001:~$ sudo usermod -a -G git anand
anand@capsimt00001:~$ groups
anand geckoo git

groups 命令显示特定用户所属的所有组。


现在,为了克隆,我正在使用git clone anand@xx.xx.xx.xxx:/home/git/repos/repo00001 - Dev Anand Sadasivam
如果您从 Windows 本地系统上使用 Eclipse IDE 访问,您需要更新私钥 id_dsa、id_rsa 文件。这些文件可以在您的 UNIX ssh 登录下创建,使用 ssh-keygen 命令并带有 -t DSA dsa 密钥对选项来生成它。您可以在 HOME 目录的 .ssh 文件夹下找到私钥文件 id_dsa、id_rsa。 - Dev Anand Sadasivam
为了在“组”中使用存储库,必须遵循--shared选项和chmod -R g+wX repo-dir的答案 - Dev Anand Sadasivam
现在我在/var文件夹中有一个仓库,即/var/repos/repo00001。我已经相应地调整了权限,以匹配git仓库用户的命令chown -R user:group user_dir。我认为在HOME下创建仓库会有一些影响。 - Dev Anand Sadasivam
基本上,push to upstream 失败了,一旦发生这种情况,我们可能需要执行 git repackgit gc。我认为当我们合并并进行其他操作时,push branch master 是成功的,但连续尝试失败,这次我是在 — Eclipse IDE 中尝试的。 - Dev Anand Sadasivam

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