介紹如何透過 Visual Studio 2015 直接連到 github repo,進行 commit/push/pull 等動作。
快速介紹一下,可以怎麼樣在 github 上新建一個 repository 並且在 VS2015 中直接接上 local git/remote repo 的 source control。
Step 1 - Create Repository on Github
在 github 個人首頁,點選 Repositories 頁籤,點下 New 的按鈕。
Step 2 - Repository Setting
設定 Repository name 並增加 gitignore 設定,選擇為 Visual Studio 就能避免不必要的檔案類型被一併簽入。
Step 3 - Install GitHub Extension for Visual Studio
安裝 Visual Studio Extension: GitHub Extension for Visual Studio
Step 4 - Connect and Login Github
選擇 Team Explorer 會看到 GitHub 的功能區塊,選擇 Connect 就能登入 github。
輸入帳號、密碼,按下 Login。
Step 5 - Clone Github Repository
登入 github 完畢後,在 Team Explorer 頁籤中就可以看到 Clone 的功能,點選後就可以將 github 上的 repo clone 一份到 local git repositories 中。
選擇 github 上想要 clone 的 repo 後,選擇要放到 local 的目錄位置,按下 Clone 即可。
Step 6 - Open Local Git Repo and Create New Solution![](https://dotblogsfile.blob.core.windows.net/user/hatelove/bc77227b-f15c-4caa-b6f8-499392e40aeb/1460363018_5302.png)
Clone 完後,會在 Local Git Repositories 的區塊中,看到剛剛 clone 下來的 repo ,double click 後就能看到裡面的 solutions 有哪些。
因為現在的 local git repo 裡面還沒有任何 solution,所以點選 New 以建立一個全新的專案。接著建立你要的專案類型,預設的 location 與 add to source control 應該都是對的。
Step 7 - Commit First Version by Visual Studio![](https://dotblogsfile.blob.core.windows.net/user/hatelove/bc77227b-f15c-4caa-b6f8-499392e40aeb/1460363251_97094.png)
原本的 repo 中,沒有任何程式碼相關的檔案,在初始化完專案後,只需在 solution 上點選右鍵,點選 Commit,就可以將初始化完的版本 commit 進 repo 中。
接著輸入 commit comment 後,Commit 的下拉選單有三個可以選:
- Commit: 因為 git 是分散式的架構,所以如果你只想先把這次的異動,加入 local 的版本控管,還不需要 push 回 github/git remote repo 上時,選擇 commit 就可以了。(這個動作不需要網路,是離線式的)
- Commit and Push: 如果這一次的 Commit 你想順便把所有與 github/git remote repo 之間差異的 commits 全部推上去的話,就選擇 Commit and Push 。Push 的動作就會把你 local 端所有還沒 push 的 commit 都推上去。Push 完你在 github 上就會看到跟 local 一樣的結果。
- Commit and Sync: 如果 git remote repo 已經有其他的 commit,local 還沒更新的話,應該要執行 pull (把 remote repo 的差異拉回 local 進行 merge)。如果你的 local 比較舊,git 是不允許你直接 push 的,應該要先執行 pull 後 merge 完再 push 。如果這一次的 commit 你想執行的動作是 pull + commit + push ,那麼直接選擇 Commit and Sync 即可。
Step 8 - Show the Latest Version on Github
當 local push 成功後,就可以在 github 上看到最新的版本囉。而且 local 的每個 commit 版本記錄也會一併推上去。
點選 Commits 的 link 後就可以看到所有 commit history,如下圖所示:
點選 commit 的 hash link 就可以看到各個 commit 版本之間的差異。
結論
VS2015 對 git 與 github 的整合已經相當便利,而 github 更是現代 developer 展現自己作品最佳的舞台,哪怕只是自己練習的作品,上課用來交流題目與作業,甚至拿來當 blog, wiki, issue tracking system 都是相當方便的媒介。
在 IDE 中直接整合,則可以讓 developer 更加方便、即時的進行版本控管。
希望這篇簡介可以幫助還不知道怎麼使用的朋友快速上手。
blog 與課程更新內容,請前往新站位置:http://tdd.best/