--- tags: NCUGC Author: uccu --- # NCUGC GIT 實作 Day1 ## 環境架設 ### 課前準備 - [安裝 Git](https://git-scm.com/) - [安裝 Visual Studio Code](https://code.visualstudio.com/) - [Github 帳號註冊](https://github.com/) ### VsCode設置 1. 開啟VsCode 2. 點擊上方工作列 Terminal -> New Terminal ![](https://i.imgur.com/JXRTgyk.png) 3. 在 Terminal 輸入 `git --version`,確認git是否正確安裝 ```shell= git --version ``` ![](https://i.imgur.com/X8TdPxF.png) 4. 點擊左方工作列 -> Extensions -> 在搜尋欄輸入git 5. 安裝 Git History、GitLens、Git Graph ![](https://i.imgur.com/kfPsQZl.png) ## Git實作教學 ### 新建Git repository 1. 開啟VsCode 2. 點擊上方工作列 File -> Open Folder ![](https://i.imgur.com/cMUHSAV.png) 3. 找到你的專案資料夾 -> 點擊選擇資料夾 (實作課請開啟一個空資料夾) 4. 點擊上方工作列 Terminal -> New Terminal 5. 在 Terminal 輸入 `git init`,新建 Git repository ```shell= git init ``` ![](https://i.imgur.com/dCx1nEd.png) ## 建立版本紀錄 1. 第一次使用,輸入名稱及E-mail ```shell= git config --global user.name "[名稱]" git config --global user.email "[e-mail]" ``` 2. 使用 `git status` 查看目錄狀態 ```shell= git status ``` ![](https://i.imgur.com/jQFnA0R.png) 3. 點擊 New File -> 取完名後按 enter 建立檔案 4. 點擊檔案新增內容 -> 按下 Ctrl + s 儲存 ![](https://i.imgur.com/uwRgScr.png) 5. 使用 `git status` 查看目錄狀態 ```shell= git status ``` ![](https://i.imgur.com/SMNWW6j.png) Untracked 指 Git 偵測到此專案的檔案發生了變動,必須先將它加入到索引裡,才能進行繳交 6. 使用 `git add .` 將專案裡的檔案加入索引 ```shell= git add . ``` 7. 使用 `git status` 查看目錄狀態 ```shell= git status ``` ![](https://i.imgur.com/5gpCLa6.png) 檔案狀態從Untracked files變成Changes to be committed,代表成功加入索引 8. 使用 `git commit` 新增一個版本 ```shell= git commit -m "[訊息]" ``` ![](https://i.imgur.com/3glRDSV.png) 9. 使用 `git status` 查看目錄狀態 ```shell= git status ``` ![](https://i.imgur.com/cOZ3eiW.png) 因為將檔案提交成了一個版本,所以工作目錄會顯示清空 10. 點擊左方工作列 -> Source Control -> View Git Graph ![](https://i.imgur.com/vM84xR1.png) 從 Git Graph 可以看到各版本的紀錄(作者、訊息、修改的檔案) ## 建立第二個版本紀錄 1. 回到編輯介面,更改檔案內容 ![](https://i.imgur.com/xGFyYxp.png) 2. 使用 `git status` 查看目錄狀態 ```shell= git status ``` ![](https://i.imgur.com/DSUd0B7.png) 3. 使用 `git add .` 將加入索引並使用 `git commit` 新增版本 ```shell= git add . git commit -m "my second commit" ``` ![](https://i.imgur.com/0cf1vOD.png) 4. 點擊左方工作列 -> Source Control -> View Git Graph ![](https://i.imgur.com/Uzqc76l.png) ### 提交版本紀錄流程 ![](https://i.imgur.com/kfT6b1n.png) ## 分支 1. 使用 `git branch` 查看所有分支以及目前所在分支 ```shell= git branch ``` ![](https://i.imgur.com/XYj0bE5.png) 2. 使用 `git branch "分支名稱"` 來建立分支並使用 `git branch` 查看分支 ```shell= git branch dev git branch ``` ![](https://i.imgur.com/6RNt8KN.png) 3. 使用 `git checkout "分支名稱"` 來切換分支並使用 `git branch` 查看分支 ```shell= git checkout dev git branch ``` ![](https://i.imgur.com/8sFN9lj.png) 4. 回到編輯介面,更改檔案內容 ![](https://i.imgur.com/kemWBzV.png) 5. 使用 `git add .` 將加入索引並使用 `git commit` 新增版本 ```shell= git add . git commit -m "my dev commit" ``` 6. 點擊左方工作列 -> Source Control -> View Git Graph ![](https://i.imgur.com/aikzsQD.png) 7. 使用 `git checkout "分支名稱"` 切換回master分支 ```shell= git checkout master ``` ![](https://i.imgur.com/c8eNBbh.png) 8. 點擊原本的檔案查看裡面內容 ![](https://i.imgur.com/gQdTgze.png) ## Merge(Fast-Forward) 1. 使用 `git branch` 查看所有分支以及目前所在分支 ```shell= git branch ``` ![](https://i.imgur.com/nwkYO7E.png) 2. 使用 `git merge "要合併的分支"` 來合併分支 ```shell= git merge dev ``` ![](https://i.imgur.com/ro3EIYZ.png) 3. 回到 Git Graph 查看變化 ![](https://i.imgur.com/OSwwmBr.png) ## Merge(Normal Case) 1. 使用 `git checkout "分支名稱"` 來切換分支並使用 `git branch` 查看分支 ```shell= git checkout dev git branch ``` ![](https://i.imgur.com/8sFN9lj.png) 2. 回到編輯介面,更改檔案內容 ![](https://i.imgur.com/2weMZoU.png) 3. 使用 `git add .` 將加入索引並使用 `git commit` 新增版本 ```shell= git add . git commit -m "more dev commit" ``` 4. 使用 `git checkout "分支名稱"` 來切換分支並使用 `git branch` 查看分支 ```shell= git checkout master ``` ![](https://i.imgur.com/c8eNBbh.png) 5. 回到編輯介面,更改檔案內容 ![](https://i.imgur.com/GdymV5T.png) 6. 使用 `git add .` 將加入索引並使用 `git commit` 新增版本 ```shell= git add . git commit -m "master develop!!" ``` 7. 回到 Git Graph 查看變化 ![](https://i.imgur.com/fyiKzop.png) 8. 使用 `git merge "要合併的分支"` 來合併分支 ```shell= git merge dev ``` ![](https://i.imgur.com/FddwMV3.png) 因為要merge的兩個分支修改到相同的檔案而 git 沒辦法決定要採用何者 9. 回到 Git Graph 查看變化 ![](https://i.imgur.com/s81AGPR.png) 10. 點擊原本的檔案查看裡面內容,試著點選衝突選項 ![](https://i.imgur.com/9UxjcMD.png) 11. 修改完檔案後,使用 `git add .` 將加入索引並使用 `git commit` 新增版本 ```shell= git add . git commit -m "merge commit" ``` 12. 回到 Git Graph 查看變化 ![](https://i.imgur.com/dHD3rdk.png) ## Github 1. [建立 Github 數據庫](https://github.com/new) 2. 輸入數據庫名稱 -> 設定為Public -> 點選建立 ![](https://i.imgur.com/87RTAW2.png) 3. 複製數據庫網址 ![](https://i.imgur.com/y24qsYE.png) 4. 回到專案,使用 `git status` 查看目錄狀態 ```shell= git status ``` ![](https://i.imgur.com/J4pNaxk.png) 5. 新增remote的數據庫 ```shell= git remote add origin <剛複製的網址> ``` 6. 將檔案傳送至遠端數據庫 (若需要帳密則需輸入github帳密) ```shell= git push origin master ``` ![](https://i.imgur.com/xeJZycs.png) 7. 重新整理 github 網站,會看到成功將資料上傳至 github 8. 可以試著建立新的版本紀錄然後將檔案傳到github ```shell= ->改編你的檔案 git add . git commit -m "訊息" git push origin master ``` ## 學習資源 [NCUGC Git 教學](https://hackmd.io/@cu/BJfjAjBqU) [六角學院-git、github教學](https://www.youtube.com/playlist?list=PLYrA-SsMvTPOZeB6DHvB0ewl3miMf-2tj) [Git & GitHub 教學手冊](https://w3c.hexschool.com/git/cfdbd310) [git learning 遊戲](https://learngitbranching.js.org) [為你自己學 Git](https://gitbook.tw/) [連猴子都能懂的Git入門指南](https://backlog.com/git-tutorial/tw/)