git 的使用筆記
git 是一個好用的版本控制軟體,可以讓軟體工程師方便的進行軟體的協作。git在網路上有很多安裝的教學,這篇要介紹的是如何在server端建立一個專案,以及如何在client端下載。
server端:
假設你建立了一個專案(例如project_1),先進入專案的資料夾底下
cd project_1
然後執行init的動作
git init
執行完之後,會在此目錄底下產生一個.git的檔案。目前這個檔案裡還沒有任何的資料。需要手動將此專案的程式碼加入git裡
git add .
git commit -m “Initial Commit”
client端:
在client端,示範使用ssh連線的例子。因為ssh是server端較為普遍支援的通訊協定。
git clone user_name@server_host.com:project_1 -key ssh.key
此處的project_1需要給出家目錄下的相對位置。例如project_1在/home/user1/c_project/project_1底下,指令就會是
git clone user1@server_host.com:c_project/project_1 -key ssh.key
另外,有關ssh的設定,預設會在~/.ssh/config檔裡。所以如果不想每次都要設定user name跟server的ip位址,可以將設定寫在~/.ssh/config之中
vi ~/.ssh/config
然後在檔案裡加上
host give_a_name
user user_name_of_server_for_login
hostname server_ip port server_port_number
identityfile location_of_ssh_key之後連線便可簡化輸入
git clone give_a_name:project_1
補充 : server端在執行完initial commit之後,需要checkout到新的branch,否則client端要push code的時候,git會認為server端還在work branch上的資料,避免資料覆蓋,git會拒絕此次的push。