搭建git服务器
sever
1.首先安装git
yum install git
2.新建一个linux用户,起名为git
adduser git
3.在git用户目录中新建目录 .ssh
cd /home/git
mkdir .ssh
4.在/home/git/.ssh/目录中新建authorized_keys文件,并将客户端提供的公钥(id_rsa.pub)黏贴到该文件中
vim authorized_keys
5.在项目目录创建一个git裸仓库,假如当前项目目录为/home/git/project.git
git init --bare project.git
6.将项目目录和git用户目录下的.ssh目录的所有者和所属组都设置成git
chown -R git.git project.git
chown -R git.git /home/git/.ssh/
7.为了安全考虑,禁用git用户的shell登录
vim /etc/passwd
注释 #git:x:500:500::/home/git:/bin/bash
改为 git:x:500:500::/home/git:/usr/bin/git-shell
8.git服务器打开RSA认证
vim /etc/ssh/sshd_config
下面3个打开
1.RSAAuthentication yes
2.PubkeyAuthentication yes
3.AuthorizedKeysFile .ssh/authorized_keys
client
1.查看公钥
cat ~/.ssh/id_rsa.pub
如果没有的话,可以执行以下命令
ssh-keygen -t rsa
2.在本地新建git仓库
git init
3.新建一个文件并推送到服务器
touch readme.txt
git add readme.txt
git commit -m "readme"
git remote add origin [email protected]:/home/git/project.git
git push origin master
注:如果提示需要密码,请检测公钥是否配置成功或RSA是否开启。
报错信息为ssh: connect to host 104.224.152.22 port 22: Connection refused
的时候注意下,sshd服务是否开启(一般都是默认开启的)
sever端解决办法
这个时候,我们要检查sshd
服务的端口是否为22
netstat -lnp|grep 22
sshd
服务的端口号不为22,我们可以在/etc/ssh/sshd_config
修改默认端口
client端解决办法
1.直接修改URL为SSH://开头
git remote set-url origin ssh://[email protected]:3022/~/Projects/p1.git
2.修改本地配置文件
vim ~/.ssh/config
# 映射一个别名
host newdomain
hostname domain.com
port 3022
git自动部署
添加钩子文件post-receive
#!/bin/bash
IS_BARE=$(git rev-parse --is-bare-repository)
if [ -z "$IS_BARE" ];
then echo >&2 "fatal: post-receive: IS_NOT_BARE" exit 1
fi unset GIT_DIR DeployPath="/var/www/blog" #这里写项目实际部署的目录
cd $DeployPath
git fetch --all
git reset --hard origin/master
服务器端创建部署项目的文件
cd /var/www
git clone /home/git/project.git 项目名
注:权限问题
在实际使用的时候,会遇到Permission Denied 之类的事情。
那么你要检查下 /var, /var/www, /var/www/your_git 三个目录的权限是否至少开到了 770 上. 然后还要考虑是否有 SELinux 在挡道。
我的处理方式是权限全部开启(因为是个人的服务器,而且也没什么人访问,主要是拿来玩的)
chmod -R 777 /var/www/xxx
还有一个问题,就是push和clone的时候,仓库是最开始创建的空仓库(本文是/home/git/project.git)
最后还有一个问题
Git: push 出错的解决 master -> master (branch is currently checked out)
在使用Git Push代码到数据仓库时,提示如下错误:
[remote rejected] master -> master (branch is currently checked out)
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]:/xxx/xxxx/xxxx
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '[email protected]:/xxx/xxxx/xxxx'
这是由于git默认拒绝了push操作,需要进行设置,修改.git/config添加如下代码:
git config receive.denyCurrentBranch ignore
参考资料:
Centos搭建GIT服务器---老高的技术博客
利用Git自动部署环境
GIT服务器实现web代码自动部署
Git服务器端代码自动部署