GIT服务端部署、gitolite配置
1.服务端安装git
yum -y install git-core
2.添加Git账户
useradd git
3.修改git 账号登陆shell
vi /etc/passwd
将git的shell改成/usr/bin/git-shell
4.添加公钥文件
4.1在/home/git/目录下新建.ssh目录
mkdir /home/git/.ssh
4.2新建authorized_keys文件
touch authorized_keys
4.3修改文件夹及文件权限
chmod 700 /home/git/.ssh
chmod 600 /home/git/.ssh/authorized_keys
5.将客户端公钥拷贝到authorized_keys中
6.Git初始化
git config --global user.name "name"
git config --global user.email "name@gmail.com"
git config --global push.default "matching"
git config --global color.status auto
git config --global color.branch auto
7.初始化仓库
cd /var
mkdir /var/gitroot/code.git
cd code.git
7.1初始化仓库
git init —bare
chown -R git:git gitroot
#以上为服务端代码仓
8.本地根据系统安装git
9.初始化git配置
mkdir git
git init
10.将仓库拷贝到本地(ssh默认端口号改成了8833,默认22的话可以不加端口号)
git clone ssh://git@xxx.xxx.xxx.xxx:8833/var/gitroot/code.git
11.创建文件并上传(以下可以不看,直接跳到14,看权限管理)
cd code #进入刚才拷贝下来的仓库
touch readme.md
git add -A
git commit -m ’test’
git push
#第一次push要指定主分支
git push -u origin master
#由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。
12.在服务器端git pull
报错
fatal: /usr/libexec/git-core/git-pull cannot be used without a working tree.
原因:
git pull is one of them. git pull is the combination of git fetch and git merge. Now, git fetch involves only the .git directory, so it can be performed on a bare repository. On the other side, git merge requires a merge on the Working Directory to be performed. And since the working directory is not present, the command will fail.
显然缺少工作目录,pull失败
13.新建工作目录
mkdir /var/op-project
cd op-project
git clone /var/gitroot/code.git
chown -R git:git op-project
cd /var/gitroot/code.git/hooks
#设置服务端钩子
vi post-receive
#!/bin/sh
unset GIT_DIR
DeployPath="/var/op-project"
LogPath="/var/gitroot/code.git/hooks"
echo -e "\n================= `date +"%Y-%m-%d %H:%M:%S"` ===============\n" >> $LogPath/gitsync.log 2>&1
cd $DeployPath
git pull origin master >> $LogPath/gitsync.log 2>&1
touch gitsync.log
chown git:git gitsync.log
cd /var/op-project/code
Git pull
成功
14.gltolite安装和配置
14.1清空authorized_keys,如果已经存在,直接改名
14.2上传客户端SSH公钥到服务器的/home/git目录,命名为name.pub
yum install 'perl(Data::Dumper)'
su git
cd /home/git
git clone https://github.com/sitaramc/gitolite
14.3新建bin文件夹
mkdir -p /home/git/bin
14.4安装到bin目录
/home/git/gitolite/install -to /home/git/bin
14.5把上传到服务器的 管理员的公钥setup到gitolite中,注意:YourName.pub改成你自己的文件名。
/home/git/bin/gitolite setup -pk /home/git/iori.pub
#发现git目录下生成了repositories目录,其中有两个仓库,gitolite-admin.git、testing.git
14.6回到管理员电脑
cd gitolite-admin
ls gitolite-admin
#看到conf 和 keydir目录,conf管理用户、用户组、新建仓库,keydir管理公钥
14.7拷贝用户公钥到keydir,命名为NAME.pub
14.8进入gitolite-admin目录
cd gitolite-admin
14.9添加用户组AAA,添加用户BBB,赋予BBB权限,建仓库AAA
vi conf/gitolite.conf
@AAA = iori BBB
repo AAA
RW = @AAA
14.10在gitolite-admin目录下
git add keydir/chuxiaoliangpub.pub conf/gitolite.conf
git status
git commit -m 'add repo AAA,add user BBB'
git push