天天看點

git的日常應用

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 -&gt; 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    -&gt; 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,如需轉載請自行聯系原作者