建立git使用者
[[email protected] /]# useradd git
#為git使用者設定密碼
[[email protected] /]# passwd git
更改使用者 git 的密碼 。
新的 密碼:
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
安裝軟體
#安裝git
[[email protected] /]# yum install git
#安裝python環境
[[email protected] /]# sudo yum install python python-setuptools
#下載下傳gitosis
[[email protected] /]# cd ~
[[email protected] ~]# git clone git://github.com/res0nat0r/gitosis.git
正克隆到 'gitosis'...
remote: Enumerating objects: 734, done.
remote: Total 734 (delta 0), reused 0 (delta 0), pack-reused 734
接收對象中: 100% (734/734), 147.40 KiB | 120.00 KiB/s, done.
處理 delta 中: 100% (458/458), done.
[[email protected] ~]# ls
gitosis
編譯gitosis
[[email protected] ~]# cd gitosis
# 在gitosis目錄裡有setup.py檔案
[[email protected] gitosis]# python setup.py install
生成目前使用者的ssh公鑰
[[email protected] gitosis]# ssh-keygen -t rsa -C '[email protected]'
拷貝公鑰到 /tmp/ 目錄下
#xx為你的昵稱
[ro[email protected] ~]# cp ~/.ssh/id_rsa.pub /tmp/xx.pub
使用公鑰初始化gitosis
#建立git檔案夾
[[email protected] ~]# cd /home/
[[email protected] home] mkdir git
#賦予/home/git 檔案夾 git使用者權限
[[email protected] ~]# chown -R git:git /home/git/
[[email protected] ~]# cd ~/gitosis/
[[email protected] gitosis]# sudo -H -u git gitosis-init < /tmp/xxx.pub
初始化空的 Git 版本庫于 /home/git/repositories/gitosis-admin.git/
重新初始化現存的 Git 版本庫于 /home/git/repositories/gitosis-admin.git/
#切換到git使用者,正确的話會出現gitosis 和repositories兩個檔案夾
[[email protected] git]# su git
[[email protected] ~]$ ls
gitosis repositories
[[email protected] ~]# sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
切換回root使用者,嘗試拉取gitosis-admin.git
[[email protected] ~]# git clone [email protected]:gitosis-admin.git
正克隆到 'gitosis-admin'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 5 (delta 0)
接收對象中: 100% (5/5), done.
#進入gitosis-admin檔案夾會發現有如下檔案
[[email protected] ~]# cd gitosis-admin/
[[email protected] gitosis-admin]# ls
gitosis.conf keydir
#檢視gitosis.conf檔案
[[email protected] gitosis-admin]# cat gitosis.conf
[gitosis]
[group gitosis-admin]
members = [email protected]
writable = gitosis-admin
接下來開始建立項目和使用者, 例如建立了同僚A,允許讀取項目demo, 那麼首先将同僚A的公鑰上傳到giosis-admin的keydir裡,并且在members裡加入同僚A的名稱
#上傳公鑰到伺服器
scp id_rsa.pub root@(ip位址):gitosis-admin/keydir/userA.pub
#修改gitosis-admin/gitosis.conf
[[email protected] ~] cd gitosis-admin
[[email protected] gitosis-admin] vim gitosis.conf
[gitosis]
[group gitosis-admin]
members = xxx
writable = gitosis-admin
[group demo]
members = userA xxx
writable = demo
#送出gitosis.conf和keydir的修改到git倉庫
[[email protected] gitosis-admin]# git add .
[[email protected] gitosis-admin]# git commit -m 'commit config'
[[email protected] gitosis-admin]# git push origin master
#建立demo項目
[[email protected] ~]# mkdir demo
[[email protected] ~]# cd demo
[[email protected] demo]# git init
初始化空的 Git 版本庫于 /root/demo/.git/
[[email protected] demo]# touch README.md
[[email protected] demo]# git add README.md
[[email protected] demo]# git commit -m 'first commit'
[[email protected] demo]# git remote add origin [email protected]:demo.git
[[email protected] demo]# git push origin master
初始化空的 Git 版本庫于 /home/git/repositories/demo.git/
Counting objects: 3, done.
Writing objects: 100% (3/3), 208 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To g[email protected]:demo.git
* [new branch] master -> master
測試同僚A的賬号在本地是否可以拉取demo項目
#在本地拉取遠端的demo項目
➜ Desktop git clone git@(ip位址):demo.git
Cloning into 'demo'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
➜ Desktop # cd demo
➜ demo git:(master) # ls
README.md
#測試push功能
➜ demo git:(master) touch test.txt
➜ demo git:(master) ✗ git add .
➜ demo git:(master) ✗ git commit -m 'test'
➜ demo git:(master) git push origin master
#伺服器
[[email protected] /]# cd ~/demo/
[[email protected] demo]# git pull origin master
[[email protected] demo]# ls
README.md test.txt
至此已經搭建完成.
參考:
https://blog.51cto.com/icola/1813763