Git

Git

2020/07/30 (更新內容)
2021/08/13 (新增連結)

基本概念

Git是個分散式版本管理系統 (Version Control System),所以,使用Git的使用者都會擁有一份完整的程式碼。Git本身並沒有權限管控,所以,一般而言,會利用git server來協助權限管控。如果不想自己架設git server,可以使用git雲端服務。常用的git雲端服務有:githubgitlabbitbucket。這三個雲端服務的主要差別在於在github免費的專案都必須是公開的,所以,目前大部分的open source專案都可以在github上找到。在gitlab上,目前不管是公開或私密的專案都是免費的。在bitbucket上,允許5個成員以下的小專案(不管是公開或私密)免費使用 (2017/4/11起提供教育單位免費免限制的使用)。

當使用git時,我們會有本地資料庫(local repository)及遠端資料庫(remote repository),通常我們會把遠端資料庫(remote repository)設在git server上。

  • Git (王曉瑜的git筆記) 使用github **

Git - vscode.ppt
  • Git

  • Gitlab新增專案

  • 下載git for windows

  • 啟動專案

    • git init / git clone

  • Staged Changes/Commit/Push

  • 解決衝突

  • Branch

使用步驟

首先,要在git server上建帳號 並開好repository及設定好repository的權限,通常,每個repository會有一個URL,利用這個URL來存取repository。

接下來,要有一位同學(如:組長)在專案底下先建立local repository。並將專案commit到local repository,然後再將local repository的內容push到remote repository (在git server上)。完成後,就可以在git server上看到專案的內容了。

其他同學利用Clone URI,選擇remote repository上的branch(例如:master)以及local repository的路徑。

當任何一位同學修改專案內容時,就可以進行Commit and Push,將更動的內容送到remote repository給其他同學,其他同學可以利用Pull下載最新的更動。也可以利用workspace synchronization進行同步,同時上傳也下載最新的更動,如果有多人同時更動同一檔案,也可以比對各版本的內容。

現在的版本管理軟體都支援分支(branch)的概念,原本是讓程式開發者能依據不同的使用者(如:公司或單位)開發不同的版本,不過,現在即使是只開發一個版本,也常使用分支。首先,每個repository會有一個稱為"master"的分支。通常,我們會維持master為最終穩定版本。為避免影響目前的最終穩定版本,當每位同學要開始進行修改時,會先產生一個新分支,當這個分支被完整測試後,再合併(merge)回master。這樣的話,就比較不會因為某一位同學push有問題的程式碼而造成大家都找不到最終穩定版本 (詳參: https://backlogtool.com/git-guide/tw/stepup/stepup1_1.html)。

$ git config --global user.name <username>

$ git config --global user.email <mailaddress>

  • 儲存帳號密碼,這樣就不用一直輸入帳號密碼了

$ git config --global credential.helper store

$ git init

$ git add <filepattern>

$ git commit -m <message>

$ git branch <branchname>

$ git checkout <branch>

$ git merge <branch>

$ git clone <url>

$ git remote add <name> <url>

$ git checkout <branch>

$ git push <repository> <refspec>

$ git pull <repository> <refspec>

$ git stash save

$ git stash list

$ git stash pop

$ git stash drop

常見問題

「You are not allowed to force push code to a protected branch on」

當使用Gitlab時,為了保持某些branch(如:master)的完整性,系統會有protected branch的功能,第一、避免protected branch被任意產生,第二,避免protected branch被更動(push),第三、避免protected branch被強迫更動(force pushing),第四、避免protected branch被刪除。在setting下,選擇repository,下面可以找到protected branch的設定。

解決方案:1.設定開發者(developer)及管理者(masters)可以merge,這樣的話,所有的更動都應該,先新增branch,等確定之後,合併(m,erge)回master(最好的做法)。2.可以開發者(developer)及管理者(masters)可以push,這樣的話,還可以管制protected branch的產生與刪除。3.可以將該branch改為unprotected (最不好的做法)。

如何進行Local Merge?

在Eclipse (egit)中,要進行merge要從「Git Repository」 view,然後選擇你的branch,然後,按右鍵,選擇Merge...,選擇遠端的的branch,按Merge。如果有衝突,就會看到有衝突。然後,編輯有衝突的檔案並且儲存。接下來,打開「Git Staging」 view,會看到剛剛改的檔案列在「Unstaged Changes」,選擇檔案,按右鍵,點選「Add to Index」,表示我們已經解決衝突了。最後,在「Git Staging view」按「Commit and Push」。最後,再到gitlab上進行merge request,就不會再出現要求local merge的訊息了 (詳參: http://stackoverflow.com/questions/21559119/how-to-resolve-conflicts-in-egithttp://wiki.eclipse.org/EGit/User_Guide#Resolving_a_merge_conflict)。

Eclipse裡的local merge不是很好用,也可以使用SourceTree或GitKraken (What are the best Git clients for Windows?) ,SourceTree和GitKraken有windows及mac版本。

To https://github.com/eddiekao/dummy-git.git

! [rejected] master -> master (fetch first)

error: failed to push some refs to 'https://github.com/eddiekao/dummy-git.git'

hint: Updates were rejected because the remote contains work that you do

hint: not have locally. This is usually caused by another repository pushing

hint: to the same ref. You may want to first integrate the remote changes

hint: (e.g., 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details

解決方法:

git pull --rebase

參考資料

09_Configuration Management.ppt