在centos上搭建git服务器并自动同步代码

参考文章

CentOS安装Git实现多人同步开发
centos中GIT服务器搭建及使用密钥链接php

简述

一、服务器上安装Git依赖及Git

二、建立Git用户及所属组

三、服务器上初始化Git仓库

四、安装Git客户端并生成公钥

五、建立证书登陆

六、使用Git Bash克隆服务器上的空仓库

七、将本地库项目推送到服务器

一、服务器上安装Git以及依赖

1.1安装Git依赖html

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel

1.2.安装Gitgit

yum install -y git

二、建立”用户组“和”用户“,用来运行git服务

2.1建立用户组web

groupadd git

2.2添加git用户组下的用户centos

adduser phper -g git

2.3为用户名为phper 的用户设置密码服务器

passwd phper
   Changing password for user git.
   New password: 
   Retype new password: 
   passwd: all authentication tokens updated successfully.

三、创建git仓库

mkdir gitroot
chmown phper:git gitroot
cd /gitroot
git init --bare project.git
chmown -R phper:git project.git
chmod 774 -R project.git
cd ../
chmod 750 gitroot

四、安装Git客户端并生成公钥

4.1下载git客户端安装好后右键选择Git GUI Here->Help->Show SSH Keyssh

image.png

image.png

就能获得私钥和公钥curl

image.png
打开Puttygenimage.pngpost

image.png

load以前生成好的私钥image.png
获得格式化后的私钥,点击保存私钥image.pngfetch

配置小乌龟image.png
4.2建立证书登陆
切换到phper目录

cd /home/phper
mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys

而后将客户端的公钥上传到.ssh目录

cd .ssh
rz

将公钥添加进authorized_keys中

cat id_rsa.pub >> authorized_keys

五、克隆到本地
右键选择git克隆image.png
这样就把服务器的空仓库拉取下来了。
image.png

如今咱们可以正常的提交代码到服务器的git,可是还不能自动同步,咱们还须要修改服务器的hooks/post-receive文件。具体post-receive内容

#!/bin/sh
unset GIT_DIR  
DeployPath="/alidata/www/project"
LogPath="/alidata/gitroot/project.git/hooks"  
   
echo -e "\n=================  `date +"%Y-%m-%d %H:%M:%S"`  ===============\n" >> $LogPath/gitsync.log 2>&1 
cd $DeployPath  
   
#git stash  
#先拉取再合并
git pull origin master  >> $LogPath/gitsync.log 2>&1 

#强制与远程服务器同步,不与本地合并,只能经过提交的客户端提交的方式修改代码。
#git fetch --all  
#git reset --hard origin/master  
   
#time=`date`  
#echo "web server pull at webserver at time: $time."  
echo "================================================"  >> $LogPath/gitsync.log 2>&1

更改post-receive的全部者和权限

chmod -R 774 post-receive
chown phper:git post-receive

最后在www下

mkdir project
chown -R phper:git project
chmod -R 774 project
cd /alidata/www
git clone /gitroot/project.git

好了git的同步就弄好了

若是同步用不了,打开hooks/gitsync.log查看错误日志

可能预见的错误

一、fatal: /usr/libexec/git-core/git-pull cannot be used without a working tree.

同步的项目文件夹没有创建---------------解决办法:在www下创建project项目文件夹

二、fatal: Not a git repository (or any of the parent directories): .git

项目文件里没有git初始化------------------解决办法:在www路径下执行git clone /alidata/gitroot/project.git

三、error: cannot open .git/FETCH_HEAD: Permission denied

git在项目目录没有写入权限---------------解决办法:修改全部者以及权限 chown -R phper:git project / chmod -R 774 project

四、每次pull push的时候仍是要输入密码image.png

秘钥没有起做用-----------------------------解决办法:/var/log/secure查看一下日志,是不是.ssh的权限问题 chmod 700 .ssh chmod 600 .ssh/authorized_keys

相关文章
相关标签/搜索