Git服务器搭建 与钩子自动更新

        怀着踩坑过后的血与泪写下这个文章,上班闲着无聊想学学Git服务器搭建。发现百度上的文章大多数要么没详细,要么就有坑。于是一边踩坑一边过来了,于是写这个文章记录一下。


为了方便理解,我把服务器上叫做服务器端,本地称为客户端!
 

一.Git安装

因为官网慢,所以用国内的镜像:
客户端安装:https://npm.taobao.org/mirrors/git-for-windows/
服务器安装: yum -y install git-core

 查看是否安装成功:git --version 

 二.服务器端创建git用户
 

useradd git  创建用户
passwd git    为git用户创建密码

三.服务器端设置git仓库

选定一个目录作为Git仓库,假定是/home/git/wanglaoji.git
服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git

chown -R git:git /home/git/wanglaoji.git

进入wanglaoji.git 目录 然后初始化仓库

git init --bare

四.客户端创建 SSH 公钥和私钥

 本地任意选一个文件夹 作为本地仓库,右键Git Bash  命令行 

git init   #初始化仓库

就会发现文件夹多了一个.git的文件 

 输入以下命令创建 ssh公钥和私钥  闭着眼睛按确认键 自己修改邮箱 随便填写

 ssh-keygen -t rsa -C "976610280@qq.com"

此时 C:\Users\用户名\.ssh 下会多出两个文件 id_rsa(私钥) 和 id_rsa.pub(公钥)


五.服务器端 Git 打开 RSA 认证

进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:

有时候会没有 这个  RSAAuthentication yes  手动添加

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

保存并重启 sshd 服务:

[root@localhost ssh]# /etc/rc.d/init.d/sshd restart
 

由 AuthorizedKeysFile 得知公钥的存放路径是 .ssh/authorized_keys,实际上是 /home/.ssh/authorized_keys,由于管理 Git 服务的用户是 git,所以实际存放公钥的路径是 /home/git/.ssh/authorized_keys

在 /home/git/ 下创建目录 .ssh

mkdir .ssh

然后把 .ssh 文件夹的用户修改为 git

chown -R git:git .ssh
 

 六.将客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件

回到 Git Bash 下,输入以下命令也可以手动去服务器添加:

ssh git@106.13.193.106 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub

需要输入服务器端 git 用户的密码

回到服务器端,查看 .ssh 下是否存在 authorized_keys 文件:

可以查看一下是否是客户端生成的公钥。

重要:

修改 .ssh 目录的权限为 700

修改 .ssh/authorized_keys 文件的权限为 600

[root@localhost git]# chmod 700 .ssh
[root@localhost git]# cd .ssh
[root@localhost .ssh]# chmod 600 authorized_keys 
 

七.关联远程仓库 Git Bash 下输入

git remote add origin git@106.13.193.106:/home/git/wanglaoji.git



八.服务器端设置钩子文件
 

cd/home/git/wanglaoji.git/hooks

复制post-update.sample转成新文件post-update
cp post-update.sample post-update

vi  post-update  输入以下内容 需要修改的自己修改


 

#!/bin/sh
unset GIT_DIR 
DIR_ONE=/www/wwwroot/wanglaoji/  # 更新后的代码会存储在这个文件夹中,文件夹需要存在 
cd $DIR_ONE
git init #初始化仓库
#git remote add gitee https://gitee.com/project.git  # 码云/GitHub代码仓库地址,如果不需要推到远程仓库,可以把这一步和最后的push去掉
git remote add origin /home/git/wanglaoji.git  # 路径指向裸仓库
git reset --hard   # git clean -df
git pull origin master  #下载代码并合并
#git push gitee master # 把代码推到远程仓库

到此结束! 自己试试吧!

如果提交到服务器 的时候  遇到这个错误
 

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'.

 

解决办法:
客户端

这是由于git默认拒绝了push操作,需要进行设置,修改.git/config文件后面添加如下代码:

[receive]
denyCurrentBranch = ignore

服务器端进入仓库那个文件夹,使用 命令

git config --bool core.bare true

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值