2012年3月22日 星期四

[Linux 文章收集] 初入 Git - 簡單使用範例


Preface :
Git 是 Linux Torvalds為了幫助管理Linux®內核開發而開發的一個開放源碼的版本控制軟件. 我們可以自己下載這個軟件用於對內核的hack分析,或者用來管理自己的軟件開發項目. 除此外它也是一套 opensource 的版本控制軟體,類似 CVS/SVN,但不需架設中央伺服器. 在開始使用之前, 我們先來看一張 Git 命令與其對應資料流的示意圖 :


而與之其我們熟知的 CVS/SVN, Git 它具有的優點為 :
* 沒server也能用(1),裝完後它本身就是個完整的server+client,所以沒server你也可以直接在本地端建立repo來進行commit和branch等動作.
* 沒server也能用(2),由於clone下來是完整的repo,所以也可以在本地端進行任何操作,包含roll back回之前的任何版本. 你可以儘量的改,直到有網路之後再合併回去,而這中間你還是享有source control system帶來的便利.
* 沒server也能用(3),local branch 超好用,切換上也很方便跟直覺. 而且 branch 時完全不用經過 server,每次有什麼新想法想試的時候,不用擔心在server上造成垃圾.
* 要export git file非常簡單,如果是 svn,你有一堆 .svn 目錄要處理,如果是git,你只要刪除最上層的 .git 目錄(或是别copy它)就ok了
* 可以永久刪除分支,不過這在某些方面看起來是缺點。
* 建立分享用的repo很容易 (前提是你的 sshd 是work 的). 幾個簡單的指令即可完成.

當然沒有十全十美的工具, 我們來看看美中不足的地方 :
* 由 source code 開始安裝有夠麻煩,公司的 server 就是從 source code 開始裝,缺東缺西,裝到快抓狂. 不過如果你是OSX、Windows這類的OS,那安裝起來還蠻快樂的.
* 有學習曲線,因為全都是命令列指令,要用起來有點好用,沒有練個幾次是很難的. 不過目前已經有一些 GUI 版本的git了,但是功能聽說不是很完整, 得靠命令列指令補助.
* 對 SVN 的支援非常的鳥,本來這是個整合用的賣點,不過實際測試的結果,非常的不安定與缺乏功能,首先重要的svn:extern tag是沒有支援的. 再來,用 git commit 時常發生意外,曾經有該 commit 進去SVN的檔案遇過沒有 commit 進去以及內容亂掉的問題. 建議還是不要跟 SVN 混用會比較好.


實戰過程 :
接著我們就來實際用用看 Git , 第一步就是先安裝它, 這裡我使用的平台為 Ubuntu, 所以直接用 apt-get 進行安裝 :
# sudo apt-get install git-core

接著我們需要一個路徑當作 Repository, 假設是 /home/john/GitRepos :
$ mkdir GitRepos
$ cd GitRepos/
~/GitRepos$ git init
Initialized empty Git repository in /home/john/GitRepos/.git/
$ ls -al
...(略)...
drwxr-xr-x 7 john john 4096 2012-03-22 05:54 .git

接著我們來建立檔案並提交 :
$ vi README # 編輯並添加內容 "Hi John"
Hi John

$ git add . # 將當前路徑下所有檔案進行快照
$ git config --global user.name "John K Lee" # 設置預設提交用戶
$ git config --global user.email puremonkey2001@yahoo.com.tw # 設置用戶 mail
$ git commit -a -m "Add README" # 提交檔案 README 變更. 並為該變更進行註解
$ git log # 檢視提交紀錄
commit 4dd76638079965d8299bca849c17deab77ef2d52
Author: john
Date: Thu Mar 22 06:44:30 2012 -0700

Add README

接著我們繼續變更 README :
$ vi README
Hi John.
This is the new line!

$ git diff master # 接著我們可以檢視目前已 Commit 與目前變更紀錄的差異.
diff --git a/README b/README
index 0fd46a7..b878a23 100644
--- a/README
+++ b/README
@@ -1 +1,2 @@
-Hi John
+Hi John.
+This is the new line!

接著我們可以為當前 Commit 添加標籤, 並可以在不同的標籤進行 diff 比對 :
$ git tag v1 # 為當前庫建立標籤 v1
$ git commit -a -m "README CHANGE" # 將剛剛變更的 README 提交變更.
[master 48d467b] README CHANGE
1 files changed, 2 insertions(+), 1 deletions(-)
$ git tag v2 # 為當前庫建立標籤 v2
$ git diff v1 v2 # 對標籤 v1 與 v2 進行 diff 比對
diff --git a/README b/README
index 0fd46a7..b878a23 100644
--- a/README
+++ b/README
@@ -1 +1,2 @@
-Hi John
+Hi John.
+This is the new line!

接著我們繼續變更 README, 但要將變更回復 :
$ vi README
Hi John.
This is the new line!
This will be reset

$ git checkout README # 回復到庫原始的狀態
$ cat README
Hi John.
This is the new line!

存取遠端 Repository :
到目前為止都是在本地端操作, 當你的專案需要多人協同開發時, 勢必需要一個 Repository Server. 這邊以 github 當作範例進行說明如何 commit 到遠端與從遠端 check out. 在開始前請先申請一個帳號並建立 Repository. 這邊我使用我的帳號建立一個 Repository 為 Gitutorial. 因為 commit/check out 都是透過 sshd 進行, 第一步必須先建立你本地端的公鑰給 Repository Server 使用 :
- 建立 RSA Key
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/john/.ssh/id_rsa): # 按下 Enter
Created directory '/home/john/.ssh'.
Enter passphrase (empty for no passphrase): # 按下 Enter
Enter same passphrase again: # 按下 Enter
Your identification has been saved in /home/john/.ssh/id_rsa.
Your public key has been saved in /home/john/.ssh/id_rsa.pub.
The key fingerprint is:
af:40:28:d8:a5:bc:ad:cb:41:31:a2:2e:31:96:ec:68 john@ubuntu
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
|. o . |
|o=.= . |
|=+* . . S |
|=+ + . . |
|oEo . . . |
|o. o . . |
| +. . |
+-----------------+
$ cd ~/.ssh
$ ls
id_rsa id_rsa.pub
$ cat id_rsa.pub
ssh-rsa ...(略)...== john@ubuntu

- 添加 Public key 到 Repository Server
如下將你剛剛建立的 Public key 內容添加到 Repository Server.


接著你可以進入你剛剛建立的 Repository 頁面 :


然後按照頁面上面步驟, 一步步就可以建立本地端的 Repository 與 Server 端 Repository 的 Push, Checkout :
$ mkdir Gitutorial
$ cd Gitutorial/
~/Gitutorial$ git init # 初始目前路徑為 Git 本地端 repository
Initialized empty Git repository in /home/john/Gitutorial/.git/
~/Gitutorial$ vi README
This is for git tutorial

~/Gitutorial$ git add README # 添加 README 到 git
~/Gitutorial$ git commit -m "first commit" # 提交變更到本地端 Repository
[master (root-commit) f028a7b] first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README
~/Gitutorial$ git remote add origin git@github.com:johnklee/Gitutorial.git # 加入遠端 Repository
~/Gitutorial$ git push -u origin master # 將本地端 Repository push 到遠端 origin 的 Repository
Counting objects: 3, done.
Writing objects: 100% (3/3), 241 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:johnklee/Gitutorial.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

接著再回到 github 的 Repository 並 Refresh 頁面, 便可以看到剛剛 push 出去的檔案 :


Supplement :
slide share : 寫給大家的 Git 教學
Tsung's Blog : Git 初學筆記 - 指令操作教學
gittutorial(7) Manual Page
JOSEPH.J Blob : Git Study
Git 跟我過去所用的 CVS、Perforce、SVN 在架構上非常不一樣!之前我用的都是一台 Server 做 Repository,且所有的動作 (Update/Commit/Add...都得跟它做連線;而 Git 的 Client 本身就是 Repositiory,每次的 Commit 都是在 Local 做. 問題是... 這樣的架構,該如何做到多人同時開發呢?原來 Git 的每個 Repository 都可透過 Publish / Pull Request 的動作讓其他人做 Merge。簡單來說,CVS / SVN 是以 Trunk 為主、Branch 為輔,而 Git 的主要架構就是以 Branch / Merge 為核心

This message was edited 26 times. Last update was at 23/03/2012 10:49:21

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...