一.部署環境 python
系統:CentOS 6.4x64 最小化安裝 nginx
IP:192.168.52.131 git
git默認使用SSH協議,在服務器上基本上不用怎麼配置就能直接使用。可是若是面向團隊服務,須要控制權限的話,仍是用gitolite方便些。github
首先用x-shell 工具連接centos 系統shell
二.安裝基礎依賴包vim
[root@nginx ~]#yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel -y
三.在線安裝gitwindows
[root@bogon ~]# yum -y install git centos
下載安裝包安裝方式:bash
[root@bogon git-2.2.0]#tar xf git-2.2.0.tar.gz [root@bogon git-2.2.0]#cd git-2.2.0/ [root@bogon git-2.2.0]#make prefix=/usr/local/git all [root@bogon git-2.2.0]#make prefix=/usr/local/git install 增長軟鏈接 [root@bogon git-2.2.0]#ln -s /usr/local/git/bin/* /usr/bin/ 驗證結果,顯示出版本號,表示安裝成功 [root@bogon git-2.2.0]#git --version git version 2.2.0
五.建立git用戶服務器
[root@bogon ~]# useradd git # 切換到git用戶 [root@bogon ~]# su - git [git@bogon ~]$ pwd #查看當前位置 /home/git # 確認.ssh/authorized_key是否存在 [git@bogon ~]$ ls -a . .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla
六.獲取git版本庫並安裝
[git@bogon ~]$ git clone https://github.com/sitaramc/gitolite
而後建立bin目錄並安裝gitolite 到bin目錄($ 表示普通用戶)
[git@bogon ~]$ mkdir $HOME/bin [git@bogon ~]$ gitolite/install -to $HOME/bin [git@bogon ~]$ cd bin/ [git@bogon bin]$ ls commands gitolite gitolite-shell lib syntactic-sugar triggers VERSION VREF
七.配置gitolite管理員
gitolite 使用特殊的版本庫gitolite-admin 來管理用戶和版本庫,因此須要建立一個管理員來管理全部的用戶和版本庫
7.1 用git用戶生成公鑰(注:若是你是接着上面一直作下來的,那麼只須要輸入cd ,而後回車,則就進入了該用戶的目錄,若是不是git用戶,則須要輸入su – git,來切換到git用戶)
[git@bogon ~]cd [git@bogon ~]$ ssh-keygen -t rsa
7.2 修改.ssh/id_rsa.pub 爲admin.pub(爲何要改爲admin.pub 由於gitolite根據這個文件名來設立賬號.我這裏用admin
[git@bogon ~]$ mv .ssh/id_rsa.pub admin.pub
7.3 使用管理員公鑰安裝gitolite(而後去/home/git/repositories 裏面,能夠看見倉庫文件.gitolite-admin.git 和test.git 一個是管理倉庫的,一個是測試用)
[git@bogon ~]$ $HOME/bin/gitolite setup -pk admin.pub
7.4 生成管理員管理倉庫(不須要輸密碼)
[git@bogon ~]$ git clone git@127.0.0.1:gitolite-admin
進入倉庫後能夠看到conf 和keydir ,conf/gitolite.conf 是添加用戶/倉庫的配置, keydir 是放對應用戶的公想·x鑰.而且此時,admin.pub這個公鑰能夠刪除了
7.5 編輯gitolite.conf
[git@bogon ~]$ vim gitolite-admin/conf/gitolite.conf
也能夠你找下圖操做:
打開圖按照下圖添加:
上面的配置含義是:
定義兩個用戶組: admin 和 user
定義兩個版本庫: gitolite-admin 和 project
用戶admin 對 這兩個版本庫有(讀 寫 刪除)RW+ 權限
user組對project 有 (讀 寫)RW 權限
也可按照格式,添加多個組,多個版本庫
若有一個組中有多個用戶,則以空格爲分割,以下圖:
7.6 而後提交修改(注:修改用戶或者增長版本庫後在下圖中的目錄中提交修改,不然會提示修改失敗)
提交修改:
[git@bogon conf]$ git add keydir/test.pub conf/gitolite.conf
[git@bogon conf]$ git add . [git@bogon conf]$ git commit -am "new project"
[git@bogon conf]$ git push origin master
# 注:全部的提交修改都是這三條命令,在客戶端上提交文件也是這三個命令第二條命令後面引號中的 能夠是任意,最好是能體現是作了什麼修改
7.7 提交完後,查看管理倉庫中多了一個project.git
[git@bogon ~]$ cd repositories/ [git@bogon repositories]$ ls gitolite-admin.git project.git
這個時候,服務器已經搭建完畢了,只須要添加用戶並給用戶添加權限就能夠了。下面咱們添加用戶測試
7.8 權限控制
[root@nginx gitolite-admin]# git status #查看git庫狀態 On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: conf/gitolite.conf #表示該文件被修改 Untracked files: (use "git add <file>..." to include in what will be committed) keydir/test.pub #增長的文件 no changes added to commit (use "git add" and/or "git commit -a") #將有改動的文件添加到git庫 [root@nginx gitolite-admin]# git add keydir/test.pub conf/gitolite.conf [root@nginx gitolite-admin]# git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: conf/gitolite.conf new file: keydir/test.pub #這裏表示已添加#在第一次執行git commit命令時會有如下提示,須要使用者代表身份 [root@nginx gitolite-admin]# git commit -m "add repo mytest; add user test" *** Please tell me who you are.Run git config --global user.email "you@example.com" git config --global user.name "Your Name"to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address (got 'root@nginx.(none)') #在這裏咱們執行如下2條命令 [root@nginx gitolite-admin]# git config --global user.email "lyao@aaa.com" [root@nginx gitolite-admin]# git config --global user.name "lyao" #再從新執行git commit命令 [root@nginx gitolite-admin]# git commit -m "add repo mytest; add user test" [master 7b877e7] add repo mytest; add user test 2 files changed, 5 insertions(+) create mode 100644 keydir/test.pub #執行完以上命令後,修改的文件只是提交在本地了,並無提交到git server中,還須要執行git push命令 [root@nginx gitolite-admin]# git push origin master #推送到遠端的master分支 Counting objects: 6, done.Compressing objects: 100% (5/5), done. Writing objects: 100% (6/6), 817 bytes | 0 bytes/s, done. Total 6 (delta 0), reused 0 (delta 0) remote: Initialized empty Git repository in /home/git/repositories/mytest.git /To git@192.168.3.27:gitolite-admin 3554f3d..7b877e7 master -> maste
8、客戶端驗證
8.1 centos客戶端驗證
[root@ipython ~]# git clone git@192.168.3.27:/mytest.git Initialized empty Git repository in /root/mytest/.git/ warning: You appear to have cloned an empty repository. [root@ipython ~]# ll total 3240 drwxr-xr-x 3 root root 4096 Apr 17 14:45 mytest [root@ipython ~]# cd mytest/ [root@ipython mytest]# git status # On branch master# # Initial commit #nothing to commit (create/copy files and use "git add" to track) #建立1個文件1.txt並添加到git [root@ipython mytest]# touch 1.txt [root@ipython mytest]# git add 1.txt [root@ipython mytest]# git commit "add file 1.txt" #第一次使用須要代表身份 [root@ipython mytest]# git commit -m "add file 1.txt" [master (root-commit) 08e9a37] add file 1.txt Committer: root <root@ipython.(none)> 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: git config --global user.name "Your Name" git config --global user.email you@example.com If the identity used for this commit is wrong, you can fix it with: git commit --amend --author='Your Name <you@example.com>' 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 1.txt [root@ipython mytest]# git config --global user.email test@aaa.com [root@ipython mytest]# git config --global user.name test [root@ipython mytest]# git commit -m "add file 1.txt" # On branch masternothing to commit (working directory clean) [root@ipython mytest]# git status # On branch masternothing to commit (working directory clean) [root@ipython mytest]# ll total 0-rw-r--r-- 1 root root 0 Apr 17 14:47 1.txt [root@ipython mytest]# git push origin master Counting objects: 3, done.Writing objects: 100% (3/3), 206 bytes, done. Total 3 (delta 0), reused 0 (delta 0)To git@192.168.3.27:/mytest.git * [new branch] master -> master
8.2 windows客戶端驗證
1)在客戶機上安裝安正的git程序,在D盤新建一個文件夾 test ,右鍵選擇Git Bash
2)打開一個DOS欄,輸入 ssh-keygen.exe。這裏能夠用Tab鍵補全,而後有提示輸入y後一直回車到結束
3)而後打開客戶機用戶目錄下的 .ssh 中能看到id_rsa 私鑰
4)而後把id_rsa 這個私鑰文件上傳到服務器的/home/git/gitolite-admin/keydir 目錄中
5)而後把這個新傳上來的私鑰更名
[git@bogon conf]$ vi gitolite.conf
這裏表示test用戶對project.git 版本庫有讀寫的權限。也能夠直接在版本庫下面添加用戶權限
6)提交修改
[git@bogon conf]$ git add . [git@bogon conf]$ git commit -am "new project" [git@bogon conf]$ git push origin master
7)回客戶端
打開Bash 窗口,輸入
git clone git@192.168.52.131:project.git
連接服務器中的project.git 版本庫,其中IP地址是本身服務器的IP地址,冒號後面須要訪問也是剛剛給該用戶有權限訪問的版本庫
而後會同步下來一個文件夾名爲project 的文件,這個就是版本庫,打開這個文件夾,建立一個任意文件
在客戶機上提交修改文件:
打開project文件夾,建立一個任意文件,而後打開Bash 窗口,輸入提交修改命令
[git@bogon conf]$ git add .
[git@bogon conf]$ git commit -am "new project"
[git@bogon conf]$ git push origin master
這樣,修改過的數據就上傳到服務器上去了,其餘用戶一樣能夠同步下來
從服務器上往別的客戶機上同步更改後的版本庫的命令是:
git pull