先裝 SSH 工具,方便登入與金鑰認證:
$ sudo apt-get install openssh-client
$ sudo apt-get install openssh-server
再裝 Git,並建立專用帳號做權限控管:
$ sudo apt-get install git git-core
$ sudo useradd git
$ sudo passwd git
用專用帳號而非 root 跑服務,是為了在需要開放協作時能把權限範圍壓到最小。
$ mkdir -p gitRepo/project.git
$ git --bare init gitRepo/project.git
--bare 建立的是沒有工作目錄的裸倉庫,這正是伺服器端該有的形式。
$ cd project
$ git init
$ git add .
$ git commit -am "init the repo"
$ git remote add origin user@ip:/path/to/project.git
$ git push origin master
若遠端路徑填錯,可用 git config 修正:
$ git config remote.origin.url # 檢視目前路徑
$ git config remote.origin.url user@ip:/path/to/project.git # 變更路徑
其他人直接 clone 即可開始:
$ git clone user@ip:/path/to/project.git local_project_name
多使用者共用同一個 git 帳號時,可在他們的 ~/.ssh/authorized_keys 各自加入公鑰,並在每行末尾用 command="git-shell -c \"$SSH_ORIGINAL_COMMAND\"" 限制該金鑰只能執行 Git 操作。若倉庫需要對外,強烈建議改用 SSH 金鑰認證並關閉 git 帳號的密碼登入。