Git 用 archive 和 checkout 匯出原始碼,並用 export-ignore 排除指定檔案

2015-09-10 Git

Git 本身的 Git repository 裸庫所儲存的是打包過的原始碼,並不能拿來當作 Web 使用

 

Git 有幾種方式可以將打包過的程式匯出成原始碼,來讓 WebServer 可以即時同步 Git 的程式碼內容

 

 

以下的實作都是在 Git repository 下動作。

Git 目錄:/home/git

Git 專案目錄:/home/git/system

WebRoot 目錄:/usr/share/nginx/html/dev.system

 

 

git archive master (推薦) 匯出 master 檔案

git archive master 是標準匯出主線版本(master)的方式,並且在這個方法將會提供排除指定檔案匯出的功能

採用 Git hooks (掛勾) 來執行匯出的動作

 

Step.1 在 repository 加入 attributes 並設定以下

$ cd /home/git/system/hooks
$ mv post-receive.sample post-receive
$ chmod +x post-receive
$ vim post-receive

git archive master | tar -x -C /usr/share/nginx/html/dev.system

archive 預設是用 tar 打包,所以用 | 來解開到指定的 WebRoot

 

Step.2 這個步驟將用 attributes 來排除要匯出的檔案或資料夾,如不需要可以略過此步驟。

$ vim /home/git/system/info/attributes

test export-ignore
test.php export-ignore

export-ignore 標示為此檔案匯出時略過

 

Step.3 測試 commit 並 push repository,原始碼將匯出到 /usr/share/nginx/html/dev.system。

 

 

 

 

git checkout HEAD 匯出最後一次 commit 檔案

也是採用 Git hooks (掛勾) 將最後一次 commit 的版本 checkout 出來,你也可以使用 git checkout -f

 

Step.1 設定 config

預設 git repository 是裸庫(不含工作目錄),所以要加入 worktree 讓他有工作目錄,也就是 WebRoot 位置。

$ vim /home/git/system/config

    [core]
        repositoryformatversion = 0
        filemode = true
-	bare = true
+       bare = false
+       worktree=/usr/share/nginx/html/dev.system
+   [receive]
+       denycurrentbranch = ignore

 

Step.2 加入 post-receive 掛勾,這個掛勾會讓你 push 到 git repository 後最後做的動作。

在 post-receive 加入 git checkout -f 並讓他有 x 執行權限

$ cd /home/git/system/hooks
$ mv post-receive.sample post-receive
$ chmod +x post-receive
$ vim post-receive

git checkout -f

 

Step.3 測試 commit 並 push repository,原始碼將匯出到 /usr/share/nginx/html/dev.system。

 

 

 

checkout 和 archive 本身還有許多強大的功能,可以進行比對或是判斷,這邊只是實現其中一小部分功能。

 

*上面兩種方式必須注意的是由 Git 匯出的檔案都是 git 權限,這並不符合正式環境使用,若要採用 nginx 或 apache 來更新 git 這對於資安也是有疑慮,以上僅供測試環境驗證使用。

 

 

參考資料:

7.2 Git 客製化 – Git 屬性

給 Mr. 沙先生一點建議

彙整

分類

展開全部 | 收合全部

License

訂閱 Mr. 沙先生 的文章

輸入你的 email 用於訂閱