SlideShare a Scribd company logo
1 of 109
Download to read offline
從微觀到宏觀 
http://ihower.tw 
2014/10/29@livehouse.in
我是誰 
• 張⽂文鈿 a.k.a. ihower 
• http://ihower.tw 
• Instructor at ALPHA Camp 
• http://alphacamp.tw 
• Git user since 2008
Agenda 
• 微觀:Git 的內部設計 
• 宏觀:Git 的分⽀支開發流程和策略 
不是 Git ⼊入⾨門教學!
Part1. Git 的內部設計
⼩小測驗 
如何⽤用 Git 底層指令,不⽤用 git add 
和 git commit 指令進⾏行 commit 動作?
⽤用 Graph 概念理解
working area 
⺫⽬目錄檔案A 檔案B
git add . 
(將⺫⽬目錄節點和檔案內容節點關聯起來) 
檔案內容A 
檔案內容B 
⺫⽬目錄
檔案內容A 
V1 
檔案內容B 
V1 
git commit 
(產⽣生commit節點,指向⺫⽬目錄節點) 
⺫⽬目錄 
V1 
Commit 
V1 
接下來我們修改檔案 A 成為 V2 版本,檔案B不變
檔案內容B 
V1 
git commit (cont.) 
(產⽣生commit V2節點,指向parent commit節點) 
⺫⽬目錄 
V1 
Commit 
V1 
檔案內容A 
V2 
⺫⽬目錄 
V2 
Commit 
V2 
檔案內容A 
V1 
檔案內容B不變
Git is objects database 
Blob 
object 
Blob 
object 
Tree 
object 
Commit 
object
儲存內容(demo) 
• git init 
• echo hello > hello.txt 
• git add . 
• tree .git 
• 存在 .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a 
• 這是 hello 內容的 SHA1 
• printf "blob 6x00hellon" | shasum 
• echo "hello" | git hash-object --stdin 
• git cat-file -p ce0136
ce0136 
hello 
blob 
blob object 的實際檔案名稱 
.git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
Blob object 
• Git 是 Content-addressable filesystem 
• Blob 沒有 metadata,沒有檔名資訊 
• Blob object 的儲存檔名,是根據內容產⽣生的 
SHA1 
• 內容⼀一樣的檔案,根據 SHA1 演算法只會存 
成同⼀一份檔案,不會浪費空間
儲存⺫⽬目錄 (demo) 
• git write-tree 
(根據 staging area 產⽣生 Tree object) 
• git cat-file -p aaa96c
Tree 
aaa96c ce0136 
100644 blob ce0136 hello.txt 
Tree object 的實際檔案名稱 
.git/objects/aa/a96ced2d9a1c8e72c56b253a0e2fe78393feb7 
hello 
blob 
其中 100644 為檔案模式,表⽰示這是⼀一個普通檔案 
100755 表⽰示可執⾏行檔,120000 表⽰示 symbolic link
儲存⺫⽬目錄 (cont.) 
• 新增兩個空⽩白檔案和⼦子⺫⽬目錄 
• touch hola.txt & mkdir lib & touch lib/bonjour.txt 
• git add . 
• git write-tree 
• git cat-file -p 16e19f (觀察這個 tree) 
• git cat-file -p e69de2 (觀察其中的 lib tree)
Tree 
040000 tree 122f77 lib 
100644 blob ce0136 hello.txt 
100644 blob e69de2 hola.txt 
100644 blob e69de2 bonjour.txt 
hello 
blob 
ce0136 
16e19f 
blob 
e69de2 
Tree 
122f77
Tree object 
• Git ⽤用 Tree object 把 Blob object 組織起來,包 
括檔案命名和⺫⽬目錄結構 
• Blob object 並沒有包含檔案名稱和⺫⽬目錄結構 
• Tree object 裡⾯面還可以有 Tree object ⼦子⺫⽬目錄 
• Tree object 的檔名,⼀一樣是根據內容產⽣生 
SHA1
遞交 Commit (demo) 
• git commit-tree 16e19f -m “First commit” 
• git cat-file -p 107aff 
• cat .git/HEAD 
• cat .git/refs/heads/master 
• git update-ref refs/heads/master 107aff 
• git rev-parse HEAD
Commit object 
指向 root tree SHA1 
Tree 
16e19f 
040000 tree 122f77 lib 
100644 blob ce0136 hello.txt 
100644 blob e69de2 hola.txt 
HEAD 
master 
commit 107aff 
tree 16e19f 
author ihower 1375381139 +0800 
committer ihower1375381139 +0800 
First commit
再次遞交 Commit (demo) 
• 修改 hola.txt 檔案,加⼊入 hola 字串 
• git commit -am “Second commit” 
• git cat-file -p 309be07
Commit object 
指向 parent commit SHA1 
Tree 
Tree 
fd44ed 
117a5 
040000 tree 122f77 lib 
100644 blob ce0136 hello.txt 
100644 blob e69de2 hola.txt 
commit 309be07 
commit 107aff 
tree 117a5b 
author ihower 1375381139 +0800 
committer ihower1375381139 +0800 
First commit 
040000 tree 122f77 lib 
100644 blob ce0136 hello.txt 
100644 blob 5c1b14 hola.txt 
tree fd44ed 
parent 107aff 
author ihower 1375381779 +0800 
committer ihower 1375381779 +0800 
Second commit
Commit object 
• 紀錄 root tree SHA1 
• 紀錄 parent commit SHA1 
• 紀錄作者、時間和 commit message 資訊 
• Commit object 的檔名,⼀一樣是根據內容產⽣生 
SHA1
Git commit 動作流程 
• ⽤用內容產⽣生 blob object 
• 寫⼊入 file mode, blob SHA1, file name 到 staging area 
• 根據 staging area 產⽣生 Tree object 
• ⽤用 root tree SHA1 和 parent commit SHA1 產⽣生 
commit object 
• ⽤用 commit SHA1 更新 master 參考
如何不⽤用 git add 和 git commit 
指令進⾏行 commit 動作? 
echo "hola" | git hash-object -w --stdin 
git update-index --add --cacheinfo  
100644 5c1b14949828006ed75a3e8858957f86a2f7e2eb hola.txt 
git write-tree 
git commit-tree 27b9d5 -m "Second commit" -p 30b060 
git update-ref refs/heads/master 97b806c9e5561a08e0df1f1a60857baad3a1f02e 
git add 
git commit 
https://gist.github.com/ihower/6132576 
測驗解答
Tag object 
(Tag 分兩種:annotated tag 才會產⽣生 object) 
• git tag -a release 
• git rev-parse release 
• git cat-file -p 2450f3
tag 2450f3 
object 309be0 commit 309be0 
type commit 
tag release 
tagger ihower 1375383070 +0800 
Release!
⼩小結論: 
Git 有四種 Objects 
• Blob 
• Tree 
• Commit 
• Tag
References 參照 
• 單純⼀一個檔案紀錄⼀一個 SHA1參照 
• Tag reference 
• Branch reference 
• HEAD reference (指向⺫⽬目前所在的 branch)
Tag reference 
• git tag tmp 
• cat .git/refs/tags/tmp 
• 不像 object 資訊豐富,reference 內容只 
有 Commit object SHA1 
refs/tags/tmp 
309be0 commit 309be0
Branch 和 HEAD 
reference 
• 每次 commit 就會變動 reference 
• HEAD 指向⺫⽬目前在哪⼀一個 branch 
• cat .git/HEAD 
• cat .git/refs/heads/master
refs/heads/master 
309be0 commit 309be0 
HEAD 
ref: refs/heads/ 
master
如果在 Branch 上產⽣生新 
Commit... 
refs/heads/master 
309be0 commit 309be0 
HEAD 
ref: refs/heads/ 
develop 
commit 55cbcb
Branch reference 就會⾃自動 
改指到新的 commit 
commit 309be0 
refs/heads/master 
55cbcb 
HEAD 
ref: refs/heads/ 
master 
commit 55cbcb
開新 Branch develop 
git branch develop 
refs/heads/master 
55cbcb commit 55cbcb 
refs/heads/develop 
55cbcb 
HEAD 
ref: refs/heads/ 
master
切換 Branch:改HEAD 
git checkout develop 
refs/heads/master 
55cbcb commit 55cbcb 
refs/heads/develop 
55cbcb 
HEAD 
ref: refs/heads/ 
develop
合併 Branch 
git merge develop 
da103e 40b603 
commit commit 
commit 
refs/heads/master 
caa307 
refs/heads/develop 
caa307 
commit
tree 5f398a5 
parent 40b603 
parent da103e 
author ihower 1375381779 +0800 
committer ihower 1375381779 +0800 
Merge branch ‘develop’ into master 
commit 
da103e 40b603 
commit commit 
commit 
refs/heads/master 
caa307 
refs/heads/develop 
caa307 
commit 
產⽣生的 merge 
commit 節點 
有兩個 parents
另⼀一種合併情況 fast-forward 
將 develop 合併進 master 
refs/heads/master 
(git merge develop) 
commit 
caa307 commi 
refs/heads/develop 
caa307 
commi
另⼀一種合併情況 fast-forward 
沒有產⽣生 merge 節點,只是移動參考 
commi 
commi 
refs/heads/master 
caa307 
refs/heads/develop 
caa307 
commi
Git 如何 Merge 
commits? 
• Git 進⾏行了⼀一個 Three-way merge 的動作 
• three-way merge 除了要合併的兩個檔 
案,還加上兩個檔案的共同祖先。如此 
可以⼤大⼤大減少⼈人為處理 conflict 的情況。 
• two-way merge 則只⽤用兩個檔案進⾏行合併 
(svn預設即 two-way merge)
Two-way merge 
12 
1234 
A B 
12? 
Conflict! 
需要⼈人⼯工判斷
12 
1234 
A B 
124 
⾃自動合併出 
正確結果 
123 
Three-way merge: 
先找出 AB 共同的祖先 
-3 +4
Part1 ⼩小結
additive 
• 跟 Unix filesystem 有類似的結構,除了 
• Git filesystem 的設計是⼀一直累加的,不會 
有東⻄西被刪除 
• Blob object 沒有 metadata
Reference is cheap 
• 開新 branch 只是 refs ⽽而已,直到 commit 
前都沒有負擔。 
• 不像有些 VCS 開分⽀支會複製⼀一份原始 
碼,⾮非常耗費資源。
Integrity 
• SHA1 是內容的 checksum 
• 如果檔案內容有損毀,就會發現跟SHA1不 
同。如果 tree 被偷改檔名,也會被發現。 
• HEAD 指向的 SHA1,就是整個 repository 
的 checksum 
• 這在分散式系統⾮非常重要:資料從⼀一個開 
發者傳到另⼀一個開發者時,確保資料沒有 
被修改。
"I will, in fact, claim that the difference between a bad 
programmer and a good one is whether he considers 
his code or his data structures more important. Bad 
programmers worry about the code. Good 
programmers worry about data structures and their 
relationships." 
- Linus Torvalds
Part2. Git 的分⽀支開發 
流程和策略
1. 常⾒見的 Remote Repositories 
的管理⽅方式
集中式⼯工作流程 
團隊內部私有專案,⼤大家都有權限 Push 到共⽤用的 Repository 
52 
shared 
repository 
developer developer developer
集成管理員⼯工作流程 
適合⼀一般 Open Source 專案,只有少部分⼈人有權限可以 Push 
到 Repository,其他開發者⽤用 request pull 請求合併。 
例如 GitHub 提供的 Fork 和 Pull Request 功能 
53 
developer 
repository 
developer 
repository 
committer 
developer 
repository 
official 
repository 
developer developer developer 
pull
司令官與副官⼯工作流程 
多層權限控管,適合⼤大型 Open Source 專案,例如 Linux Kernel 
54 
official 
repository 
director 
developer 
lieutenant 
repository 
lieutenant 
repository 
developer developer developer
2. 團隊如何管理 Branches 分⽀支?
Case Study 
• Github flow 
• Rails 
• CMake 
• Homebrew 
• Gitflow
Github flow 
‧master 是 stable/production 可佈署的版本 
‧任何開發從 master branch 分⽀支出 feature 
branch 
‧送 pull request 開始進⾏行討論、code review 
和測試 
‧最後合併回 master 代表可以佈署了 
57 
http://ihower.tw/blog/archives/7798
https://guides.github.com/introduction/flow/index.html
pros and cons 
• 簡單、清楚、容易了解 
• 搭配 Github 的 Pull Request 介⾯面 
• 沒有 release branch,東⻄西⼀一進 master 就 
上 production 
• Web app 如果有問題很容易 revert,如 
果是 desktop/mobile 軟體就 GG 了
Ruby on Rails 
‧Rails ⺫⽬目前的版本是 4.1 
‧master 是開發版本,開發下⼀一次的主要版本 4.2 
‧feature branches 審核完後,合併進 master 
‧maintenance branches,⽤用 cherry-pick 做 backporting 
–2-3-stable 已停⽌止維護 
–3-2-stable security fixes 
–4-0-stable bugs fixes 
–4-1-stable bugs fixes 
60
pros and cons 
• 基本上就是 Github flow 加上 maintenance 
branches 維護舊版的設計 
• ⾮非常多的 Open Source 專案採⽤用這種形式 
• 版本號(Tag)打在 master 上,透過 preview 
和 beta 的版本號進⾏行提前釋出
CMake 
http://public.kitware.com/Wiki/Git/Workflow/Topic 
• master 預備釋出的版本,feature branches 從這裡分⽀支出去 
• feature branch 完成後,合併進 next 
• next 整合版本,完成的 feature branch 先合併到這裡進⾏行測試 
• 在 next 測好的 feature branch,才合併進 master 
• 可以將 master 合併進 next,減少之後的 code conflicts 
• 不會將 next 合併進 master 
• nightly 每天 1:00 UTC ⾃自動從 next branch 分⽀支出來跑⾃自動測 
試
pros and cons 
• 個別 feature branch 的狀態很清楚 
• 需要發⾏行 binary release 的軟體需要較正 
式、較花時間的釋出過程(feature branch - 
> next -> master -> release branch)。來確 
保釋出的軟體有⾜足夠的測試。 
• 但是線圖其實很不好追蹤,很多 merge 
commits 跟真正的 commits 混雜在⼀一起 
• Revert a merge commit 其實不好做
Homebrew 
• 只有 master branch,沒有 merge commit 
• 所有 feature branch ⼀一律 rebase 和 
squashed 後進主幹
pros and cons 
• Homebrew 不只把 Git 當 VCS,甚⾄至當成 
delivery 部署機制 
• 歷史紀錄超級 Readable 容易讀 
• 沒有 release branch,釋出很快。有 bug 
也很容易 revert、re-apply 或 bisect 
• 合併前的 branch commits 訊息丟失
Git flow 
http://nvie.com/posts/a-successful-git-branching-model/
兩個主要分⽀支 
‧master: 穩定的 production 版 
‧develop: 開發版本,從 master 分⽀支出來 
71
三種⽀支援性分⽀支(1) 
‧Feature branches 
–開發新功能或修 bugs 
–從 develop 分⽀支出來 
–完成後 merge 回 develop 
–如果開發時間較⻑⾧長,則需定期同步 develop 主幹的 
程式(初學可⽤用 merge,建議改⽤用 rebase),不然最後會合併不回 
去。 
73
三種⽀支援性分⽀支(2) 
‧Release branches 
–準備要 release 的版本,只修 bugs 
–從 develop 分⽀支出來 
–完成後 merge 回 master 和 develop 
76
三種⽀支援性分⽀支(3) 
‧Hotfix branches 
–等不及 release 版本就必須⾺馬上修 master 趕著上線 
–會從 master 分⽀支出來 
–完成後 merge 回 master 和 develop 
77
pros and cons 
• 第⼀一個出名的、有形式的 Git 分⽀支⼯工作流 
程 
• 很清楚的追蹤 feature, release, hotfix 等 
branches,確保釋出和審核流程 
• 有點複雜,不適合⼩小專案或 Open Source 
專案 
• 不適合頻繁釋出(Continuous Deployment)
Discussions 
• Rebase before merge 
• Feature branch issue 
• Code review policy 
• Release branch
1. Rebase before merge 
• 在 merge 前,做不做 rebase 整理 
commits? 
• 建議能⼒力和時間所及,就去做
rebase + merge 的完美合併法 
(假設我們要將 feature branch 合併回主幹 develop) 
‧原因 
–feature branch 很亂,不時 merge 與主幹同步 
–feature branch 有 typo,commit 訊息想改 
–feature branch 有些 commits 想合併或拆開 
‧作法 
–先在 feature branch 做 git rebase develop -i 
–(反覆整理直到滿意) git rebase 分岔點 -i 
–在從 develop branch 做 git merge feature --no-ff 
83
超級乾淨,每⼀一次的 merge 
commit 就代表⼀一個功能完成
Demo (before merge) 
85 
http://ihower.tw/blog/archives/6704/
Demo (normal merge) 
86
Demo (rebase + merge) 
87
注意事項 (1) 
‧必須要加 --no-ff 才會有 merge commit。不然 
會是 fast-forward。 
‧rebase 之後的 feature branch 就不要再 push 
出去了 
‧如果有遠端的 feature branch,合併完也砍掉 
88
‧不求⼀一次 rebase 到完美,不然中間的 conflict 
會搞混。 
‧可以⼀一次改點東⻄西就 rebase ⼀一次,然後開個臨 
時的 branch 存檔起來,再繼續 rebase ⾃自⼰己直到 
滿意為⽌止。 
89 
注意事項 (2)
Rebase demo screencast 
‧http://ihower.tw/blog/archives/6704 
90
2. Feature branch issue 
‧根據 Feature 或 User Story 來建⽴立 Branch 開 
發,直到 Branch 驗收完成才合併回主幹 
‧可讓主幹總是 releasable 
‧缺點是與主幹的分歧造成合併問題和不利於 CI 
–主幹如果有更新,Feature Branch 必須經常去合併回 
來 
–Feature Branch 週期越短越好,幾天到不超過⼀一個開 
發週期 (Iteration) 
–不要同時開太多 Feature Branch (避免半成品) 
–需要⼀一個 Tech Lead 來負責主幹的合併
Feature Branch (cont.) 
‧對 Open Source Project 來說⾮非常有效率 
–Small core team 可以主導要不要接受 patch 
–Release date 不固定,可以慢慢考慮 patch 
‧但對⼤大型商業團隊來說,可能變成 Anti-pattern 
(超⻑⾧長 branch 合併不回來)。團隊需要良好紀律: 
–codebase 需要良好模組化 
–⼤大家都乖乖定期更新主幹的 code,並且經常 
commit 程式到主幹 
–Delivery team 不能因為時程壓⼒力⽽而輕率 merge 
92
反思 Develop on Mainline ? 
from Continuous Delivery ch.14 
‧以唯⼀一的 Mainline branch 作為開發⽤用途 
–還是可以開以不合併為前提的 branch,例如 release 
branch 或 spike 實驗 
‧Continuous Integration 最好做,程式碼總是 
integrated 的狀態 
‧開發者總是拿到最新的 code 
‧避免開發後期 merge hell 和 integration hell 的問 
題 
‧缺點:releasable 程度可能降低,必須在架構設 
計上有增量式開發的能⼒力和技巧。
在單⼀一Branch 上做 
增量式開發的技巧 
‧Feature Toggle 
http://martinfowler.com/bliki/FeatureToggle.html 
‧Branch By Abstraction 
http://continuousdelivery.com/2011/05/make-large-scale-changes-incrementally-with-branch- 
by-abstraction/ 
94
Feature Toggle 
• 功能完成,不代表業務⾯面需要⾺馬上上線 
• 擔⼼心 Feature branch 放太久臭掉? 
• 建議先合併進主幹! 但是 UI 先藏起來即可! 
• 甚⾄至是設計權限,讓 admin 可以提早看 
到做 production 的線上測試
3. Code Review Policy 
‧規定只有 project leaders 可以 commit/merge 進 
develop branch。 
‧規定只有 release team 可以管理 master branch。 
‧其他開發者都開 topic branches,完成後發 pull 
request,做完 code review 後,沒問題才 merge 
進 develop。 
‧例如:GitHub 的 pull request 可以作,但沒有強 
制性
GitLab
Android 
• Repo is a repository management tool that 
we built on top of Git 
• Gerrit is a web-based code review system 
for projects that use git 
https://source.android.com/source/developing.html
Facebook 
• http://phabricator.org/
4. Release branch? 
釋出週期越短 
每天或 Continuous Deployment 
釋出週期越⻑⾧長 
數週或需要等 app store 審核 
可以不需要 Release branch 
直接主幹當 production 版 
需要較穩定的 
Release branches
開發與佈署流程 
http://ihower.tw/blog/archives/7798 
‧分⽀支流程不只與開發相關,也與測試和軟體 
部署(釋出)流程相關 
‧什麼時候,哪個分⽀支跑 CI ⾃自動測試? 
‧什麼時候,哪個分⽀支佈署到 Staging Server 進 
⾏行⼈人⼯工測試? 
‧使⽤用 Github flow 的話,需要讓每個 feature branch 都 
可以上 CI 和 staging server 環境 
‧或是採⽤用 CMake 解法,有⼀一個 next(staging) branch 
專⾨門⽤用來整合跑 CI,這個 branch 不合併回 master 
104
與專案管理的搭配 
• Scrum (有 iteration 開發週期) 
• ⽐比較適合搭配有 release branch 的 Git flow 
• Kanban (沒有 iteration 開發週期) 
• ⽐比較適合 Github flow 流程
Part2 
TL;DR ⼩小結 
• Github flow 或 Gitflow ⼆二選⼀一 
• desktop/mobile software:⽤用 Gitflow 
• 想要嚴謹的流程:⽤用 Gitflow 
• 頻繁釋出的 Web app: ⽤用 Github flow
謝謝,請多指教 
http://ihower.tw
參考資料 
• http://ihower.tw/blog/category/git 
• http://pragprog.com/screencasts/v-jwsceasy/source-control-made-easy 
• http://www.youtube.com/watch?v=4XpnKHJAok8 Linux 的演講 
• http://www.softdevtube.com/2013/02/05/advanced-git/ 
• http://git-scm.com/book 
• Git from the bottom up 
http://ftp.newartisans.com/pub/git.from.bottom.up.pdf 
• Version Control with Git, O'Reilly 
• http://nfarina.com/post/9868516270/git-is-simpler 
• http://think-like-a-git.net/sections/graph-theory.html
‧Git in Practice, Manning 
‧https://peepcode.com/products/git 
‧https://peepcode.com/products/advanced-git 
‧Git Internals, Peepcode 
‧Pragmatic Version Control Using Git, Pragmatic 
‧Pragmatic Guide to Git, Pragmatic 
‧Continuous Delivery Ch.14 
‧https://www.atlassian.com/git/tutorials/comparing-workflows 
‧https://guides.github.com/introduction/flow/index.html

More Related Content

What's hot

デザイナのためのGit入門
デザイナのためのGit入門デザイナのためのGit入門
デザイナのためのGit入門dsuke Takaoka
 
やりなおせる Git 入門
やりなおせる Git 入門やりなおせる Git 入門
やりなおせる Git 入門Tomohiko Himura
 
社内Git勉強会向け資料
社内Git勉強会向け資料社内Git勉強会向け資料
社内Git勉強会向け資料Hiroki Saiki
 
ノンプログラマでも今日から使える「Git」でバージョン管理
ノンプログラマでも今日から使える「Git」でバージョン管理ノンプログラマでも今日から使える「Git」でバージョン管理
ノンプログラマでも今日から使える「Git」でバージョン管理H2O Space. Co., Ltd.
 
Git and GitHub | Concept about Git and GitHub Process | Git Process overview
Git and GitHub | Concept about Git and GitHub Process | Git Process overviewGit and GitHub | Concept about Git and GitHub Process | Git Process overview
Git and GitHub | Concept about Git and GitHub Process | Git Process overviewRueful Robin
 
Advanced Git Tutorial
Advanced Git TutorialAdvanced Git Tutorial
Advanced Git TutorialSage Sharp
 
Learning git
Learning gitLearning git
Learning gitSid Anand
 
いつやるの?Git入門 v1.1.0
いつやるの?Git入門 v1.1.0いつやるの?Git入門 v1.1.0
いつやるの?Git入門 v1.1.0Masakazu Matsushita
 
Learning Git and GitHub - BIT GDSC.pdf
Learning Git and GitHub - BIT GDSC.pdfLearning Git and GitHub - BIT GDSC.pdf
Learning Git and GitHub - BIT GDSC.pdfJayprakash677449
 
新人Git/Github研修公開用スライド(その1)
新人Git/Github研修公開用スライド(その1)新人Git/Github研修公開用スライド(その1)
新人Git/Github研修公開用スライド(その1)pupupopo88
 
Présentation de git
Présentation de gitPrésentation de git
Présentation de gitJulien Blin
 
Git and GitHub workflows
Git and GitHub workflowsGit and GitHub workflows
Git and GitHub workflowsArthur Shvetsov
 
Github - Git Training Slides: Foundations
Github - Git Training Slides: FoundationsGithub - Git Training Slides: Foundations
Github - Git Training Slides: FoundationsLee Hanxue
 
Git One Day Training Notes
Git One Day Training NotesGit One Day Training Notes
Git One Day Training Notesglen_a_smith
 

What's hot (20)

いつやるの?Git入門
いつやるの?Git入門いつやるの?Git入門
いつやるの?Git入門
 
Git 版本控制 (使用教學)
Git 版本控制 (使用教學)Git 版本控制 (使用教學)
Git 版本控制 (使用教學)
 
デザイナのためのGit入門
デザイナのためのGit入門デザイナのためのGit入門
デザイナのためのGit入門
 
やりなおせる Git 入門
やりなおせる Git 入門やりなおせる Git 入門
やりなおせる Git 入門
 
社内Git勉強会向け資料
社内Git勉強会向け資料社内Git勉強会向け資料
社内Git勉強会向け資料
 
ノンプログラマでも今日から使える「Git」でバージョン管理
ノンプログラマでも今日から使える「Git」でバージョン管理ノンプログラマでも今日から使える「Git」でバージョン管理
ノンプログラマでも今日から使える「Git」でバージョン管理
 
Git and GitHub | Concept about Git and GitHub Process | Git Process overview
Git and GitHub | Concept about Git and GitHub Process | Git Process overviewGit and GitHub | Concept about Git and GitHub Process | Git Process overview
Git and GitHub | Concept about Git and GitHub Process | Git Process overview
 
Introduction To Git
Introduction To GitIntroduction To Git
Introduction To Git
 
Git 教學
Git 教學Git 教學
Git 教學
 
git and github
git and githubgit and github
git and github
 
Advanced Git Tutorial
Advanced Git TutorialAdvanced Git Tutorial
Advanced Git Tutorial
 
Learning git
Learning gitLearning git
Learning git
 
いつやるの?Git入門 v1.1.0
いつやるの?Git入門 v1.1.0いつやるの?Git入門 v1.1.0
いつやるの?Git入門 v1.1.0
 
Learning Git and GitHub - BIT GDSC.pdf
Learning Git and GitHub - BIT GDSC.pdfLearning Git and GitHub - BIT GDSC.pdf
Learning Git and GitHub - BIT GDSC.pdf
 
新人Git/Github研修公開用スライド(その1)
新人Git/Github研修公開用スライド(その1)新人Git/Github研修公開用スライド(その1)
新人Git/Github研修公開用スライド(その1)
 
Présentation de git
Présentation de gitPrésentation de git
Présentation de git
 
Git and GitHub workflows
Git and GitHub workflowsGit and GitHub workflows
Git and GitHub workflows
 
Git training v10
Git training v10Git training v10
Git training v10
 
Github - Git Training Slides: Foundations
Github - Git Training Slides: FoundationsGithub - Git Training Slides: Foundations
Github - Git Training Slides: Foundations
 
Git One Day Training Notes
Git One Day Training NotesGit One Day Training Notes
Git One Day Training Notes
 

Viewers also liked

Git 實務圖解
Git 實務圖解Git 實務圖解
Git 實務圖解Pokai Chang
 
A brief introduction to Machine Learning
A brief introduction to Machine LearningA brief introduction to Machine Learning
A brief introduction to Machine LearningWen-Tien Chang
 
Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby (with presentation note)Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby (with presentation note)Wen-Tien Chang
 
Exception Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyException Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyWen-Tien Chang
 
淺談 Startup 公司的軟體開發流程 v2
淺談 Startup 公司的軟體開發流程 v2淺談 Startup 公司的軟體開發流程 v2
淺談 Startup 公司的軟體開發流程 v2Wen-Tien Chang
 
網站自動化測試
網站自動化測試網站自動化測試
網站自動化測試Bruce Chen
 
git, repo, Gerrit 基礎教學
git, repo, Gerrit 基礎教學git, repo, Gerrit 基礎教學
git, repo, Gerrit 基礎教學Doremi Lin
 
Introduction to Git/Github - A beginner's guide
Introduction to Git/Github - A beginner's guideIntroduction to Git/Github - A beginner's guide
Introduction to Git/Github - A beginner's guideRohit Arora
 
Git 101: Git and GitHub for Beginners
Git 101: Git and GitHub for Beginners Git 101: Git and GitHub for Beginners
Git 101: Git and GitHub for Beginners HubSpot
 

Viewers also liked (9)

Git 實務圖解
Git 實務圖解Git 實務圖解
Git 實務圖解
 
A brief introduction to Machine Learning
A brief introduction to Machine LearningA brief introduction to Machine Learning
A brief introduction to Machine Learning
 
Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby (with presentation note)Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby (with presentation note)
 
Exception Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyException Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in Ruby
 
淺談 Startup 公司的軟體開發流程 v2
淺談 Startup 公司的軟體開發流程 v2淺談 Startup 公司的軟體開發流程 v2
淺談 Startup 公司的軟體開發流程 v2
 
網站自動化測試
網站自動化測試網站自動化測試
網站自動化測試
 
git, repo, Gerrit 基礎教學
git, repo, Gerrit 基礎教學git, repo, Gerrit 基礎教學
git, repo, Gerrit 基礎教學
 
Introduction to Git/Github - A beginner's guide
Introduction to Git/Github - A beginner's guideIntroduction to Git/Github - A beginner's guide
Introduction to Git/Github - A beginner's guide
 
Git 101: Git and GitHub for Beginners
Git 101: Git and GitHub for Beginners Git 101: Git and GitHub for Beginners
Git 101: Git and GitHub for Beginners
 

Similar to Git 版本控制系統 -- 從微觀到宏觀

Git in a nutshell
Git in a nutshellGit in a nutshell
Git in a nutshellNelson Tai
 
Git 使用介绍
Git 使用介绍Git 使用介绍
Git 使用介绍medcl
 
Yet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom upYet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom upWen-Tien Chang
 
Git 入门实战
Git 入门实战Git 入门实战
Git 入门实战icy leaf
 
Git tutorial for windows user (給 Windows user 的 Git 教學)
Git tutorial for windows user (給 Windows user 的 Git 教學)Git tutorial for windows user (給 Windows user 的 Git 教學)
Git tutorial for windows user (給 Windows user 的 Git 教學)Cloud Tu
 
Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 超簡單學習懶人包(軟體程式版本控管系統)Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 超簡單學習懶人包(軟體程式版本控管系統)flylon
 
Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹PingLun Liao
 
Android 程式設計(4)
Android 程式設計(4)Android 程式設計(4)
Android 程式設計(4)Roy Wang
 
Learn git
Learn gitLearn git
Learn git甘 李
 
Git+使用教程
Git+使用教程Git+使用教程
Git+使用教程gemron
 
Learning to Use Git | WeiYuan
Learning to Use Git | WeiYuanLearning to Use Git | WeiYuan
Learning to Use Git | WeiYuanWei-Yuan Chang
 
Github简介及实用入门
Github简介及实用入门Github简介及实用入门
Github简介及实用入门Rongxing Liu
 
Git Essence Tutorial
Git Essence TutorialGit Essence Tutorial
Git Essence TutorialHo Kim
 
Git introduction
Git introductionGit introduction
Git introductionmythnc
 
Git flow 與團隊合作
Git flow 與團隊合作Git flow 與團隊合作
Git flow 與團隊合作Bo-Yi Wu
 
Git flow
Git flowGit flow
Git flowshaokun
 
Git & git flow
Git & git flowGit & git flow
Git & git flowAmo Wu
 
COSCUP 2015 開源之道-Git工作坊教學簡報
COSCUP 2015 開源之道-Git工作坊教學簡報COSCUP 2015 開源之道-Git工作坊教學簡報
COSCUP 2015 開源之道-Git工作坊教學簡報Bachue Zhou
 
Git 入門與實作
Git 入門與實作Git 入門與實作
Git 入門與實作奕浦 郭
 

Similar to Git 版本控制系統 -- 從微觀到宏觀 (20)

Git in a nutshell
Git in a nutshellGit in a nutshell
Git in a nutshell
 
Git 使用介绍
Git 使用介绍Git 使用介绍
Git 使用介绍
 
Yet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom upYet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom up
 
Git 入门实战
Git 入门实战Git 入门实战
Git 入门实战
 
Git tutorial for windows user (給 Windows user 的 Git 教學)
Git tutorial for windows user (給 Windows user 的 Git 教學)Git tutorial for windows user (給 Windows user 的 Git 教學)
Git tutorial for windows user (給 Windows user 的 Git 教學)
 
Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 超簡單學習懶人包(軟體程式版本控管系統)Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 超簡單學習懶人包(軟體程式版本控管系統)
 
Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹
 
Android 程式設計(4)
Android 程式設計(4)Android 程式設計(4)
Android 程式設計(4)
 
Learn git
Learn gitLearn git
Learn git
 
Git+使用教程
Git+使用教程Git+使用教程
Git+使用教程
 
Learning to Use Git | WeiYuan
Learning to Use Git | WeiYuanLearning to Use Git | WeiYuan
Learning to Use Git | WeiYuan
 
Github简介及实用入门
Github简介及实用入门Github简介及实用入门
Github简介及实用入门
 
Git Essence Tutorial
Git Essence TutorialGit Essence Tutorial
Git Essence Tutorial
 
Git introduction
Git introductionGit introduction
Git introduction
 
Git Tutorial
Git TutorialGit Tutorial
Git Tutorial
 
Git flow 與團隊合作
Git flow 與團隊合作Git flow 與團隊合作
Git flow 與團隊合作
 
Git flow
Git flowGit flow
Git flow
 
Git & git flow
Git & git flowGit & git flow
Git & git flow
 
COSCUP 2015 開源之道-Git工作坊教學簡報
COSCUP 2015 開源之道-Git工作坊教學簡報COSCUP 2015 開源之道-Git工作坊教學簡報
COSCUP 2015 開源之道-Git工作坊教學簡報
 
Git 入門與實作
Git 入門與實作Git 入門與實作
Git 入門與實作
 

More from Wen-Tien Chang

⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨Wen-Tien Chang
 
Ruby Rails 老司機帶飛
Ruby Rails 老司機帶飛Ruby Rails 老司機帶飛
Ruby Rails 老司機帶飛Wen-Tien Chang
 
RSpec on Rails Tutorial
RSpec on Rails TutorialRSpec on Rails Tutorial
RSpec on Rails TutorialWen-Tien Chang
 
ALPHAhackathon: How to collaborate
ALPHAhackathon: How to collaborateALPHAhackathon: How to collaborate
ALPHAhackathon: How to collaborateWen-Tien Chang
 
從 Classes 到 Objects: 那些 OOP 教我的事
從 Classes 到 Objects: 那些 OOP 教我的事從 Classes 到 Objects: 那些 OOP 教我的事
從 Classes 到 Objects: 那些 OOP 教我的事Wen-Tien Chang
 
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩Wen-Tien Chang
 
Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Wen-Tien Chang
 
A brief introduction to SPDY - 邁向 HTTP/2.0
A brief introduction to SPDY - 邁向 HTTP/2.0A brief introduction to SPDY - 邁向 HTTP/2.0
A brief introduction to SPDY - 邁向 HTTP/2.0Wen-Tien Chang
 
RubyConf Taiwan 2012 Opening & Closing
RubyConf Taiwan 2012 Opening & ClosingRubyConf Taiwan 2012 Opening & Closing
RubyConf Taiwan 2012 Opening & ClosingWen-Tien Chang
 
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean StartupWen-Tien Chang
 
那些 Functional Programming 教我的事
那些 Functional Programming 教我的事那些 Functional Programming 教我的事
那些 Functional Programming 教我的事Wen-Tien Chang
 
RubyConf Taiwan 2011 Opening & Closing
RubyConf Taiwan 2011 Opening & ClosingRubyConf Taiwan 2011 Opening & Closing
RubyConf Taiwan 2011 Opening & ClosingWen-Tien Chang
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit TestingWen-Tien Chang
 
RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試Wen-Tien Chang
 
Service-Oriented Design and Implement with Rails3
Service-Oriented Design and Implement with Rails3Service-Oriented Design and Implement with Rails3
Service-Oriented Design and Implement with Rails3Wen-Tien Chang
 

More from Wen-Tien Chang (20)

⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨
 
Ruby Rails 老司機帶飛
Ruby Rails 老司機帶飛Ruby Rails 老司機帶飛
Ruby Rails 老司機帶飛
 
RSpec on Rails Tutorial
RSpec on Rails TutorialRSpec on Rails Tutorial
RSpec on Rails Tutorial
 
RSpec & TDD Tutorial
RSpec & TDD TutorialRSpec & TDD Tutorial
RSpec & TDD Tutorial
 
ALPHAhackathon: How to collaborate
ALPHAhackathon: How to collaborateALPHAhackathon: How to collaborate
ALPHAhackathon: How to collaborate
 
從 Classes 到 Objects: 那些 OOP 教我的事
從 Classes 到 Objects: 那些 OOP 教我的事從 Classes 到 Objects: 那些 OOP 教我的事
從 Classes 到 Objects: 那些 OOP 教我的事
 
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
 
Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介
 
A brief introduction to SPDY - 邁向 HTTP/2.0
A brief introduction to SPDY - 邁向 HTTP/2.0A brief introduction to SPDY - 邁向 HTTP/2.0
A brief introduction to SPDY - 邁向 HTTP/2.0
 
RubyConf Taiwan 2012 Opening & Closing
RubyConf Taiwan 2012 Opening & ClosingRubyConf Taiwan 2012 Opening & Closing
RubyConf Taiwan 2012 Opening & Closing
 
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup
 
那些 Functional Programming 教我的事
那些 Functional Programming 教我的事那些 Functional Programming 教我的事
那些 Functional Programming 教我的事
 
RubyConf Taiwan 2011 Opening & Closing
RubyConf Taiwan 2011 Opening & ClosingRubyConf Taiwan 2011 Opening & Closing
RubyConf Taiwan 2011 Opening & Closing
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit Testing
 
RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試
 
Git and Github
Git and GithubGit and Github
Git and Github
 
Service-Oriented Design and Implement with Rails3
Service-Oriented Design and Implement with Rails3Service-Oriented Design and Implement with Rails3
Service-Oriented Design and Implement with Rails3
 
Rails3 changesets
Rails3 changesetsRails3 changesets
Rails3 changesets
 
遇見 Ruby on Rails
遇見 Ruby on Rails遇見 Ruby on Rails
遇見 Ruby on Rails
 
Designing Ruby APIs
Designing Ruby APIsDesigning Ruby APIs
Designing Ruby APIs
 

Git 版本控制系統 -- 從微觀到宏觀

  • 2. 我是誰 • 張⽂文鈿 a.k.a. ihower • http://ihower.tw • Instructor at ALPHA Camp • http://alphacamp.tw • Git user since 2008
  • 3. Agenda • 微觀:Git 的內部設計 • 宏觀:Git 的分⽀支開發流程和策略 不是 Git ⼊入⾨門教學!
  • 5. ⼩小測驗 如何⽤用 Git 底層指令,不⽤用 git add 和 git commit 指令進⾏行 commit 動作?
  • 8. git add . (將⺫⽬目錄節點和檔案內容節點關聯起來) 檔案內容A 檔案內容B ⺫⽬目錄
  • 9. 檔案內容A V1 檔案內容B V1 git commit (產⽣生commit節點,指向⺫⽬目錄節點) ⺫⽬目錄 V1 Commit V1 接下來我們修改檔案 A 成為 V2 版本,檔案B不變
  • 10. 檔案內容B V1 git commit (cont.) (產⽣生commit V2節點,指向parent commit節點) ⺫⽬目錄 V1 Commit V1 檔案內容A V2 ⺫⽬目錄 V2 Commit V2 檔案內容A V1 檔案內容B不變
  • 11. Git is objects database Blob object Blob object Tree object Commit object
  • 12. 儲存內容(demo) • git init • echo hello > hello.txt • git add . • tree .git • 存在 .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a • 這是 hello 內容的 SHA1 • printf "blob 6x00hellon" | shasum • echo "hello" | git hash-object --stdin • git cat-file -p ce0136
  • 13. ce0136 hello blob blob object 的實際檔案名稱 .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
  • 14. Blob object • Git 是 Content-addressable filesystem • Blob 沒有 metadata,沒有檔名資訊 • Blob object 的儲存檔名,是根據內容產⽣生的 SHA1 • 內容⼀一樣的檔案,根據 SHA1 演算法只會存 成同⼀一份檔案,不會浪費空間
  • 15. 儲存⺫⽬目錄 (demo) • git write-tree (根據 staging area 產⽣生 Tree object) • git cat-file -p aaa96c
  • 16. Tree aaa96c ce0136 100644 blob ce0136 hello.txt Tree object 的實際檔案名稱 .git/objects/aa/a96ced2d9a1c8e72c56b253a0e2fe78393feb7 hello blob 其中 100644 為檔案模式,表⽰示這是⼀一個普通檔案 100755 表⽰示可執⾏行檔,120000 表⽰示 symbolic link
  • 17. 儲存⺫⽬目錄 (cont.) • 新增兩個空⽩白檔案和⼦子⺫⽬目錄 • touch hola.txt & mkdir lib & touch lib/bonjour.txt • git add . • git write-tree • git cat-file -p 16e19f (觀察這個 tree) • git cat-file -p e69de2 (觀察其中的 lib tree)
  • 18. Tree 040000 tree 122f77 lib 100644 blob ce0136 hello.txt 100644 blob e69de2 hola.txt 100644 blob e69de2 bonjour.txt hello blob ce0136 16e19f blob e69de2 Tree 122f77
  • 19. Tree object • Git ⽤用 Tree object 把 Blob object 組織起來,包 括檔案命名和⺫⽬目錄結構 • Blob object 並沒有包含檔案名稱和⺫⽬目錄結構 • Tree object 裡⾯面還可以有 Tree object ⼦子⺫⽬目錄 • Tree object 的檔名,⼀一樣是根據內容產⽣生 SHA1
  • 20. 遞交 Commit (demo) • git commit-tree 16e19f -m “First commit” • git cat-file -p 107aff • cat .git/HEAD • cat .git/refs/heads/master • git update-ref refs/heads/master 107aff • git rev-parse HEAD
  • 21. Commit object 指向 root tree SHA1 Tree 16e19f 040000 tree 122f77 lib 100644 blob ce0136 hello.txt 100644 blob e69de2 hola.txt HEAD master commit 107aff tree 16e19f author ihower 1375381139 +0800 committer ihower1375381139 +0800 First commit
  • 22. 再次遞交 Commit (demo) • 修改 hola.txt 檔案,加⼊入 hola 字串 • git commit -am “Second commit” • git cat-file -p 309be07
  • 23. Commit object 指向 parent commit SHA1 Tree Tree fd44ed 117a5 040000 tree 122f77 lib 100644 blob ce0136 hello.txt 100644 blob e69de2 hola.txt commit 309be07 commit 107aff tree 117a5b author ihower 1375381139 +0800 committer ihower1375381139 +0800 First commit 040000 tree 122f77 lib 100644 blob ce0136 hello.txt 100644 blob 5c1b14 hola.txt tree fd44ed parent 107aff author ihower 1375381779 +0800 committer ihower 1375381779 +0800 Second commit
  • 24. Commit object • 紀錄 root tree SHA1 • 紀錄 parent commit SHA1 • 紀錄作者、時間和 commit message 資訊 • Commit object 的檔名,⼀一樣是根據內容產⽣生 SHA1
  • 25. Git commit 動作流程 • ⽤用內容產⽣生 blob object • 寫⼊入 file mode, blob SHA1, file name 到 staging area • 根據 staging area 產⽣生 Tree object • ⽤用 root tree SHA1 和 parent commit SHA1 產⽣生 commit object • ⽤用 commit SHA1 更新 master 參考
  • 26. 如何不⽤用 git add 和 git commit 指令進⾏行 commit 動作? echo "hola" | git hash-object -w --stdin git update-index --add --cacheinfo 100644 5c1b14949828006ed75a3e8858957f86a2f7e2eb hola.txt git write-tree git commit-tree 27b9d5 -m "Second commit" -p 30b060 git update-ref refs/heads/master 97b806c9e5561a08e0df1f1a60857baad3a1f02e git add git commit https://gist.github.com/ihower/6132576 測驗解答
  • 27. Tag object (Tag 分兩種:annotated tag 才會產⽣生 object) • git tag -a release • git rev-parse release • git cat-file -p 2450f3
  • 28. tag 2450f3 object 309be0 commit 309be0 type commit tag release tagger ihower 1375383070 +0800 Release!
  • 29. ⼩小結論: Git 有四種 Objects • Blob • Tree • Commit • Tag
  • 30. References 參照 • 單純⼀一個檔案紀錄⼀一個 SHA1參照 • Tag reference • Branch reference • HEAD reference (指向⺫⽬目前所在的 branch)
  • 31. Tag reference • git tag tmp • cat .git/refs/tags/tmp • 不像 object 資訊豐富,reference 內容只 有 Commit object SHA1 refs/tags/tmp 309be0 commit 309be0
  • 32. Branch 和 HEAD reference • 每次 commit 就會變動 reference • HEAD 指向⺫⽬目前在哪⼀一個 branch • cat .git/HEAD • cat .git/refs/heads/master
  • 33. refs/heads/master 309be0 commit 309be0 HEAD ref: refs/heads/ master
  • 34. 如果在 Branch 上產⽣生新 Commit... refs/heads/master 309be0 commit 309be0 HEAD ref: refs/heads/ develop commit 55cbcb
  • 35. Branch reference 就會⾃自動 改指到新的 commit commit 309be0 refs/heads/master 55cbcb HEAD ref: refs/heads/ master commit 55cbcb
  • 36. 開新 Branch develop git branch develop refs/heads/master 55cbcb commit 55cbcb refs/heads/develop 55cbcb HEAD ref: refs/heads/ master
  • 37. 切換 Branch:改HEAD git checkout develop refs/heads/master 55cbcb commit 55cbcb refs/heads/develop 55cbcb HEAD ref: refs/heads/ develop
  • 38. 合併 Branch git merge develop da103e 40b603 commit commit commit refs/heads/master caa307 refs/heads/develop caa307 commit
  • 39. tree 5f398a5 parent 40b603 parent da103e author ihower 1375381779 +0800 committer ihower 1375381779 +0800 Merge branch ‘develop’ into master commit da103e 40b603 commit commit commit refs/heads/master caa307 refs/heads/develop caa307 commit 產⽣生的 merge commit 節點 有兩個 parents
  • 40. 另⼀一種合併情況 fast-forward 將 develop 合併進 master refs/heads/master (git merge develop) commit caa307 commi refs/heads/develop caa307 commi
  • 41. 另⼀一種合併情況 fast-forward 沒有產⽣生 merge 節點,只是移動參考 commi commi refs/heads/master caa307 refs/heads/develop caa307 commi
  • 42. Git 如何 Merge commits? • Git 進⾏行了⼀一個 Three-way merge 的動作 • three-way merge 除了要合併的兩個檔 案,還加上兩個檔案的共同祖先。如此 可以⼤大⼤大減少⼈人為處理 conflict 的情況。 • two-way merge 則只⽤用兩個檔案進⾏行合併 (svn預設即 two-way merge)
  • 43. Two-way merge 12 1234 A B 12? Conflict! 需要⼈人⼯工判斷
  • 44. 12 1234 A B 124 ⾃自動合併出 正確結果 123 Three-way merge: 先找出 AB 共同的祖先 -3 +4
  • 46. additive • 跟 Unix filesystem 有類似的結構,除了 • Git filesystem 的設計是⼀一直累加的,不會 有東⻄西被刪除 • Blob object 沒有 metadata
  • 47. Reference is cheap • 開新 branch 只是 refs ⽽而已,直到 commit 前都沒有負擔。 • 不像有些 VCS 開分⽀支會複製⼀一份原始 碼,⾮非常耗費資源。
  • 48. Integrity • SHA1 是內容的 checksum • 如果檔案內容有損毀,就會發現跟SHA1不 同。如果 tree 被偷改檔名,也會被發現。 • HEAD 指向的 SHA1,就是整個 repository 的 checksum • 這在分散式系統⾮非常重要:資料從⼀一個開 發者傳到另⼀一個開發者時,確保資料沒有 被修改。
  • 49. "I will, in fact, claim that the difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships." - Linus Torvalds
  • 50. Part2. Git 的分⽀支開發 流程和策略
  • 51. 1. 常⾒見的 Remote Repositories 的管理⽅方式
  • 52. 集中式⼯工作流程 團隊內部私有專案,⼤大家都有權限 Push 到共⽤用的 Repository 52 shared repository developer developer developer
  • 53. 集成管理員⼯工作流程 適合⼀一般 Open Source 專案,只有少部分⼈人有權限可以 Push 到 Repository,其他開發者⽤用 request pull 請求合併。 例如 GitHub 提供的 Fork 和 Pull Request 功能 53 developer repository developer repository committer developer repository official repository developer developer developer pull
  • 54. 司令官與副官⼯工作流程 多層權限控管,適合⼤大型 Open Source 專案,例如 Linux Kernel 54 official repository director developer lieutenant repository lieutenant repository developer developer developer
  • 56. Case Study • Github flow • Rails • CMake • Homebrew • Gitflow
  • 57. Github flow ‧master 是 stable/production 可佈署的版本 ‧任何開發從 master branch 分⽀支出 feature branch ‧送 pull request 開始進⾏行討論、code review 和測試 ‧最後合併回 master 代表可以佈署了 57 http://ihower.tw/blog/archives/7798
  • 59. pros and cons • 簡單、清楚、容易了解 • 搭配 Github 的 Pull Request 介⾯面 • 沒有 release branch,東⻄西⼀一進 master 就 上 production • Web app 如果有問題很容易 revert,如 果是 desktop/mobile 軟體就 GG 了
  • 60. Ruby on Rails ‧Rails ⺫⽬目前的版本是 4.1 ‧master 是開發版本,開發下⼀一次的主要版本 4.2 ‧feature branches 審核完後,合併進 master ‧maintenance branches,⽤用 cherry-pick 做 backporting –2-3-stable 已停⽌止維護 –3-2-stable security fixes –4-0-stable bugs fixes –4-1-stable bugs fixes 60
  • 61.
  • 62.
  • 63. pros and cons • 基本上就是 Github flow 加上 maintenance branches 維護舊版的設計 • ⾮非常多的 Open Source 專案採⽤用這種形式 • 版本號(Tag)打在 master 上,透過 preview 和 beta 的版本號進⾏行提前釋出
  • 64. CMake http://public.kitware.com/Wiki/Git/Workflow/Topic • master 預備釋出的版本,feature branches 從這裡分⽀支出去 • feature branch 完成後,合併進 next • next 整合版本,完成的 feature branch 先合併到這裡進⾏行測試 • 在 next 測好的 feature branch,才合併進 master • 可以將 master 合併進 next,減少之後的 code conflicts • 不會將 next 合併進 master • nightly 每天 1:00 UTC ⾃自動從 next branch 分⽀支出來跑⾃自動測 試
  • 65.
  • 66. pros and cons • 個別 feature branch 的狀態很清楚 • 需要發⾏行 binary release 的軟體需要較正 式、較花時間的釋出過程(feature branch - > next -> master -> release branch)。來確 保釋出的軟體有⾜足夠的測試。 • 但是線圖其實很不好追蹤,很多 merge commits 跟真正的 commits 混雜在⼀一起 • Revert a merge commit 其實不好做
  • 67. Homebrew • 只有 master branch,沒有 merge commit • 所有 feature branch ⼀一律 rebase 和 squashed 後進主幹
  • 68.
  • 69. pros and cons • Homebrew 不只把 Git 當 VCS,甚⾄至當成 delivery 部署機制 • 歷史紀錄超級 Readable 容易讀 • 沒有 release branch,釋出很快。有 bug 也很容易 revert、re-apply 或 bisect • 合併前的 branch commits 訊息丟失
  • 71. 兩個主要分⽀支 ‧master: 穩定的 production 版 ‧develop: 開發版本,從 master 分⽀支出來 71
  • 72.
  • 73. 三種⽀支援性分⽀支(1) ‧Feature branches –開發新功能或修 bugs –從 develop 分⽀支出來 –完成後 merge 回 develop –如果開發時間較⻑⾧長,則需定期同步 develop 主幹的 程式(初學可⽤用 merge,建議改⽤用 rebase),不然最後會合併不回 去。 73
  • 74.
  • 75.
  • 76. 三種⽀支援性分⽀支(2) ‧Release branches –準備要 release 的版本,只修 bugs –從 develop 分⽀支出來 –完成後 merge 回 master 和 develop 76
  • 77. 三種⽀支援性分⽀支(3) ‧Hotfix branches –等不及 release 版本就必須⾺馬上修 master 趕著上線 –會從 master 分⽀支出來 –完成後 merge 回 master 和 develop 77
  • 78.
  • 79.
  • 80. pros and cons • 第⼀一個出名的、有形式的 Git 分⽀支⼯工作流 程 • 很清楚的追蹤 feature, release, hotfix 等 branches,確保釋出和審核流程 • 有點複雜,不適合⼩小專案或 Open Source 專案 • 不適合頻繁釋出(Continuous Deployment)
  • 81. Discussions • Rebase before merge • Feature branch issue • Code review policy • Release branch
  • 82. 1. Rebase before merge • 在 merge 前,做不做 rebase 整理 commits? • 建議能⼒力和時間所及,就去做
  • 83. rebase + merge 的完美合併法 (假設我們要將 feature branch 合併回主幹 develop) ‧原因 –feature branch 很亂,不時 merge 與主幹同步 –feature branch 有 typo,commit 訊息想改 –feature branch 有些 commits 想合併或拆開 ‧作法 –先在 feature branch 做 git rebase develop -i –(反覆整理直到滿意) git rebase 分岔點 -i –在從 develop branch 做 git merge feature --no-ff 83
  • 84. 超級乾淨,每⼀一次的 merge commit 就代表⼀一個功能完成
  • 85. Demo (before merge) 85 http://ihower.tw/blog/archives/6704/
  • 87. Demo (rebase + merge) 87
  • 88. 注意事項 (1) ‧必須要加 --no-ff 才會有 merge commit。不然 會是 fast-forward。 ‧rebase 之後的 feature branch 就不要再 push 出去了 ‧如果有遠端的 feature branch,合併完也砍掉 88
  • 89. ‧不求⼀一次 rebase 到完美,不然中間的 conflict 會搞混。 ‧可以⼀一次改點東⻄西就 rebase ⼀一次,然後開個臨 時的 branch 存檔起來,再繼續 rebase ⾃自⼰己直到 滿意為⽌止。 89 注意事項 (2)
  • 90. Rebase demo screencast ‧http://ihower.tw/blog/archives/6704 90
  • 91. 2. Feature branch issue ‧根據 Feature 或 User Story 來建⽴立 Branch 開 發,直到 Branch 驗收完成才合併回主幹 ‧可讓主幹總是 releasable ‧缺點是與主幹的分歧造成合併問題和不利於 CI –主幹如果有更新,Feature Branch 必須經常去合併回 來 –Feature Branch 週期越短越好,幾天到不超過⼀一個開 發週期 (Iteration) –不要同時開太多 Feature Branch (避免半成品) –需要⼀一個 Tech Lead 來負責主幹的合併
  • 92. Feature Branch (cont.) ‧對 Open Source Project 來說⾮非常有效率 –Small core team 可以主導要不要接受 patch –Release date 不固定,可以慢慢考慮 patch ‧但對⼤大型商業團隊來說,可能變成 Anti-pattern (超⻑⾧長 branch 合併不回來)。團隊需要良好紀律: –codebase 需要良好模組化 –⼤大家都乖乖定期更新主幹的 code,並且經常 commit 程式到主幹 –Delivery team 不能因為時程壓⼒力⽽而輕率 merge 92
  • 93. 反思 Develop on Mainline ? from Continuous Delivery ch.14 ‧以唯⼀一的 Mainline branch 作為開發⽤用途 –還是可以開以不合併為前提的 branch,例如 release branch 或 spike 實驗 ‧Continuous Integration 最好做,程式碼總是 integrated 的狀態 ‧開發者總是拿到最新的 code ‧避免開發後期 merge hell 和 integration hell 的問 題 ‧缺點:releasable 程度可能降低,必須在架構設 計上有增量式開發的能⼒力和技巧。
  • 94. 在單⼀一Branch 上做 增量式開發的技巧 ‧Feature Toggle http://martinfowler.com/bliki/FeatureToggle.html ‧Branch By Abstraction http://continuousdelivery.com/2011/05/make-large-scale-changes-incrementally-with-branch- by-abstraction/ 94
  • 95. Feature Toggle • 功能完成,不代表業務⾯面需要⾺馬上上線 • 擔⼼心 Feature branch 放太久臭掉? • 建議先合併進主幹! 但是 UI 先藏起來即可! • 甚⾄至是設計權限,讓 admin 可以提早看 到做 production 的線上測試
  • 96. 3. Code Review Policy ‧規定只有 project leaders 可以 commit/merge 進 develop branch。 ‧規定只有 release team 可以管理 master branch。 ‧其他開發者都開 topic branches,完成後發 pull request,做完 code review 後,沒問題才 merge 進 develop。 ‧例如:GitHub 的 pull request 可以作,但沒有強 制性
  • 97.
  • 99.
  • 100. Android • Repo is a repository management tool that we built on top of Git • Gerrit is a web-based code review system for projects that use git https://source.android.com/source/developing.html
  • 101.
  • 103. 4. Release branch? 釋出週期越短 每天或 Continuous Deployment 釋出週期越⻑⾧長 數週或需要等 app store 審核 可以不需要 Release branch 直接主幹當 production 版 需要較穩定的 Release branches
  • 104. 開發與佈署流程 http://ihower.tw/blog/archives/7798 ‧分⽀支流程不只與開發相關,也與測試和軟體 部署(釋出)流程相關 ‧什麼時候,哪個分⽀支跑 CI ⾃自動測試? ‧什麼時候,哪個分⽀支佈署到 Staging Server 進 ⾏行⼈人⼯工測試? ‧使⽤用 Github flow 的話,需要讓每個 feature branch 都 可以上 CI 和 staging server 環境 ‧或是採⽤用 CMake 解法,有⼀一個 next(staging) branch 專⾨門⽤用來整合跑 CI,這個 branch 不合併回 master 104
  • 105. 與專案管理的搭配 • Scrum (有 iteration 開發週期) • ⽐比較適合搭配有 release branch 的 Git flow • Kanban (沒有 iteration 開發週期) • ⽐比較適合 Github flow 流程
  • 106. Part2 TL;DR ⼩小結 • Github flow 或 Gitflow ⼆二選⼀一 • desktop/mobile software:⽤用 Gitflow • 想要嚴謹的流程:⽤用 Gitflow • 頻繁釋出的 Web app: ⽤用 Github flow
  • 108. 參考資料 • http://ihower.tw/blog/category/git • http://pragprog.com/screencasts/v-jwsceasy/source-control-made-easy • http://www.youtube.com/watch?v=4XpnKHJAok8 Linux 的演講 • http://www.softdevtube.com/2013/02/05/advanced-git/ • http://git-scm.com/book • Git from the bottom up http://ftp.newartisans.com/pub/git.from.bottom.up.pdf • Version Control with Git, O'Reilly • http://nfarina.com/post/9868516270/git-is-simpler • http://think-like-a-git.net/sections/graph-theory.html
  • 109. ‧Git in Practice, Manning ‧https://peepcode.com/products/git ‧https://peepcode.com/products/advanced-git ‧Git Internals, Peepcode ‧Pragmatic Version Control Using Git, Pragmatic ‧Pragmatic Guide to Git, Pragmatic ‧Continuous Delivery Ch.14 ‧https://www.atlassian.com/git/tutorials/comparing-workflows ‧https://guides.github.com/introduction/flow/index.html