<p>本地版本库中存放开发的项目代码。以下内容介绍如何将本地版本库修改通过执行"git push web"命令同步到web站点目录。</p><p>1、首先创建本地版本库:</p><pre class="brush:bash;toolbar:false">
$mkdirwebsite&&cdwebsite
$gitinit
InitializedemptyGitrepositoryin/home/ams/website/.git/
$echo'Hello,world!'>index.html
$gitaddindex.html
$gitcommit-q-m"Thehumblebeginningsofmywebsite."</pre><p>本地版本库中包含需要同步到web网站的相关代码。</p><p>2、远程版本库</p><p>假设web站点建立在远程服务器上。在远程服务器上,创建一个镜像版本库:</p><pre class="brush:bash;toolbar:false">$mkdirwebsite.git&&cdwebsite.git
$gitinit--bare
InitializedemptyGitrepositoryin/home/ams/website.git/</pre><p>然后,定义一个post-receive钩子文件,利用post-receive,检测最新更新,并同步最新更新到web根目录。post-receive文件内容如下:</p><pre class="brush:bash;toolbar:false">$mkdir/var/www/www.example.org
$cat>hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.orggitcheckout-f
$chmod+xhooks/post-receive
/var/www/www.example.org:此目录为web站点根目录</pre><p>3、本地版本库</p><p>为本地版本库添加远程版本库路径,并指定push分支:</p><pre class="brush:bash;toolbar:false">$gitremoteaddwebssh://server.example.org/home/ams/website.git
$gitpushweb+master:refs/heads/master</pre><p>4、查看远程服务器web根目录,可以看到新建的index.html文件。</p><p></p><p>5、更新操作</p><p>在本地版本库,执行以下命令,既可更新远程服务器的web根目录:</p><pre class="brush:bash;toolbar:false">gitpushweb</pre>