在同一台服务器上设置多个 Git 仓库时,可以通过以下方法来实现:
为了避免多个仓库之间的权限混乱,可以为每个仓库创建一个单独的 Git 用户。在服务器上创建新用户,并为其分配一个主目录,例如:
sudo adduser gituser1
sudo adduser gituser2
为每个 Git 用户创建一个单独的 Git 仓库。例如,在每个用户的主目录中创建一个新的目录,并初始化一个新的 Git 仓库:
cd /home/gituser1
mkdir myrepo.git
cd myrepo.git
git init --bare
重复此过程,为每个用户创建一个新的 Git 仓库。
为每个 Git 用户创建一个 SSH 密钥,并将其添加到服务器上的 ~/.ssh/authorized_keys
文件中。例如,为 gituser1
创建一个新的 SSH 密钥,并将其添加到服务器上:
su gituser1
cd ~
ssh-keygen -t rsa -b 4096 -C "gituser1@example.com"
cat ~/.ssh/id_rsa.pub | sudo tee -a ~/.ssh/authorized_keys
重复此过程,为每个用户创建一个新的 SSH 密钥,并将其添加到服务器上的 ~/.ssh/authorized_keys
文件中。
现在,每个用户都可以通过 SSH 访问其自己的 Git 仓库。例如,要克隆 gituser1
的仓库,可以使用以下命令:
git clone gituser1@example.com:/home/gituser1/myrepo.git
要推送更改到远程仓库,可以使用以下命令:
git push gituser1@example.com:/home/gituser1/myrepo.git
通过这种方式,可以在同一台服务器上设置多个 Git 仓库,并为每个仓库分配一个单独的 Git 用户和 SSH 密钥。
没有搜到相关的结果
领取专属 10元无门槛券
手把手带您无忧上云