# 講座一:淺談版本管理與分岔(實作) * 講義: [TOC] ## 實作一:探索 Git 儲存庫託管平台 逛一逛幾個 Git 儲存庫(Repository)的託管網站 * The Linux Kernel Archives * https://kernel.org ![git-repo-linux](https://hackmd.io/_uploads/S12_bT1kxg.png) * Audacity GitHub * https://github.com/audacity/audacity * Blender GitHub * https://github.com/blender/blender * NotePad++ (台灣開發者侯今吾寫的文字編輯器) * https://github.com/notepad-plus-plus * Aesthetic-Programming 《美學程式設計》GitLab * https://gitlab.com/aesthetic-programming * 最後,申請一個 GitHub 帳號 * https://github.com/ ![lab01](https://hackmd.io/_uploads/BJ5S5Ryklx.png) ## 實作二:安裝 Git(本地端工具) Ref: [How to Install Git on Mac and Windows! (Beginner's Guide)](https://www.youtube.com/watch?v=7ouVv6PFZGc) (youtube) 步驟: 1. 前往 https://git-scm.com/downloads * Windwos 用戶下載 `64-bit Git for Windows Setup.` * Mac 用戶依照指引使用 brew 安裝 git 1. 先依照 https://brew.sh/ 的指示安裝 brew ``` /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 2. 再透過 brew 安裝 git ``` brew install git ``` * 驗證:在終端機下執行 `git --version` 看看是否輸出版本編號 ![git-install-01](https://hackmd.io/_uploads/HJDajTJJgx.png) ![git-install-02](https://hackmd.io/_uploads/SyvTiTkyee.png) ![git-install-03](https://hackmd.io/_uploads/B1vToaykxe.png) ![git-install-04](https://hackmd.io/_uploads/Skvaoa1yll.png) ![git-install-05](https://hackmd.io/_uploads/B1v6ipyJxl.png) ## 實作三:在 GitHub 建立第一個儲存庫 指令: | Git 指令 | 說明 | |----------------------|-------------| |`git --help` | 查詢 git 指令 | |`git status` | 查看 repository 狀態 | |`git init` | 建立一個「空的」local repo | |`git add` | 將檔案納管,暫時加入 暫存區(Stage) | |`git commit -m "訊息"` | 將暫存區(Stage) 內的檔案,提交(Commit)到 local repo | |`git log` | 顯示 commit logs | |`git diff` | 顯示 commit 前修改的內容 | |`git push` | 將 local 端 repo 的狀態同步到 remote 端 | |`git pull` | 將 remote 端 repo 的狀態同步回 local 端 | github 申請後 ``` git config --global user.email "you@example.com" git config --global user.name "Your Name" ``` ### Step 1. 在 local 端建立自己的儲存庫,並且提交幾個檔案 ![lab03](https://hackmd.io/_uploads/HJHIcAJkgl.png) 操作示範: ![git-commit](https://hackmd.io/_uploads/B1BXUklyle.png) 如果剛剛沒有設定 email 和名稱,commit 時會報錯 ![git-config](https://hackmd.io/_uploads/BktxYylyge.png) ### Step 2. 在 GitHub 建立一個 remote 端的空儲存庫 建立一個 remote repo ![new-repo](https://hackmd.io/_uploads/ryjD30Jkeg.png) ![new-repo3](https://hackmd.io/_uploads/B1UVc1g1ll.png) ### Step 3. 將 local 端的儲存庫上傳(push)到 remote 端(GitHub) ![git-push](https://hackmd.io/_uploads/B1nNcyg1ge.png) push 指令: ``` git remote add origin git@github.com:<你的帳號>/<你的repo_name>.git git push -u origin main ``` ## 實作四:練習修改遠端檔案並同步 | Git 指令 | 說明 | |----------------------|-------------| |`git pull` | 將 remote 端 repo 的狀態同步回 local 端 | 實驗步驟 1. 直接在 GitHub(remote端) 修改或新增檔案 2. 在 local 端開啟終端機,進入「由 git 控管的目錄」,執行 ``` git pull ``` 3. 檢查看看 local 端的檔案是否已經與 remote 端同步 ![git-pull](https://hackmd.io/_uploads/SJl-Bzgkll.png) ## 實作五:回溯到特定版本 ![git-checkout](https://hackmd.io/_uploads/HyFaqzeyge.png) 指令 ``` git checkout <SHA/revision> ``` ``` git checkout HEAD ``` ## 實作六:Fork 儲存庫,製作自己的副本 ![fork](https://hackmd.io/_uploads/H1ZGYVlJxe.png) ## 實作七:讓你的貢獻被看見(從 Fork 到 Merge)