git: Linux中创建中心仓库(or 裸仓库:bare repository)

目录

1. 前言

2. 创建步骤

step1: cd到你要放置该git-repo的地方

step2: 创建git repository

3. git clone

4. 设置权限


1. 前言

        简单介绍在linux系统中创建一个多人协同作业用的git repository的步骤及一些相关设置。这里仅考虑在同一linux服务器内的用户要访问该git-repo,暂不涉及从外部网络访问该git-repo的设置。

        首先,假定你有管理员权限,并且假定先创建了一个组grp_git,允许该组中的成员访问该共享git repository。

2. 创建步骤

step1: cd到你要放置该git-repo的地方

>> cd /path/to/where-you-want-to-put-the-git-repo        

step2: 创建git repository

>> git init --bare  myrepo.git

        使用 --bare 参数初始化的仓库,我们一般称之为裸仓库, 因为这样创建的仓库并不包含 工作区 , 也就是说,我们并不能在这个目录下执行我们一般使用的 Git 命令。

        这个命令执行后,将在本地创建一个名为 repo 的文件夹, 里面包含着 Git 的基本目录, 我们一般会将这个文件夹命名为后面加 .git 的形式,如 repo.git (这也是为什么我们从 GitHub clone 仓库的时候,地址都是 xxx.git 这样的形式的原因)。

3. git clone

        以上创建好了后,用户便可以使用git clone命令将该仓库clone到本地作业目录了。如下所示:

>> git clone /path/to/myrepo.git

        但是,用户应该还不能进行push。常见的git push error如下所示:

error: insufficient permission for adding an object to repository database ./objects
fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit

        解决方案如下章所示。

4. 设置权限

>> cd /path/to/myrepo.git

>> chgrp -R grp_git objects

>> chmod -R g+rws objects

        chgrp命令将访问权限赋给grp_git组内成员。

        The “s” option in the “g+rws” is to set the setuid bit on the objects folder. This will make sure any new directory created under objects folder will make the group name from the objects folder which is owned by git group.

        不建议使用“chmod -R 777 objects”。

        除了以上设置外,还需要将该git-repo设置为共享仓库(shared repository),如下所示:

>> git config core.sharedRepository group

        注意,以上命令中,“group”就是参数名,不是表示组名,即(在本例中)不需要改成grp_git!

        执行以上命令后可以通过执行git config命令查看git仓库的信息:

$ git config -l
user.name=***
user.email=***
core.repositoryformatversion=0
core.filemode=true
core.bare=true
core.logallrefupdates=true
core.sharedrepository=group

        好了,到这里就大功告成,该git仓库可以作为多人协同作业用的共享仓库正式投入使用了。

参考:How to Fix Git Push Insufficient Permission Error for Objects Permanently 

猜你喜欢

转载自blog.csdn.net/chenxy_bwave/article/details/130197891
今日推荐