<style> .reveal { font-size: 24px; } </style> # GIT 版本控制 #### Jason & Rice --- ## 開始前,請大家... 1. 在電腦上安裝 git - windows: [git for windows](https://git-scm.com/download/win) - ubuntu: `sudo apt install git` 2. 至 github.com 註冊帳號 3. (optional) 習慣用 GUI 的朋友們 請額外下載這兩個擇一並註冊帳號 * [GitHub desktop](https://desktop.github.com/) * [Sourcetree](https://www.sourcetreeapp.com/) [bitbucket 帳號](https://id.atlassian.com/signup) Extra : [配合課程的小玩具](https://gitbook.tw/playground) --- ## git 是什麼,不就是 GitHub 嗎? 投票一下? ---- ![](https://i.imgur.com/k7sQJqI.png) ---- 常見混淆:git & GitHub 的差異? :::success git 是一種分散式的版本控制系統 (Distributed Version Control System) 簡單來說,git 就是一(大)段 code ,而他的功能是幫你管理而已啦~ GitHub 的本體是一個包含 git 功能的伺服器,方便大家透過介面來操作 / 管理 同時提供雲端備份,Pull Request ... 等等眾多功能 ::: 你可以這麼想 :::info 我們知道 facebook, youtube, 蝦皮都是利用 **某種推薦演算法** 的平台 那相同的, GitHub 就是利用 **git** 的平台 ::: ---- 比較一下 github 和 drive 的詳細程度 ![](https://i.imgur.com/lJaPfcu.png =500x) 說一下看到什麼差異? ---- ### 當你不用 git ... 我改了什麼? 我什麼時候改的? ![](https://i.imgur.com/MJEgwKn.png =250x) ---- ![](https://i.imgur.com/bNNT3lK.png =250x) ---- ![](https://i.imgur.com/9FugHra.png) --- ## 各種版本控制 ---- ### 本地端版本控制 - git 在沒配合 github 的情況下可以簡化成這個 ![](https://i.imgur.com/UcORn1Z.png =400x) ---- ### 集中式版本控制 - 你只能跟"中心"存取進度 - 本機出事了 → 還能重抓一份 - 中心出事了 → 沒救 - 異地備份的重要性 ![](https://i.imgur.com/J8sZjXW.png =400x) ---- ### 分散式版本控制 或者用 "分享" 一詞更為適合? 與集中式的最大差別是:本地有沒有一份完整的修改歷史紀錄 (也就是備份啦) 去中心化:"每個人都是中心",就沒有中心了 ![](https://i.imgur.com/qu2WbnQ.png =400x) ---- ### 分散式版本控制 & 去中心化 - 思考時間 ![](https://i.imgur.com/qu2WbnQ.png =300x) > 思考1:什麼改變導致去中心化的出現? > 思考2:server computer有存在必要嗎? --- ## 需要知道的名詞 - 1 - repo. - repository(儲存庫) 的簡稱,一個 repo 裡會有你想記錄的檔案及對應的修改紀錄、分支 - commit - 修改紀錄,包含了誰改的,什麼時候改的,改了哪裡以及為什麼要改 - ~~沒事多 commit,多 commit 沒事~~ - 改變是 "以 現在 比對 過去,相對產生的差異" - branch - "分頭行動",不同的人可以利用分支來分配不同的工作 - 在合理分工的前提下,不會產生互相影響的問題,且能夠隨時統合工作進度 - 看過科幻作品的同學們,可以把他想成平行時空(? - 在分歧點之前的經歷都會是一樣的,但之後會怎麼發展全看主角怎麼決定 ---- ## 需要知道的名詞 - 2 - ref. - Reference ( 參考 ) 的簡稱,類似指標 ( pointer ) - 是一個 40 字元的 SHA1 hash 碼,作為 commit, branch 的身分證 - HEAD - 可以想成指標 ( pointer ),原則上是指向目前 branch 最新的一次 commit - 稱為該分支的 HEAD - EXTRA : [detached HEAD](https://gitbook.tw/chapters/faq/detached-head.html) --- ## 踏出第一步 ### git init 新房子要整理完才能住進去,git 也是同樣道理! 在目標資料夾內會生成一個 .git 資料夾,裡面存放的是head, ref, commit message... 之類的東西,以後這個資料夾就是一個 repo 囉 ![git init](https://images-cdn.kahoot.it/d4272959-87db-48c2-9a24-01cce4567315 =300x) ---- ### git add / git stage 倉庫、貨架與客人的關係 * git add newCode.cpp * git add . ![git add](https://i.imgur.com/FYc5Vcb.png =400x) ---- ### git commit * 基礎版 * git commit * 一步到位版 * git commit -m "your msg here" ---- #### a good commit msg? ![](https://i.imgur.com/2zaR2d5.png =300x) who, when, where git 都幫你用了 我們要做的就是寫出簡短、精準的 message 方便跟組員、同事、上司,抑或是未來的自己 說明你現在所做的部分 ---- ### git stash 先去做緊急的事 ![](https://i.imgur.com/B5y7TLV.png =200x) stack 的味道( ? * git stash push * git stash pop --- ## git status 詳細資訊 ![](https://i.imgur.com/jEWYgI0.png) * git status * git log --- ## 分支操作 * 開新分支 1. git branch newBranchName 2. git checkout / switch tgtBranchName * 一步到位版 1. git checkout -b newBranchName * 切換到目標分支 * git checkout branchName * 刪除分支 * git branch -d tgtBranchName ![](https://i.imgur.com/OFBud1T.png =250x) --- ## 合併分支 * git merge < branch > * merge A ~~(into B)~~ * 把 A(目標) 的東西收進 B(自己) 背包裡 * 你可以這麼想 : 大公司(B)併購小公司(A) * 特殊情況 : fast-forward merge * git rebase < branch > * rebase ~~(A onto)~~ B * 把 A(自己) 的東西接到 B(目標) "上" * 你可以這麼想 : 移花(A)接木(B) * 特殊用法:[互動模式](https://www.gss.com.tw/blog/%E4%BD%BF%E7%94%A8-git-rebase-interactive-%E6%A8%A1%E5%BC%8F%E6%95%B4%E7%90%86-commit) --- ## ctrl z (x ![](https://i.imgur.com/wpN1Ii6.png =400x) ---- ### 人生不能重來,但是 git 可以 * git checkout < HASH > * git reset * 作用上更像 go to (? | reset模式 | mixed 模式 | soft 模式 | hard 模式 | | ------- | --------- | --------- | --------- | | 工作目錄 | 不變 | 不變 | 丟掉 | | 暫存區 | 丟掉 | 不變 | 丟掉 | ---- ### 人生不能重來,但是 git 可以 (extra) * git revert * 更像抵銷... * 優點:更安全 * git commit --amend * 註 : 有效範圍只有**最近一次 commit** * git rebase 的互動模式 * 建議自學(? --- ## remote / GitHub多人合作 ![](https://i.imgur.com/ytixb5p.png) ---- ### remote 指令 * git fetch * git push * git pull * pull = fetch + merge --- ## 其他重要的指令 - git clone - git fork - git blame - git cherry-pick - git bisect --- # Thanks! 課外資源: https://gitbook.tw/ https://backlog.com/git-tutorial/tw/
{"metaMigratedAt":"2023-06-15T20:23:44.098Z","metaMigratedFrom":"YAML","title":"GIT 版本控制","breaks":true,"slideOptions":"{\"theme\":\"moon\",\"transition\":\"fade\"}","description":"在電腦上安裝 git","contributors":"[{\"id\":\"a8528c1d-4f69-45d5-a840-726bc6afe86a\",\"add\":7189,\"del\":2533}]"}
    820 views