git 服務器的搭建


文章轉載地址:http://www.linuxidc.com/Linux/2015-05/117502.htm

親測可行,已經自己搭建一個正在使用中,搭建完成后,結合着pycharm +git,就能直接本地編輯了;

注:文章中有在原文修改的部分,會用紅色標出

Git是一款免費、開源的分布式版本控制系統,用於敏捷高效地處理任何或小或大的項目,學過Linux的都知道,Git的優點我就不再多說了,我也是很喜歡Linux的。今天我們一起學習Git服務器在Ubuntu 12.04上的配置,廢話不多說,走起! 

1.Git 服務器的搭建過程詳細記錄,如下:

搭建環境:Ubuntu 12.04 桌面版或服務器版均可 軟件要求: git-core, gitosis, openssh-server, openssh-client, Apache2(Gitweb),python編譯環境。

2,安裝配置git服務器。

  准備工作,為了不改變計算機的環境,我們新增家一個用戶:
  User Name:git
  Password:git
  切換用戶:su git

  

3.安裝git和openssh: 
  git@lightserver-GREATWALL-PC:~$ sudo apt-get install git-core openssh-server openssh-client

   

 

4.添加Git用戶gitadmin,該用戶將作為所有代碼倉庫和用戶權限的管理者:

  git@lightserver-GREATWALL-PC:~$ sudo useradd -m gitadmin

    

5.建立一個git倉庫的存儲點:

  

 

6.更改gitadmin的權限(同時也要保證 gitadmin下的所有文件,都歸屬與gitadmin

  git@lightserver-GREATWALL-PC:~/repo$ sudo chown gitadmin:gitadmin /home/git/repo

   

   更改倉庫的訪問權限:

  sudo chmod 700 /home/git/repo

  

7.安裝配置gitosis

  初始化一下服務器的git用戶,這一步其實是為了安裝gitosis做准備。在任何一台機器上使用git,第一次必須要初始化一下:初始化用戶名和郵箱。
  git@lightserver-GREATWALL-PC:~/repo$ sudo git config --global user.name “Your Name”

  

  

  OK,沒有報錯,通過設置!
  初始化git郵箱
  git@lightserver-GREATWALL-PC:~/repo$ sudo git config --global user.email “你的用戶名@你的ip“ 

      @后是服務器IP。

  

8.安裝一下python的setup tool, 這個也是為了gitosis做准備:

  

  git@lightserver-GREATWALL-PC:~/repo$ sudo apt-get install python-setuptools 
  OK,Next!

9.獲得gitosis包:切換到/tmp下。

  git@lightserver-GREATWALL-PC:/$ cd /tmp 

   

    git@lightserver-GREATWALL-PC:/tmp$ git clone https://github.com/res0nat0r/gitosis.git

   

    ls一下:多了個gitosis文件夾吧?

    

   Perfect,Next,python 安裝gitosis

    git@lightserver-GREATWALL-PC:/tmp/gitosis$ sudo python setup.py install

  

  到此為止,gitosis安裝完成!

10.切換到gitadmin用戶下:
  light-server@lightserver-GREATWALL-PC:~sugitadmin  : 

  

  為什么是$ ?多難看。Linux支持bash,shell,sh或許就是這個原因吧?我猜的,試試不就知道了?走起。。   

  

  還真是這個原因,這樣看着順眼!

11.默認狀態下,gitosis會將git倉庫放在 gitadmin用戶的home下,所以我們做一個鏈接到/home/repo
  ln -s /home/git/repo /home/gitadmin/repositories
  返回正常用戶:
  $ exit 
  light-server@lightserver-GREATWALL-PC:~$

   

  查看軟鏈接狀態:

  

12.如果你將作為git服務器的管理員,那么在你的電 腦上(另一台pc)生成ssh公鑰:

這是在windows下生成公匙的方法點擊 (如果你就一個服務器,可以這樣弄)

配合着本地的git Bash

  我在我的電腦上,用戶為:wuzhang@ubuntu:$ 
  生成ssh公鑰。
  wuzhang@ubuntu~:$ ssh-keygen -t rsa

  

  OK,生成公鑰成功!
  復制到遠程主機上,這就考驗我們Linux 命令的基礎了。還好大一linux學的還不錯!
  Scp 遠程復制命令: scp 本地文件 遠端主機:存儲路徑

  例如:scp .ssh/id_rsa.pub light-server@10.255.13.253:/tmp

  

  OK,100%這個我喜歡, 說明復制成功了,不信我們可以去主機查看:

  

  果然有id_rsa.pub.

  在git服務器上,更改權限:
  git@lightserver-GREATWALL-PC:/tmp/gitosis$ sudo chmod a+r /tmp/id_rsa.pub

  

13.讓gitosis運行起來:

  執行命令:sudo -H -u git gitosis-init < /tmp/id_rsa.pub

  

  Initialized empty Git repository in /home/repo/gitosis-admin.git/

   Reinitialized existing Git repository in /home/repo/gitosis-admin.git/
   說明實例化空的git倉庫/home/git/repositories/gitosis-admin.git/已成功了。

注:執行到這一步有可能出現問題

 

解決辦法:重啟服務器,重新生成 id_rsa.pub(具體原因:點擊,這個解決應該可行)

14.gitosis的有趣之處在於,它通過一個git倉庫來管理配置文件,倉庫就放在了/home/git/repositories/gitosis-admin.git。

我們需要為一個文件加上可執行權限:

 獲取root權限

  light-server@lightserver-GREATWALL-PC:/home$ sudo -i 
  目錄切換:
  root@lightserver-GREATWALL-PC:/home/git# cd repositories/ 
  root@lightserver-GREATWALL-PC:/home/git/repositories# ls 
  gitosis-admin.git 
  root@lightserver-GREATWALL-PC:/home/git/repositories# cd gitosis-admin.git 
  root@lightserver-GREATWALL-PC:/home/git/repositories/gitosis-admin.git# ls 
  config gitosis.conf gitosis-export HEAD hooks index objects refs

  

更改權限(關鍵步驟)

root@lightserver-GREATWALL-PC:/home/git/repositories/gitosis-admin.git# sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update 
root@lightserver-GREATWALL-PC:/home/git/repositories/gitosis-admin.git# exit
准備工作到此結束了,下面可以進行測試了!

15.在服務器上新建一個測試項目倉庫

我建了一個叫“teamwork”的倉庫。切換到gitadmin用戶:
gitadmin@lightserver-GREATWALL-PC:~mkdirteamwork.gitgitadmin@lightserverGREATWALLPC:   cd teamwork.git/ 
gitadmin@lightserver-GREATWALL-PC:~/teamwork.git$

git的初始化:
gitadmin@lightserver-GREATWALL-PC:~/teamwork.git$ git init --bare 
Initialized empty Git repository in /home/gitadmin/teamwork.git/ 
gitadmin@lightserver-GREATWALL-PC:~/teamwork.git$

 

但是,到目前為止,這只是一個空倉庫,空倉庫是不能clone下來的。為了能做clone,我們必須先讓某個有權限的人放一個初始化的版本到倉庫中。
所以,我們必須先修改一下/home/git/repositories /gitosis-admin.

16.管理gitosis的配置文件

剛剛提到,gitosis本身的配置也是通過git來實現的。在你自己的開發機里,把gitosis-admin.git這個倉庫clone下來,就可以以管理員的身份修改配置。

好的,我們回到另一台PC上進行對服務端的測試! 我自己的PC,名字叫:wuzhang@ubuntu

wuzhang@ubuntu:~/work$ sudo git clone root@10.255.13.253:/home/git/repositories/gitosis-admin.git 
Cloning into 'gitosis-admin'... 
root@10.255.13.253's password: 
remote: Counting objects: 5, done. 
remote: Compressing objects: 100% (4/4), done. 
remote: Total 5 (delta 0), reused 5 (delta 0) 
Receiving objects: 100% (5/5), done. 
wuzhang@ubuntu:~/work$

PS:目前主機必須是root才可以克隆.

wuzhang@ubuntu:~/work/gitosis-admincdkeydir/wuzhang@ubuntu: /work/gitosisadmin/keydir  ls 
wuzhang@ubuntu.pub 
wuzhang@ubuntu:~/work/gitosis-admin/keydir$ 
wuzhang@ubuntu上用戶權限配置(
gitosis.conf)使用vi編輯器打開(默認應該就有一個用戶)

[gitosis]

[group gitosis-admin] 
members = wuzhang@ubuntu 
writable = gitosis-admin 
members = wuzhang@10.255.13.253 light-server@10.255.13.253

[group hello] 
writable = teamwork 
members = lijiangkun@server b git@10.258.13.100

[group hello_ro] 
readonly = teamwork 
members = lz

 

 

 wq!  保存退出.

這個配置文件表達了如下含義:gitosis-admin組成員有wuzhang@ubuntu, wuzhang@10.255.13.100, light-server@10.255.13.253
該組對gitosis-admin倉庫有讀寫權限;
team組有lijinagkun@server,b兩個成員.該組對teamwork倉庫有讀寫權限;
team_ro組有lz一個成員,對teamwork倉庫有只讀權限
在wuzhang@ubuntu上創建一hello.txt的測試文檔:
wuzhang@ubuntu:~/work/teamwork_projectecho"Testgitserver.">hello.txtwuzhang@ubuntu: /work/teamworroject  ls -al 
總用量 16 
drwxrwxr-x 3 wuzhang wuzhang 4096 5月 13 18:08 . 
drwxrwxr-x 4 wuzhang wuzhang 4096 5月 13 18:07 .. 
drwxrwxr-x 7 wuzhang wuzhang 4096 5月 13 18:07 .git 
-rw-rw-r-- 1 wuzhang wuzhang 18 5月 13 18:09 hello.txt 
wuzhang@ubuntu:~/work/teamwork_project$

接下來要提交到遠端服務器了。

root@ubuntu:/home/wuzhang/work/gitosis-admin# git add . 
root@ubuntu:/home/wuzhang/work/gitosis-admin# git commit -m "add teamwork and user for git server" 
[master 51a4055] add teamwork and user for git server 
Committer: root <root@ubuntu.ubuntu-domain> 
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

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

2 files changed, 9 insertions(+) 
create mode 100644 keydir/b.pub

wuzhang@ubuntu:~/work/teamwork_project$ git commit -m "initial version" 
# On branch master 

# Initial commit 

# Untracked files: 
# (use "git add <file>..." to include in what will be committed) 

# hello.txt 
nothing added to commit but untracked files present (use "git add" to track) 
wuzhang@ubuntu:~/work/teamwork_project$ 
添加遠端服務器的地址:
root@ubuntu:/home/wuzhang/work/teamwork_project# git remote add origin gitadmin@10.255.13.253:/home/gitadmin/teamwork.git

注:如果上一步出現問題:(具體點擊

提示出錯信息:fatal: remote origin already exists.

    解決辦法如下:

    1、先輸入$ git remote rm origin

    2、再輸入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不會報錯了!


root@ubuntu:/home/wuzhang/work/teamwork_project# git remote -v 
origin gitadmin@10.255.13.253:/home/gitadmin/teamwork.git (fetch) 
origin gitadmin@10.255.13.253:/home/gitadmin/teamwork.git (push) 
root@ubuntu:/home/wuzhang/work/teamwork_project# 
提交文件到服務器:
lroot@ubuntu:/home/wuzhang/work/gitosis-admin# git remote -v 
origin gitadmin@10.255.13.253:teamwork.git (fetch) 
origin gitadmin@10.255.13.253:teamwork.git (push) 
root@ubuntu:/home/wuzhang/work/gitosis-admin# git push -f origin master 
gitadmin@10.255.13.253's password: 
Counting objects: 9, done. 
Delta compression using up to 4 threads. 
Compressing objects: 100% (8/8), done. 
Writing objects: 100% (9/9), 1.12 KiB, done. 
Total 9 (delta 1), reused 4 (delta 0) 
To gitadmin@10.255.13.253:teamwork.git 
* [new branch] master -> master 
root@ubuntu:/home/wuzhang/work/gitosis-admin#

換個用戶,測試下是否可以從服務器克隆剛提交的文件?
切換到另一賬戶進行git clone測試:
wuzhang@ubuntu:~sugitgit@ubuntu:/home/wuzhan

查看服務器的地址:
git@ubuntu:/home/wuzhang/work/teamwork_project$ git remote -v 
origin gitadmin@10.255.13.253:teamwork.git (fetch) 
origin gitadmin@10.255.13.253:teamwork.git (push)

 

創建個文件夾,用於存放克隆從服務器獲取的文件

git@ubuntu:/home/wuzhang/work/teamwork_projectcd/gitClone/git@ubuntu:/gitClone  git clone gitadmin@10.255.13.253:teamwork.git 
fatal: could not create work tree dir 'teamwork'.: ???? 
git@ubuntu:/gitClone$ git clone gitadmin@10.255.13.253:teamwork.git

出錯了,不要怕,應該是權限的問題,我們再試下!

PS:因為git@ubuntu是普通用戶,克隆的文件在本地需要創建文件夾,所以權限不夠,需要sudo
git@ubuntu:/gitClone$ sudo git clone gitadmin@10.255.13.253:/home/wuzhang/work/teamwork 
Cloning into 'teamwork'... 
gitadmin@10.255.13.253's password: 
remote: Counting objects: 10, done. 
remote: Compressing objects: 100% (7/7), done. 
remote: Total 10 (delta 1), reused 10 (delta 1) 
Receiving objects: 100% (10/10), done. 
Resolving deltas: 100% (1/1), done. 
git@ubuntu:/gitClonelsteamworkgit@ubuntu:/gitClone  cd teamwork/

顯示一下克隆的文件:
git@ubuntu:/gitClone/teamwork$ ls 
1 1.c 1.c~ hello hello.txt

執行下我編譯過得1.c文件

git@ubuntu:/gitClone/teamwork$ ./1 
Git Server is OK! 
git@ubuntu:/gitClone/teamwork$ 
克隆完成,服務器環境搭建完成!

可以在windows下直接克隆,然后試試!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM