版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/q85795362/article/details/79289311
1.安装git
yum -y install git
卸载
yum -y remove git
2.查看安装的版本
git --version
3.创建git用户
useradd git
passwd git
#根据提示创建git用户密码 线上建议复杂一点
设置邮箱和名称
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
# win和linux换行转换
git config --global core.autocrlf false
# 避免中文乱码
git config --global core.quotepath off
git config --global gui.encoding utf-8
删除用户
userdel -r 用户名
4.创建git仓库
mkdir -p /home/git
git init --bare /home/git/one.git
cd /home/git/
## 给git用户给予仓库权限
chown -R git:git one.git
5.禁用git用户shell登录
vim /etc/passwd
#######将git用户修改为如下(一般在最后一行)
git:x:1000:1000::/home/git:/usr/bin/git-shell
6.本地克隆项目
在想要克隆项目的目录下右键打开 Git Bash Here
#### IP地址或者域名 git仓库路径
git clone [email protected]:/home/git/one.git
#### 注意 ####
由于没有添加密匙 所以每次pull push 都需要输入上面创建git用户所输入的密码
#### 注意 ####
由于没有添加密匙 所以每次pull push 都需要输入上面创建git用户所输入的密码
7.项目代码自动更新
### 打开 hooks 目录
cd /home/git/marry.git/hooks
### cp一份 post-receive
cp post-receive.sample post-receive
### 编辑
vim post-receive
### 写入如下内容
#!/bin/sh
unset GIT_DIR
DeployPath="/home/www/one"
LogPath="/home/git/one.git/hooks"
echo -e "\n================= `date +"%Y-%m-%d %H:%M:%S"` ===============\n" >> $LogPath/gitsync.log 2>&1
cd $DeployPath
#git stash
#这里直接丢弃工作区的内容,防止出现一些奇怪的错误。web目录只做pull,不在这里修改东西
git reset --hard
#先拉取再合并
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
8.给予post-receive git用户权限及可执行权限
chown git:git post-receive
chmod +x post-receive
9.最后在线上站点目录clone一下
cd /home/www
## git仓库地址
git clone /home/git/one.git
### 打开站点目录
cd /home/www
### 给予项目one目录git用户权限
chown git:git -R one
10.免密拉取及提交
打开Git Bash
生成密匙,一路回车就可以了
ssh-keygen -t rsa -C "[email protected]"
Git服务器打开RSA认证
vim /etc/ssh/sshd_config
添加如下
RSAAuthentication yes
将下面有注释的取消注释
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
在/home/git/目录下
cd /home/git
创建.ssh文件夹 如已经存在就不需要创建
给予权限
chmod 700 /home/git/.ssh
没有这个文件就创建一个
chmod 600 /home/git/.ssh/authorized_keys
最后创建的文件请设置用户组为git
然后把本地的id_rsa.pub里面的公钥复制到/home/git/.ssh/authorized_keys里面 一行一个即可
git命令现在不需要密码了,但是git乌龟还是需要密码
git乌龟用puttygen转成ppk格式的公钥 然后提交的时候选择ppk公钥地址就可以了
11.实现线上生产和测试代码都可访问
#### linux服务器 ####
#### 创建仓库
git init --bare /home/git/one.git
#### 权限
chown -R git:git /home/git/one.git
#### 创建站点目录
mkdir -p /home/www
#### 打开
cd /home/www
#### 克隆项目
git clone /home/git/one.git
#### 权限
chown git:git -R one
#### 克隆项目 命名测试
git clone /home/git/one.git test
#### 权限
chown git:git -R test
#### 本地 ####
git clone [email protected]:/home/git/one.git
#### 放入项目代码然后提交
#### 创建本地分支
git branch test
#### 创建远程分支
git push origin test:test
#### 服务器 ####
cd /home/www/one
git pull
git checkout master
chown git:git -R one
cd /home/www/test
git pull
git checkout test
chown git:git -R test
#### 钩子文件
cd /home/git/one.git/hooks
vim post-receive
钩子文件内容
#!/bin/sh
unset GIT_DIR
#生产目录
Path="/home/www/one"
#测试目录
TestPath="/home/www/test"
#测试分支名
TestName="test"
#日志目录
LogPath="/home/git/one.git/hooks"
echo -e "\n================= `date +"%Y-%m-%d %H:%M:%S"` ===============\n" >> $LogPath/gitsync.log 2>&1
#获取分支名
read oldrev newrev ref
Branch=${ref#refs/heads/}
if [ $Branch = $TestName ]; then
#打开测试目录并更新代码
cd $TestPath
#切换分支
git checkout $Branch >> $LogPath/gitsync.log 2>&1
#这里直接丢弃工作区的内容,防止出现一些奇怪的错误。web目录只做pull,不在这里修改东西
git reset --hard
#拉取代码
git pull >> $LogPath/gitsync.log 2>&1
elif [ "$Branch" = "master" ]; then
#打开测试目录
cd $Path
#切换分支
git checkout master >> $LogPath/gitsync.log 2>&1
#这里直接丢弃工作区的内容,防止出现一些奇怪的错误。web目录只做pull,不在这里修改东西
git reset --hard
#拉取代码
git pull >> $LogPath/gitsync.log 2>&1
fi
#强制与远程服务器同步,不与本地合并,只能通过提交的客户端提交的方式修改代码。
#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
给予钩子文件权限
chown git:git post-receive
chmod +x post-receive