git服务器自动部署(自己根据自己查的资料以及部署经历总结)

1、添加钩子文件
进入对应Git仓库

cd /home/myrepo.git 

创建post-receive

touch hooks/post-receive 

使用vim打开post-receive将如下shell写入文件中

#!/bin/sh  
   
#author: embbnux  
#Blog of Embbnux: http://www.embbnux.com  
   
#判断是不是远端仓库  
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="/data/www/myrepo"  
echo "==============================================="  
cd $DeployPath  
echo "deploying the test web"  
  
#git stash  
#git pull origin master  
git fetch --all  
git reset --hard origin/master  
   
time=`date`  
echo "web server pull at webserver at time: $time."  
echo "================================================"

保存后赋予可执行权限

chmod +x hooks/post-receive

注意执行文件所有人操作chown -R git hooks/post-receive
本地仓库添加remote源

git remote add deploy git@ip:/home/myrepo.git 
git push deploy master

注意:自己踩坑了好久,作为一个Git+Linux小白,才弄明白所谓的自动部署的权限问题,当git用户与项目用户不是同一用户(基本上肯定不是同一个),这时上面部署成功,是一直提示权限问题不能更改项目文件,后来看到有人说是要将Git用户或者post-receive更改为与项目用户所在同一组,我也更改了但是还是不行,后来发现更改为同一组只是其中的一步,还需要再把项目文件所在组赋予写的权限,组权限设置[root@iZ2864ctaajZ /]# chmod g+w /data/这样就给root所在组赋予了对data文件夹的写权限,将git用户加入root所在组[root@iZ2864ctaajZ /]# usermod -a -G root git查看git所在组情况

groups git 
git : nobody root

这时再执行push操作

Counting objects: 3, done.
Writing objects: 100% (3/3), 232 bytes | 232.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: ===============================================
remote: deploying the test web
remote: 正在获取 origin
remote: 来自 /home/gitreport/coachline
remote:    7630ad1..a509e1b  master     -> origin/master
remote: HEAD 现在位于 a509e1b test1
remote: web server pull at webserver at time: 2019年 01月 16日 星期三 12:25:28 CST.
remote: ================================================

执行成功文件已同步部署到了项目文件中,在服务器项目文件中查看更新的内容,更本地一致,OK,总算搞定了!网上大部分的自动部署都是用GitHub或者其他的webhooks方式,我也是自己给自己找不痛快!尴尬啊!
Git操作命令
在现有目录中初始化仓库:git init

克隆仓库:git clone [url]

添加文件到暂存区:git add [name]

提交文件到仓库:git commit [name] -m [comment]

从远程仓库中拉取:git pull [remote-name]
拉取之前首先git fetch

推送到远程仓库:git push [origin] [master]

创建一个裸仓库(没有工作区):git init --bare [repname.git]
git提交后push报错

fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

解决方法:
第一种如上图中的提示:git push --set-upstream origin master。其中的origin是你在clone远程代码时,git为你创建的指向这个远程代码库的标签,它指向repository。为了能清楚了解你要指向的repository,可以用命令git remote -v进行查看。master是你远程的branch,可以用git branch -a查看所有分支,远程分支是红色的部分。然后确定好这两个值后,将值换掉即可。
另一种方法是:git push -u origin master。同样根据自己的需要,替换origin和master。
两个命令的区别是第一条命令是要保证你的远程分支存在,如果不存在,也就无法进行关联。而第二条指令即使远程没有你要关联的分支,它也会自动创建一个出来,以实现关联。
回退版本命令

git reset --hard 版本号 //这里的值可以通过git log -300查看最近300次记录中的提交版本号

强制提交到master_ptu分支(具体需要提交到哪个分支请酌情修改,此例为提交到master_ptu分支)

git push -f -u origin master_ptu

回退时的注意事项
执行以上脚本前 一定记得 做个 分支的备份
涉及到多个分支合并后 又想回滚代码的,请注意提交记录编号的选择,请一定选择 当前分支的提交记录编号,否则可能会回滚成其它分支的编号,例如我打算回滚到 master_ptu的某个历史版本:
git reset --hard 版本号
‘版本号’ 一定得是 直接在master_ptu上的直接提交记录编号,否则会回滚成 其它分支的某个版本。
更简单的做法(恢复上一个版本)是:

git reset --hard HEAD~1 
git add . 
git commit  -m‘roll-back’ 
git push -f origin master

最后一句强制更新,直接使用git push报错

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值