最近,軟件部需要一個版本控制系統,經過討論,我們選擇了git。對於git和其他版本控制系統如SVN、CVS的區別比較大家可以自己上網查閱。下面我們開始安裝git服務。
一、安裝git
[root@centos ~]# wget http://codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
ps:網上一些文檔給的下載地址是無效的,當然我們也可以到github下載
[root@centos ~]# tar xvf git-latest.tar.gz
[root@centos ~]# ls
[root@centos ~]# cd git-$(date)/
[root@centos ~]# yum install autoconf automake
[root@centos git-$(date)]# autoconf
[root@centos git-$(date)]# mkdir -p /usr/local/git/
[root@centos git-$(date)]# ls /usr/local/
[root@centos git-$(date)# ./configure --prefix=/usr/local/git
[root@centos git-$(date)]# make
在此遇到了一些問題,一些git需要的包沒安裝,之前已經安裝了部分,此時我缺少的是,
[root@centos git-$(date)]# yum install curl curl-devel expat-devel
我們應該以自己的系統爲準,先安裝,錯誤提示缺少什麼再安裝什麼,這也會是我們經驗積累的一個途徑,一般git依賴以下幾個包:curl curl-devel zlib-devel perl cpio expat-devel gettext-devel。
[root@centos git-$(date)]# make install
[root@centos git-$(date)]# git --version
git version 1.7.12-rc2
到此,git安裝成功。建議學習下git的相關操作,即使你自己不寫代碼,不使用版本控制系統,接下來配置gitosis會用到。
二、安裝及配置gitosis
git的服務管理工具有gitosis、gitolite,個人比較喜歡gitolite,但因爲剛學習了git,有些概念還不清楚,鑑於gitosis的資料比較多,感覺比較好掌握,就選擇了gitosis,在以後難免會遷移到gitolite。
開始安裝gitosis:
下載gitosis:
[root@centos gitosis]# git clone git://github.com/res0nat0r/gitosis.git
安裝gitosis時我們使用python setup.py install所以要先安裝python的setup tool,用ez_setup.py安裝。
[root@centos ~]#
wget http://peak.telecommunity.com/dist/ez_setup.py
[root@centos ~]#
python ez_setup.py
[root@centos ~]#
python -c "import setuptools" //測試setuptool是否裝上,無輸出即裝上。
安裝gitosis:
[root@centos ~]#
cd gitosis/
[root@centos gitosis]#
python setup.py install
gitosis安裝結束,接下來進行配置:
[root@centos ~]# useradd -s /bin/bash -d /git/ git
/git爲放置版本庫的位置。
ps:網上有些文檔指定shell環境爲/bin/sh或者/bin/nologin,我建議先指定/bin/bash配置完成後再usermod進行修改,這是因爲之前我配置時因爲自己的一些錯誤加上環境變量,gitosis在初始gitosis-admin庫時始終出錯,困擾了我好幾天。
[root@centos
~]#su - git
[git@centos ~]$vi .bashrc
在末尾添加export PATH=/usr/local/git/bin:/usr/local/git/libexec/git-core:$PATH 。 環境變量一定要添加上,不然會報錯。
在開發的機子上生成公共密鑰:
ssh-keygen
-t rsa //如果在windows下一般也安裝了msysgit可以進入git bash 或者也有生成公鑰的工具。
上傳密鑰到服務器:
scp
~/.ssh/id_rsa.pub root@xxx:/tmp/
環境變量生效後在服務器上進行gitosis-admin管理版本庫初始化,:
[git@centos ~]$
gitosis-init <</tmp/id_rsa.pub
初始化後的管理版本庫位於/git/repositories 建立其他版本庫也應該在這裏。如何配置權限、新建版本庫、更新等,大部分是git相關命令,也足夠在寫一篇博文了,就不寫了。接下來我就記錄幾個自己遇到的問題。
問題一、
[root@centos ~]#
sudo -H -u git gitosis-init < /tmp/id_rsa.pub
Traceback (most recent call last):
File "/usr/local/bin/gitosis-init", line 8, in <module>
load_entry_point('gitosis==0.2', 'console_scripts', 'gitosis-init')()
File "/usr/local/lib/python2.7/site-packages/gitosis-0.2-py2.7.egg/gitosis/app.py", line 24, in run
return app.main()
File "/usr/local/lib/python2.7/site-packages/gitosis-0.2-py2.7.egg/gitosis/app.py", line 38, in main
self.handle_args(parser, cfg, options, args)
File "/usr/local/lib/python2.7/site-packages/gitosis-0.2-py2.7.egg/gitosis/init.py", line 138, in handle_args
user=user,
File "/usr/local/lib/python2.7/site-packages/gitosis-0.2-py2.7.egg/gitosis/init.py", line 75, in init_admin_repository
template=resource_filename('gitosis.templates', 'admin')
File "/usr/local/lib/python2.7/site-packages/gitosis-0.2-py2.7.egg/gitosis/repository.py", line 63, in init
close_fds=True,
File "/usr/local/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/local/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/local/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
出現這樣是環境變量出了問題,你可以用root直接執行 gitosis-init < /tmp/id_rsa.pub 看是否會出現同樣的問題。當然root的環境變量記得加上。如我的是git的安裝目錄下這兩個目錄bin、libexec 。之前因爲是自己出錯把git下的lib和libexec放在/root結果無論怎樣設置git用戶的環境變量,cp還是添加軟鏈接總是提示出錯。加sudo命令也是出錯,後來才知道sudo命令後並不繼承root,或者git用戶的環境。加上之前懷疑python安裝有問題,一直測試、搜索,這個問題困擾了好幾天。能肯定的是如果出現上面提示的極有可能是環境變量的問題。
問題二、
remote:
error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To [email protected]:/var/git.server/.../web
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '[email protected]:/var/git.server/.../web'
這是因爲你在初始版本庫是不是建立裸庫即加上--bare參數,git服務器拒絕了push命令,設置下就可以了
[git@centos
tt.git]$ git config receive.denyCurrentBranch "warn"
一般來說初始git時也應該設置user.name user.email。