1、Git的介紹及安裝
Git:全宇宙最牛的分布式版本控制軟體,Git是目前世界上最先進的分布式版本控制系統
<code>#CentOS7下git的安裝</code>
<code>[root@ip-172-31-22-8 ~]</code><code># yum -y install git</code>
<code>#設定git賬号資訊</code>
<code>[root@ip-172-31-22-8 ~]</code><code># git config --global user.name "molewan"</code>
<code>[root@ip-172-31-22-8 ~]</code><code># git config --global user.email "[email protected]"</code>
<code>a)因為Git是分布式版本控制系統,是以,每個機器都必須報家:你的名字和Email位址。你也許</code>
<code>會擔,如果有故意冒充别怎麼辦?這個不必擔,先我們相信家都是善良</code>
<code>知的群衆,其次,真的有冒充的也是有辦法可查的。</code>
<code>b)注意git config指令的--global參數,了這個參數,表你這台機器上所有的Git倉庫都會使</code>
<code>這個配置,當然也可以對某個倉庫指定不同的戶名和Email位址。</code>
2、檢視git的配置:
<code>[root@ip-172-31-22-8 ~]</code><code># git config --list</code>
<code>user.name=molewan</code>
<code>[email protected]</code>
3、為git配置顔色
<code>[root@ip-172-31-22-8 ~]</code><code># git config --global color.ui true</code>
<code>color.ui=</code><code>true</code>
4、建立一個本地的git庫
<code>[root@ip-172-31-22-8 ~]</code><code># mkdir molewan</code>
<code>[root@ip-172-31-22-8 ~]</code><code># cd molewan</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># ll</code>
<code>total 0</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># git init</code>
<code>Initialized empty Git repository </code><code>in</code> <code>/root/molewan/</code><code>.git/</code>
<code>說明:倉庫初始化一個git倉庫,使用git init指令,将這個目錄變成git可以管理的</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># ls -la</code>
<code>total 8</code>
<code>drwxr-xr-x. 3 root root 17 Nov 26 17:04 .</code>
<code>dr-xr-x---. 12 root root 4096 Nov 26 17:04 ..</code>
<code>drwxr-xr-x. 7 root root 4096 Nov 26 17:04 .git</code>
5、将檔案添加到版本庫:
将一個檔案放到Git倉庫隻需要兩步:
a)git add告訴git,将文本添加到倉庫
b)用git commit告訴git,把檔案送出到倉庫
<code>[root@ip-172-31-22-8 molewan]</code><code># vim readme.txt</code>
<code>You have new mail </code><code>in</code> <code>/var/spool/mail/root</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># cat readme.txt </code>
<code>hehe</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># git status</code>
<code># On branch master</code>
<code>#</code>
<code># Initial commit</code>
<code># Untracked files:</code>
<code># (use "git add <file>..." to include in what will be committed)</code>
<code>#readme.txt</code>
<code>nothing added to commit but untracked files present (use </code><code>"git add"</code> <code>to track)</code>
<code>添加一個新檔案,readme.txt</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># git add readme.txt </code>
<code># Changes to be committed:</code>
<code># (use "git rm --cached <file>..." to unstage)</code>
<code>#new file: readme.txt</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># git commit -m "the first commity"</code>
<code>[master (root-commit) 24a5b30] the first commity</code>
<code> </code><code>1 </code><code>file</code> <code>changed, 1 insertion(+)</code>
<code> </code><code>create mode 100644 readme.txt</code>
<code>說明,commit -m 後面的内容隻是針對本次送出的一個描述</code>
<code>nothing to commit, working directory clean</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># vim deply.sh</code>
<code>#deply.sh</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># git add deply.sh </code>
<code>[root@ip-172-31-22-8 molewan]</code><code># git commit -m "2th commit"</code>
<code>[master f30d737] 2th commit</code>
<code> </code><code>1 </code><code>file</code> <code>changed, 2 insertions(+)</code>
<code> </code><code>create mode 100644 deply.sh</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># ls -l</code>
<code>-rw-r--r--. 1 root root 22 Nov 26 17:15 deply.sh</code>
<code>-rw-r--r--. 1 root root 5 Nov 26 17:07 readme.txt</code>
6、 使用git log進行檢視
<code>[root@ip-172-31-22-8 molewan]</code><code># git log</code>
<code>commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8</code>
<code>Author: molewan <[email protected]></code>
<code>Date: Sat Nov 26 17:16:27 2016 -0500</code>
<code> </code><code>2th commit</code>
<code>commit 24a5b3094f55e815ad46561f4b741c23bc7ad371</code>
<code>Date: Sat Nov 26 17:13:37 2016 -0500</code>
<code> </code><code>the first commity</code>
7、git diff進行對比
<code>[root@ip-172-31-22-8 molewan]</code><code># vim readme.txt </code>
<code># 添加一行文字,hehe</code>
<code># Changes not staged for commit:</code>
<code># (use "git add <file>..." to update what will be committed)</code>
<code># (use "git checkout -- <file>..." to discard changes in working directory)</code>
<code>#modified: readme.txt</code>
<code>no changes added to commit (use </code><code>"git add"</code> <code>and</code><code>/or</code> <code>"git commit -a"</code><code>)</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># git diff readme.txt </code>
<code>diff</code> <code>--git a</code><code>/readme</code><code>.txt b</code><code>/readme</code><code>.txt</code>
<code>index 91ca0fa..197cac2 100644</code>
<code>--- a</code><code>/readme</code><code>.txt</code>
<code>+++ b</code><code>/readme</code><code>.txt</code>
<code>@@ -1 +1,2 @@</code>
<code> </code><code>hehe</code>
<code>+hehe</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># git commit -m "add 2hehe"</code>
<code>[master c33cc4f] add 2hehe</code>
<code>commit c33cc4f3c6a1dc6741bce035de920f5a17c82b4b</code>
<code>Date: Sat Nov 26 17:22:05 2016 -0500</code>
<code> </code><code>add 2hehe</code>
<code>:...skipping...</code>
小結:
<code>a)所有的版本控制系統,其實隻能跟蹤本件的改動,如TXT件,網頁,所有的程式代碼等等,Git也不</code>
<code>例外。</code>
<code>b)版本控制系統 可以告訴你每次的改動,如在第5加了個單詞“Linux”删了個單詞“Windows”</code>
<code>c)圖檔視訊這些二進制檔案,雖然也能由版本 控制系統管理,但沒法跟蹤件的變化,隻能把二進制文</code>
<code>件每次改動串起來,也就是隻知道圖從100KB改成了120KB,但到底改了啥,版本控制系統不知道,沒法</code>
<code>知道。</code>
<code>d)不幸的是,Microsoft的Word格式是進制格式,是以,版本控制系統是沒法跟蹤Word件的改動的,</code>
<code>我們舉的例隻是為了示範,真正使版本控制系統,就要以純本式編寫件。</code>
8、版本回退(回退到上一個版本)
<code>[root@ip-172-31-22-8 molewan]</code><code># git reset --hard HEAD^</code>
<code>HEAD is now at f30d737 2th commit</code>
<code># 說明:HEAD^代表上一個版本,^^代表上上個版本</code>
9、回退到指定的某個版本
<code>[root@ip-172-31-22-8 molewan]</code><code># git reflog</code>
<code>f30d737 HEAD@{0}: reset: moving to HEAD^</code>
<code>c33cc4f HEAD@{1}: commit: add 2hehe</code>
<code>f30d737 HEAD@{2}: commit: 2th commit</code>
<code>24a5b30 HEAD@{3}: commit (initial): the first commity</code>
<code>[root@ip-172-31-22-8 molewan]</code><code># git reset --hard 24a5b30</code>
<code>HEAD is now at 24a5b30 the first commity</code>
<code>total 4</code>
<code>-rw-r--r--. 1 root root 5 Nov 26 17:25 readme.txt</code>
<code>說明:Git的版本回退速度常快,因為Git在内部有個指向目前版本的HEAD指針,當你回退版本</code>
<code>的時候,Git僅僅是把HEAD從指向“append GPL”</code>
<code>a)HEAD指向的版本就是目前版本,是以,Git允許我們在版本的曆史之間穿梭,使指令</code>
<code>git reset --hard commit_id。</code>
<code>b)整理、排版: numbbbbb 穿梭前,git log可以檢視送出曆史,以便确定要回退到哪個版本。</code>
<code>c) 要重返未來,git reflog檢視指令曆史,以便确定要回到未來的哪個版本</code>
10、工作區、版本庫、暫存區:
工作區:就是你在電腦能看到的目錄,如我的某個檔案夾
版本庫(Repository):作區有個隐藏目錄“.git”,這個不算作區,是Git的版本庫。
暫存區:Git的版本庫存了很多東,其中最重要的就是稱為stage(或者叫index)的暫存區,還有Git為我們動建立的第一個分支master,以及指向master的個指針叫HEAD。
<code>[root@LVS-DR01 git-data]</code><code># pwd</code>
<code>/git-data</code><code>#工作區</code>
<code>[root@LVS-DR01 git-data]</code><code># ls -la</code>
<code>total 40</code>
<code>drwxr-xr-x 3 root root 119 Jun 29 11:22 .</code>
<code>dr-xr-xr-x. 19 root root 281 Jun 29 09:32 ..</code>
<code>-rw-r--r-- 1 root root 10 Jun 29 11:20 dev.txt</code>
<code>drwxr-xr-x 8 root root 201 Jun 29 11:21 .git</code><code>#版本庫</code>
<code>-rw-r--r-- 1 root root 1045 Jun 29 11:20 .gitignore</code>
<code>-rw-r--r-- 1 root root 8870 Jun 29 11:20 git.txt</code>
<code>-rw-r--r-- 1 root root 11357 Jun 29 11:14 LICENSE</code>
<code>-rw-r--r-- 1 root root 25 Jun 29 11:20 README.md</code>
<code>-rw-r--r-- 1 root root 1774 Jun 29 11:20 談運維.txt</code>
<code>需要先add,才能commit</code>
<code>說明:要掌握工作區的狀态,使用git status指令,而用git </code><code>diff</code><code>可以檢視修改内容</code>
本文轉自 冰凍vs西瓜 51CTO部落格,原文連結:http://blog.51cto.com/molewan/1943143,如需轉載請自行聯系原作者