--- 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  3. 在 Terminal 輸入 `git --version`,確認git是否正確安裝 ```shell= git --version ```  4. 點擊左方工作列 -> Extensions -> 在搜尋欄輸入git 5. 安裝 Git History、GitLens、Git Graph  ## Git實作教學 ### 新建Git repository 1. 開啟VsCode 2. 點擊上方工作列 File -> Open Folder  3. 找到你的專案資料夾 -> 點擊選擇資料夾 (實作課請開啟一個空資料夾) 4. 點擊上方工作列 Terminal -> New Terminal 5. 在 Terminal 輸入 `git init`,新建 Git repository ```shell= git init ```  ## 建立版本紀錄 1. 第一次使用,輸入名稱及E-mail ```shell= git config --global user.name "[名稱]" git config --global user.email "[e-mail]" ``` 2. 使用 `git status` 查看目錄狀態 ```shell= git status ```  3. 點擊 New File -> 取完名後按 enter 建立檔案 4. 點擊檔案新增內容 -> 按下 Ctrl + s 儲存  5. 使用 `git status` 查看目錄狀態 ```shell= git status ```  Untracked 指 Git 偵測到此專案的檔案發生了變動,必須先將它加入到索引裡,才能進行繳交 6. 使用 `git add .` 將專案裡的檔案加入索引 ```shell= git add . ``` 7. 使用 `git status` 查看目錄狀態 ```shell= git status ```  檔案狀態從Untracked files變成Changes to be committed,代表成功加入索引 8. 使用 `git commit` 新增一個版本 ```shell= git commit -m "[訊息]" ```  9. 使用 `git status` 查看目錄狀態 ```shell= git status ```  因為將檔案提交成了一個版本,所以工作目錄會顯示清空 10. 點擊左方工作列 -> Source Control -> View Git Graph  從 Git Graph 可以看到各版本的紀錄(作者、訊息、修改的檔案) ## 建立第二個版本紀錄 1. 回到編輯介面,更改檔案內容  2. 使用 `git status` 查看目錄狀態 ```shell= git status ```  3. 使用 `git add .` 將加入索引並使用 `git commit` 新增版本 ```shell= git add . git commit -m "my second commit" ```  4. 點擊左方工作列 -> Source Control -> View Git Graph  ### 提交版本紀錄流程  ## 分支 1. 使用 `git branch` 查看所有分支以及目前所在分支 ```shell= git branch ```  2. 使用 `git branch "分支名稱"` 來建立分支並使用 `git branch` 查看分支 ```shell= git branch dev git branch ```  3. 使用 `git checkout "分支名稱"` 來切換分支並使用 `git branch` 查看分支 ```shell= git checkout dev git branch ```  4. 回到編輯介面,更改檔案內容  5. 使用 `git add .` 將加入索引並使用 `git commit` 新增版本 ```shell= git add . git commit -m "my dev commit" ``` 6. 點擊左方工作列 -> Source Control -> View Git Graph  7. 使用 `git checkout "分支名稱"` 切換回master分支 ```shell= git checkout master ```  8. 點擊原本的檔案查看裡面內容  ## Merge(Fast-Forward) 1. 使用 `git branch` 查看所有分支以及目前所在分支 ```shell= git branch ```  2. 使用 `git merge "要合併的分支"` 來合併分支 ```shell= git merge dev ```  3. 回到 Git Graph 查看變化  ## Merge(Normal Case) 1. 使用 `git checkout "分支名稱"` 來切換分支並使用 `git branch` 查看分支 ```shell= git checkout dev git branch ```  2. 回到編輯介面,更改檔案內容  3. 使用 `git add .` 將加入索引並使用 `git commit` 新增版本 ```shell= git add . git commit -m "more dev commit" ``` 4. 使用 `git checkout "分支名稱"` 來切換分支並使用 `git branch` 查看分支 ```shell= git checkout master ```  5. 回到編輯介面,更改檔案內容  6. 使用 `git add .` 將加入索引並使用 `git commit` 新增版本 ```shell= git add . git commit -m "master develop!!" ``` 7. 回到 Git Graph 查看變化  8. 使用 `git merge "要合併的分支"` 來合併分支 ```shell= git merge dev ```  因為要merge的兩個分支修改到相同的檔案而 git 沒辦法決定要採用何者 9. 回到 Git Graph 查看變化  10. 點擊原本的檔案查看裡面內容,試著點選衝突選項  11. 修改完檔案後,使用 `git add .` 將加入索引並使用 `git commit` 新增版本 ```shell= git add . git commit -m "merge commit" ``` 12. 回到 Git Graph 查看變化  ## Github 1. [建立 Github 數據庫](https://github.com/new) 2. 輸入數據庫名稱 -> 設定為Public -> 點選建立  3. 複製數據庫網址  4. 回到專案,使用 `git status` 查看目錄狀態 ```shell= git status ```  5. 新增remote的數據庫 ```shell= git remote add origin <剛複製的網址> ``` 6. 將檔案傳送至遠端數據庫 (若需要帳密則需輸入github帳密) ```shell= git push origin master ```  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/)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up