经过前面的代码版本管理软件Git的优势和用法分析,最后绝对采用Git来作为组织的代码版本管理软件。今天来折腾安装了一下,记录一下要点。
1、增加git用户root@stu-system:/#useradd git
root@stu-system:/#vim /etc/passwd
git:x:501:501::/home/git:/bin/bash/git-shell
root@stu-system:/sudo passwd git
2、安装git和gitosis
依次运行一下命令进行安装,其中gitosis是git的权限管理工具
sudo apt-get install git
sudo apt-get install openssh-server
sudo apt-get install python2.6
sudo apt-get install python-setuptools
apt-get install gitosis
3、git和gitosis配置
修改gitweb.conf
vim /etc/gitweb.conf
# path to git projects (.git)
$projectroot = "/home/git/repositories/";
依次运行以下命令,使用ssh-keygen生成密钥对SSH_KEY和SSH_KEY.pub,并初始化gitosis-admin.git库
ssh-keygen -t rsa
sudo -H -u git gitosis-init
为/home/git/repositories/gitosis-admin.git/hook/post-update增加执行权限
chmod +x /home/git/repositories/gitosis-admin.git/hook/post-update
4、创建git新库的脚本
写了个脚本create-repo.sh方便创建新库,并解决了最后提到的一些问题;在给脚本文件加上执行权限后,只要执行“/home/git/create-repo.sh repo-name.git”即可完成新库创建,其中repo-name替换为新库名称。
root@stu-system:/home/git# cat create-repo.sh
#!/bin/bash
if [ $# -ne 1 ];
then
echo "Usage: $0 repo-name"
exit
fi
cd /home/git/repositories/
mkdir $1.git
cd $1.git
git init
git config --bool core.bare true
cd ..
chown -R git *
chgrp -R git *
5、使用gitosis进行git权限管理
使用git修改gitosis-admin.git库中的gitosis.conf文件,对应的用户公钥放在keydir文件夹中,公钥文件以“用户名.pub”命名,修改完后push回去就可以。提示:使用ssh key generator在Windows下生成的密钥放到gitosis-admin.git前需要在Linux用以下命令来将密钥转换为Linux下的格式。
ssh-keygen -if 公钥文件名>新公钥文件名
实际上push到git主库后,gitosis.conf文件会被更新到/home/git/repositories/gitosis-admin.git/gitosis.conf,这个才是真正生效的权限配置文件,如果通过git库改错了的话,可以在服务器上直接修改这个文件来解决。
#vim /home/git/repositories/gitosis-admin.git/gitosis.conf
[gitosis]
gitweb=yes
[group admin]
members = maple root@stu-system
[group tech]
members = @admin
[group gitosis-admin]
writable = gitosis-admin
members = @admin
[group test]
writable = test
members = @tech
[group test-readonly]
readonly = test
members = maple2
6、各种问题
(1)出现'gitosis-admin' does not appear to be a git repository,短路径无效时替换为全路径
在服务器上使用短路径会取现下面这个问题,原因是没有找到对应匹配的密钥所致,如果正确使用了ssh密钥则不会出现这个问题,这里就不去折腾了
root@stu-system:/home/git/repositories# git clone git@127.0.0.1:gitosis-admin.git
Cloning into gitosis-admin...
git@127.0.0.1's password:
fatal: 'gitosis-admin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
使用以下命令解决,只不过每次push和pull的时候都需要输入一遍git用户的密码,略为繁琐
git clone git@127.0.0.1:/home/git/repositories/gitosis-admin.git
(2)出现Unable to create temporary file: Permission denied
在Windows上使用TortoiseGit执行Push时出现以下错误
git.exe push --force --progress "origin" master:master
Counting objects: 189, done.
Compressing objects: 100% (187/187)
Writing objects: 7% (14/189)
fatal: Unable to create temporary file: Permission denied
fatal: sha1 file '' write error: Invalid argument
error: failed to push some refs to 'git@10.73.4.46:channelv.git'
git did not exit cleanly (exit code 1)
原来是服务器上是用root账户建立的库目录,导致git账户无权写入,方法就是修改文件夹的所属用户和所属用户组
root@stu-system:/home/git/repositories# chown -R git *
root@stu-system:/home/git/repositories# chgrp -R git *
(3)出现failed to push some refs to 'git@10.73.4.46:channelv.git'
在Windows上使用TortoiseGit执行Push时出现以下错误
git.exe push --progress "origin" master:master
Counting objects: 189, done.
Compressing objects: 100% (158/158)
Writing objects: 100% (189/189), 1016.00 KiB | 997 KiB/s
Writing objects: 100% (189/189), 1.12 MiB | 997 KiB/s, done.
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git@10.73.4.46:channelv.git
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@10.73.4.46:channelv.git'
git did not exit cleanly (exit code 1)
在服务器对应的库目录下执行以下命令增加配置即可
git config --bool core.bare true
参考:
本博客所有文章如无特别注明均为原创。