(注: 引入 http://blog.csdn.net/wsyw126/article/details/52167147)php
总结小点:html
一、建立linux空仓库(详见“在 Linux 下搭建 Git 服务器”);linux
二、在项目位置clone仓库,生成仓库名的项目;git
三、建立master分支(个人方式是git push origin master一个文件后就自动生成,详见“git-bash的一些操做命令”);web
四、添加hooks(我用于本地window提交到服务器的仓库后,同步项目代码)。spring
在服务器上建立一个裸仓库(git服务器上的远端仓库)bash
首先要在服务器上创建一个裸仓库,我存放裸仓库的文件夹是/home/workspace/gitbook,进入到该文件夹,而后使用git init –bare springSummary.git建立裸仓库。
在服务器上建立一个普通Git仓库服务器
在服务器上创建一个普通Git仓库用于存放网站的源代码。(web服务器上的另外一个本地仓库)post
mkdir /var/www/workspace cd /var/www/workspace git clone /home/workspace/gitbook/springSummary.git
配置Git Hookfetch
进入到/home/workspace/gitbook/springSummary.git/hooks文件夹,使用vi post-receive建立一个脚本,当你在本地仓库执行git push后就会触发post-receive。
post-receive的内容以下:
#!/bin/sh #判断是否是远端仓库 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/workspace/springSummary" echo "===============================================" cd $DeployPath echo "deploying the test web" #git stash #git pull origin master git fetch --all git reset --hard origin/master #gitbook build #sleep 15 time=`date` echo "web server pull at webserver at time: $time." echo "================================================"
保存后赋予可执行权限:
chmod +x /var/www/workspace/springSummary/hooks/post-receive
这样在开发者提交代码的时候,就会自动部署。
在这里须要解释两个问题:
gitbook build
,可是仅添加这一行的话会致使最后这个脚步执行失败,由于build须要大约8秒时间。因此个人解决方案是在脚本中添加sleep 15
,这样能够等待build执行结束并把结果返回到显示的终端。(若是只是须要自动更新项目,请把这两行指令删去)在这里我使用的是git fetch,为何没有用git pull实现。区别在于:
这里就有个问题,若是开发者在提交过程出现失误,使用git reset复位后,如今远端的代码版本低于web端的代码版本,再使用pull的时候就不能实现和开发者本地的代码的同步。因此这里使用fetch后,在强制使用reset实现web端的代码版本指针和git服务端的一致。(若是在本机能够使用stash,而后pull,而后drop刚才stash的内容)
一、git pull 报错:error: insufficient permission for adding an object to repository database .git/objects
解:找到项目里面的文件 sudo chmod 777 -R .git/objects