如何在Linux系統環境下搭建Git伺服器
搭建環境:
伺服器 CentOS6.6 + git(version 1.8.3.1)
客戶端 Windows10 + git(version 2.11.1.windows.1)
1. 安裝Git相關軟體
Linux是伺服器端系統,Windows作為客戶端系統,分別安裝Git
安裝服務端:
[[email protected] ~]# yum install -y git
[[email protected] ~]# git –version //安裝完後,檢視 Git 版本
git version 1.8.3.1
安裝客戶端:
下載 Git for Windows,地址:https://git-for-windows.github.io/
安裝完之後,可以使用Git Bash作為命令列客戶端。
$ git –version
git version 2.11.1.windows.1 //安裝完之後,檢視Git版本
安裝Gitosis:
[[email protected] ~]# cd software/
[[email protected] software]# git clone https://github.com/res0nat0r/gitosis.git
[[email protected] software]# yum install python-setuptools -y
[[email protected] software]# cd gitosis
[[email protected] gitosis]# sudo python setup.py install
出現下面的資訊表示安裝成功了
Using /usr/lib/python2.6/site-packages
Finished processing dependencies for gitosis==0.2
2. 伺服器端建立git使用者來管理Git服務
[[email protected] ~]# id git //檢視git使用者是否存在
id: git: no such user
[[email protected] ~]# useradd git
[[email protected]~]# echo “123” | passwd –stdin git
[[email protected] ~]# su – git //切換到git使用者下
3. 配置公鑰
在Windows上配置管理者,git伺服器需要一些管理者,通過上傳開發者機器的公鑰到伺服器,新增成為git伺服器的管理者,開啟git命令列
$ ssh-keygen -t rsa //一直回車,不需要設定密碼
~ scp ~/.ssh/id_rsa.pub [email protected]:~ //複製到git伺服器上
4. 配置gitosis
使用git使用者並初始化gitosis
[[email protected] ~]# cd .ssh
[[email protected] ~]# gitosis-init < ./id_rsa.pub
Initialized empty Git repository in /home/git/repositories/gitosis-admin.git/
Reinitialized existing Git repository in /home/git/repositories/gitosis-admin.git/
[[email protected] ~]# chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update //新增許可權
在Windows上機器上clone gitosis-admin到管理者主機
$ git clone ssh://[email protected]:22/gitosis-admin.git
$ cd gitosis-admin
$ ls
$ gitosis.conf keydir
引數說明:
gitosis.conf: git伺服器配置檔案
keydir: 存放客戶端公鑰
配置gitosis.conf檔案
$ vim gitosis.conf
[gitosis][group gitosis-admin] #組名稱
members = [email protected] #組成員
writable = gitosis-admin #專案名稱[group test] //這裡添加了”test”專案組,上傳到個git伺服器
members = [email protected]
writable = test
在Windows管理者機器上建立本地test倉庫,並上傳到git服務端
$ git config –global user.name “Your Name” //第一次提交需要設定個人資訊,設定使用者名稱和郵箱
$ git config –global user.email “[email protected]”
$ cd ~/repo
$ mkdir test
$ git init
$ tocuh readme.txt
提交到遠端伺服器
$ git add .
$ git commit -a -m ‘init test’
$ git remote add repo [email protected]:test.git //repo 遠端庫的名稱,可以換成任意名稱
$ git push repo master //上傳本地所有分支程式碼到遠端對應的分支上
服務端會自動建立test倉庫
[[email protected]]# pwd
/home/git/repositories
[[email protected] repositories]$ ls
gitosis-admin.git test.git
5.新增其他git使用者開發者
由於公司開發團隊人數不斷增多,手動新增開發者私鑰到/home/git/.ssh/authorized_keys比較麻煩,通過上面的Windows機器的管理者統一收集其他開發者的私鑰id_rsa.pub檔案,然後傳到伺服器上,配置好後,使用者即獲得專案許可權,可以從遠端倉庫拉取和推送專案,達到共同開發專案。
$ cd ~/gitosis-admin/keydir
$ mv ~/id_rsa.pub [email protected] //修改公鑰為主機名.pub
$ vim gitosis.conf
[group test]
writable = test
members = [email protected] [email protected] //新增成員
$ git add .
$ git commit -m “add [email protected] pub and update gitosis.conf”
$ git push repo master
推送完成後,新加進來的開發者就可以進行專案的開發了,後續增加人員可以這樣新增進來,開發者直接把倉庫clone下來就可以了。
git clone [email protected]:/home/git/repositories/test.git
報錯問題:ERROR:gitosis serve main repository read access denied
根據這個報錯,可以看出key是沒問題的,通過排查,發現不應該把這個/home/git/repositories/test.git寫全,
git clone [email protected]:test.git
這樣就可以了。