32
Git rebase -i Charlie Tsai

Git rebase -i

Embed Size (px)

Citation preview

Page 1: Git rebase -i

Git rebase -iCharlie Tsai

Page 2: Git rebase -i

Git rebase -iCharlie Tsai

Page 3: Git rebase -i

Agenda• 修改多個 commit message• 合併 commit• 改變 commit 順序• 嫁接 branch• 上述事情發生時,實際上背後 git 做了什麼

Page 4: Git rebase -i

修改 commit

• 改 HEAD 上的 commit -> $ git commit —amend

• 改多個怎麼辦?

Page 5: Git rebase -i

用— amend 的 case

N1

N0

N2

Checkout

Base

Page 6: Git rebase -i

用— amend 的 case

N1

N0

N2

git commit —amend

Base

Page 7: Git rebase -i

用— amend 的 case

N1

New N0’

N2

Base

Page 8: Git rebase -i

實際狀況N1

New N0’

N2

Base

N0

灰色表示 detached( 想成孤兒就好 )

Page 9: Git rebase -i

用— amend 的 case

N1

New N0

N2

Base

New N1

只好 cherry-pick N1 再 amend( 就算 N1 不改 commit message也要 cherry-pick)

Page 10: Git rebase -i

用— amend 的 case

N1

New N0

N2

Base

New N1

N2 也要 cherry-pick 再 amend

New N2

Page 11: Git rebase -i

真心麻煩!

Page 12: Git rebase -i

改用 rebase

N1

N0

N2

CheckoutBase

Page 13: Git rebase -i

改用 rebase

N1

N0

N2

git rebase -i BaseBase

Page 14: Git rebase -i

改用 rebase

New N1

New N0

New N2

Base

Page 15: Git rebase -i

實際狀況New N1

New N0

New N2

Base

N1

N0

N2

Page 16: Git rebase -i

一步到位!

Page 17: Git rebase -i

改變 commit 順序

Base

N0

N1

git checkout

Page 18: Git rebase -i

改變 commit 順序

Base

N0

N1

git rebase Base -i

Page 19: Git rebase -i

改變 commit 順序

Base

N1

N0

Page 20: Git rebase -i

實際狀況

Base

New N1

New N0

N0

N1

Page 21: Git rebase -i

改順序的應用情境• 整理 branch 上的 commit• 某個 branch 上的 commit 想先 merge 回

master ,就可以先拉到 branch 的頭,把那個commit 給 merge 回 master

Page 22: Git rebase -i

合併 commit

Base

N0

N1

Page 23: Git rebase -i

合併 commit

Base

New N0

Page 24: Git rebase -i

實際狀況

Base

New N0

N0

N1

Page 25: Git rebase -i

合併的使用情境• 避免太過瑣碎的 commit• 寫新的 class/module 時可以 step by step ,最後再整理成較少的 commit• 一次改比較多東西但又希望只有一個 commit 的時候,中間可以先 commit 一些暫存點,最後再合併 (# 也可以用 git stash, 但 stash 太多會亂 )

Page 26: Git rebase -i

嫁接 branch

Base

Base_1

N0

N1

N2

想把 N0 的前一個 commit 改到 Base_1 這點上

Page 27: Git rebase -i

嫁接 branch

Base

Base_1

N0

N1

N2

git checkout

Page 28: Git rebase -i

嫁接 branch

Base

Base_1

N0

N1

N2

git rebase -i Base_1

Page 29: Git rebase -i

嫁接 branch

Base

Base_1

New N0

New N1

New N2

Page 30: Git rebase -i

實際狀況

Base

Base_1

New N0

New N1

New N2

N0

N1

N2

Page 31: Git rebase -i

嫁接的使用情境• 當你開發一個很大 / 需時較久時的功能• 避免你開發到一半的 branch 沒法 merge 回

master也避免 merge 回去卻不會動時,沒有頭緒• local 有多個瑣碎但高度相關的 branch ,可以整理成一個 branch 再 merge 回 master

Page 32: Git rebase -i

Question?