1.代码部署在内网git服务器上,简称git服务器。
2.个人办公机器,简称个人电脑
3.线上服务器
4.个人电脑通过ssh公钥联通git服务器以及线上服务器
需求:通过个人电脑把代码发布更新到线上服务器
一.个人电脑上的git操作。
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com$ </code><code>mkdir</code> <code>www.gits</code>
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com$ </code><code>cd</code>
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits$ git init</code>
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits$ git status</code>
<code>On branch master</code>
<code>Initial commit</code>
<code>nothing to commit (create</code><code>/copy</code> <code>files and use </code><code>"git add"</code> <code>to track)</code>
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits$ ll -a</code>
<code>total 12</code>
<code>drwxrwxr-x 3 huwei huwei 4096 7月 24 14:14 ./</code>
<code>drwxrwxr-x 4 huwei huwei 4096 7月 24 14:14 ../</code>
<code>drwxrwxr-x 7 huwei huwei 4096 7月 24 14:21 .git/</code>
第一个要配置的是你个人的用户名称和电子邮件地址。这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,所以会随更新内容一起被永久纳入历史记录:
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits$ git config --global user.name </code><code>"weihu"</code>
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits$ git config --global user.email </code><code>"[email protected]"</code>
查看配置信息
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits$ git config --list </code>
<code>user.name=weihu</code>
<code>[email protected]</code>
<code>core.repositoryformatversion=0</code>
<code>core.filemode=</code><code>true</code>
<code>core.bare=</code><code>false</code>
<code>core.logallrefupdates=</code><code>true</code>
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits$ git clone [email protected]:kutongji</code><code>/www_old</code><code>.git</code>
<code>Cloning into </code><code>'www_old'</code><code>...</code>
<code>remote: Counting objects: 31397, </code><code>done</code><code>.</code>
<code>remote: Compressing objects: 100% (8608</code><code>/8608</code><code>), </code><code>done</code><code>.</code>
<code>remote: Total 31397 (delta 20722), reused 31300 (delta 20671)</code>
<code>Receiving objects: 100% (31397</code><code>/31397</code><code>), 63.94 MiB | 10.93 MiB</code><code>/s</code><code>, </code><code>done</code><code>.</code>
<code>Resolving deltas: 100% (20722</code><code>/20722</code><code>), </code><code>done</code><code>.</code>
<code>Checking connectivity... </code><code>done</code><code>.</code>
<code>total 16</code>
<code>drwxrwxr-x 4 huwei huwei 4096 7月 24 14:31 ./</code>
<code>drwxrwxr-x 4 huwei huwei 4096 7月 24 14:14 ../</code>
<code>drwxrwxr-x 7 huwei huwei 4096 7月 24 14:21 .git/</code>
<code>drwxrwxr-x 10 huwei huwei 4096 7月 24 14:31 www_old/</code>
其中www_old文件为本地git服务器文件,即开发环境。
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits$ </code><code>cd</code> <code>www_old/</code>
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits</code><code>/www_old</code><code>$ ll -a</code>
<code>total 48</code>
<code>drwxrwxr-x 10 huwei huwei 4096 7月 24 14:31 ./</code>
<code>drwxrwxr-x 4 huwei huwei 4096 7月 24 14:31 ../</code>
<code>drwxrwxr-x 9 huwei huwei 4096 7月 24 14:31 application/</code>
<code>drwxrwxr-x 6 huwei huwei 4096 7月 24 14:31 data/</code>
<code>drwxrwxr-x 4 huwei huwei 4096 7月 24 14:31 docs/</code>
<code>drwxrwxr-x 8 huwei huwei 4096 7月 24 14:31 .git/</code>
<code>-rw-rw-r-- 1 huwei huwei 137 7月 24 14:31 .gitignore</code>
<code>drwxrwxr-x 3 huwei huwei 4096 7月 24 14:31 library/</code>
<code>drwxrwxr-x 7 huwei huwei 4096 7月 24 14:31 public/</code>
<code>-rwxrwxr-x 1 huwei huwei 1335 7月 24 14:31 README.md*</code>
<code>drwxrwxr-x 4 huwei huwei 4096 7月 24 14:31 scripts/</code>
<code>drwxrwxr-x 3 huwei huwei 4096 7月 24 14:31 tests/</code>
<code>huwei@huwei:</code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits</code><code>/www_old</code><code>$ git remote -</code><code>v</code>
<code>origin [email protected]:kutongji</code><code>/www_old</code><code>.git (fetch)</code>
<code>origin [email protected]:kutongji</code><code>/www_old</code><code>.git (push)</code>
二.线上服务器操作
思路:线上服务器创建两个git目录A,B。其中A为git裸库,负责接受个人电脑的推送以及更新
目录B为代码发布区,clone目录A,在裸库更新后通过git pull获取更新,实现代码更新以及回滚。
<code>git init --bare</code>
创建裸库的操作
<code>[root@localhost www.gits]</code><code># git init --bare</code>
<code>Initialized empty Git repository </code><code>in</code> <code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits/</code>
<code>[root@localhost www.gits]</code><code># ll -a</code>
<code>total 40</code>
<code>drwxr-xr-x. 7 root root 4096 7月 24 14:43 .</code>
<code>drwxr-xr-x. 4 root root 4096 7月 24 14:40 ..</code>
<code>drwxr-xr-x. 2 root root 4096 7月 24 14:43 branches</code>
<code>-rw-r--r--. 1 root root 66 7月 24 14:43 config</code>
<code>-rw-r--r--. 1 root root 73 7月 24 14:43 description</code>
<code>-rw-r--r--. 1 root root 23 7月 24 14:43 HEAD</code>
<code>drwxr-xr-x. 2 root root 4096 7月 24 14:43 hooks</code>
<code>drwxr-xr-x. 2 root root 4096 7月 24 14:43 info</code>
<code>drwxr-xr-x. 4 root root 4096 7月 24 14:43 objects</code>
<code>drwxr-xr-x. 4 root root 4096 7月 24 14:43 refs</code>
<code>[root@localhost www.gits]</code><code># git config --global user.name "weihu"</code>
<code>[root@localhost www.gits]</code><code># git config --global user.email "[email protected]"</code>
<code>[root@localhost www.gits]</code><code># git config --list</code>
<code>core.bare=</code><code>true</code>
在个人电脑目录上操作
<code>切换到代码目录:</code>
<code>huwei@huwei:/web/www.kutongji.com/www.gits/www_old$ cd ..</code>
<code>huwei@huwei:/web/www.kutongji.com/www.gits$ cd www_old/</code>
<code>查看目录关联的远程机,很明显因为从git服务器上clone,因此目录里面有一个主机是origin表示git主机</code>
<code>huwei@huwei:/web/www.kutongji.com/www.gits/www_old$ git remote -v</code>
<code>origin [email protected]:kutongji/www_old.git (fetch)</code>
<code>origin [email protected]:kutongji/www_old.git (push)</code>
<code>添加线上服务器主机,其中root@</code><code>192.168</code><code>.</code><code>66.35</code><code>:/web/www.kutongji.com/www.gits为裸库git地址,kutongji为一个主机别名,并非真实主机名称</code>
<code>huwei@huwei:/web/www.kutongji.com/www.gits$ git remote add kutongji root@</code><code>192.168</code><code>.</code><code>66.35</code><code>:/web/www.kutongji.com/www.gits</code>
<code>kutongji root@</code><code>192.168</code><code>.</code><code>66.35</code><code>:/web/www.kutongji.com/www.gits (fetch)</code>
<code>kutongji root@</code><code>192.168</code><code>.</code><code>66.35</code><code>:/web/www.kutongji.com/www.gits (push)</code>
<code>origin [email protected]:kutongji/www_old.git (push) </code>
<code>查看本地目录的分支</code>
<code>huwei@huwei:/web/www.kutongji.com/www.gits/www_old$ git branch </code>
<code>* develop</code>
<code>推送本地分支到线上服务器裸库</code>
<code>huwei@huwei:/web/www.kutongji.com/www.gits/www_old$ git push kutongji develop </code>
<code>Counting objects: </code><code>31357</code><code>, done.</code>
<code>Delta compression using up to </code><code>2</code> <code>threads.</code>
<code>Compressing objects: </code><code>100</code><code>% (</code><code>8517</code><code>/</code><code>8517</code><code>), done.</code>
<code>Writing objects: </code><code>100</code><code>% (</code><code>31357</code><code>/</code><code>31357</code><code>), </code><code>63.93</code> <code>MiB | </code><code>10.19</code> <code>MiB/s, done.</code>
<code>Total </code><code>31357</code> <code>(delta </code><code>20722</code><code>), reused </code><code>31357</code> <code>(delta </code><code>20722</code><code>)</code>
<code>To root@</code><code>192.168</code><code>.</code><code>66.35</code><code>:/web/www.kutongji.com/www.gits</code>
<code> </code><code>* [</code><code>new</code> <code>branch] develop -> develop</code>
<code>推送成功</code>
<code>切换到线上服务器</code>
<code>[root@localhost www.kutongji.com]</code><code># ll -a</code>
<code>drwxr-xr-x. 4 root root 4096 7月 24 14:40 .</code>
<code>drwxr-xr-x. 3 root root 4096 7月 24 14:40 ..</code>
<code>drwxr-xr-x. 7 root root 4096 7月 24 14:43 www.gits</code>
<code>drwxr-xr-x. 2 root root 4096 7月 24 14:40 wwwroot</code>
<code>[root@localhost www.kutongji.com]</code><code># cd wwwroot/</code>
<code>配置git库以及个人信息</code>
<code>[root@localhost wwwroot]</code><code># git init</code>
<code>Initialized empty Git repository </code><code>in</code> <code>/web/www</code><code>.kutongji.com</code><code>/wwwroot/</code><code>.git/</code>
<code>[root@localhost wwwroot]</code><code># git config --global user.name "weihu"</code>
<code>[root@localhost wwwroot]</code><code># git config --global user.email "[email protected]"</code>
<code>[root@localhost wwwroot]</code><code># git config --list</code>
使用git remote add添加主机
<code>[root@localhost wwwroot]</code><code># git remote add kutongji /web/www.kutongji.com/www.gits</code>
<code>[root@localhost wwwroot]</code><code># git remote -v</code>
<code>kutongji </code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits (fetch)</code>
<code>kutongji </code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits (push)</code>
更新代码
<code>[root@localhost wwwroot]</code><code># git pull kutongji develop</code>
<code>remote: Counting objects: 31357, </code><code>done</code><code>.</code>
<code>remote: Compressing objects: 100% (8517</code><code>/8517</code><code>), </code><code>done</code><code>.</code>
<code>remote: Total 31357 (delta 20722), reused 31357 (delta 20722)</code>
<code>Receiving objects: 100% (31357</code><code>/31357</code><code>), 63.93 MiB | 18.00 MiB</code><code>/s</code><code>, </code><code>done</code><code>.</code>
<code>From </code><code>/web/www</code><code>.kutongji.com</code><code>/www</code><code>.gits</code>
<code> </code><code>* branch develop -> FETCH_HEAD</code>
查看是否成功
<code>[root@localhost wwwroot]</code><code># ll -a</code>
<code>drwxr-xr-x. 10 root root 4096 7月 24 15:26 .</code>
<code>drwxr-xr-x. 4 root root 4096 7月 24 15:23 ..</code>
<code>drwxr-xr-x. 9 root root 4096 7月 24 15:26 application</code>
<code>drwxr-xr-x. 6 root root 4096 7月 24 15:26 data</code>
<code>drwxr-xr-x. 4 root root 4096 7月 24 15:26 docs</code>
<code>drwxr-xr-x. 8 root root 4096 7月 24 15:26 .git</code>
<code>-rw-r--r--. 1 root root 137 7月 24 15:26 .gitignore</code>
<code>drwxr-xr-x. 3 root root 4096 7月 24 15:26 library</code>
<code>drwxr-xr-x. 7 root root 4096 7月 24 15:26 public</code>
<code>-rwxr-xr-x. 1 root root 1335 7月 24 15:26 README.md</code>
<code>drwxr-xr-x. 4 root root 4096 7月 24 15:26 scripts</code>
<code>drwxr-xr-x. 3 root root 4096 7月 24 15:26 tests</code>
本文转自 yawei555 51CTO博客,原文链接:http://blog.51cto.com/huwei555/1678030,如需转载请自行联系原作者