SlideShare a Scribd company logo
Git rebase -i
Charlie Tsai
Git rebase -i
Charlie Tsai
Agenda
• 修改多個commit message
• 合併commit
• 改變commit順序
• 嫁接branch
• 上述事情發生時,實際上背後git做了什麼
修改commit
• 改HEAD上的commit -> $ git commit —amend
• 改多個怎麼辦?
用—amend的case
N1
N0
N2
Checkout
Base
用—amend的case
N1
N0
N2
git commit —amend
Base
用—amend的case
N1
New
N0’
N2
Base
實際狀況
N1
New
N0’
N2
Base
N0
灰色表示detached
(想成孤兒就好)
用—amend的case
N1
New
N0
N2
Base
New
N1
只好cherry-pick N1再amend
(就算N1不改commit message
也要cherry-pick)
用—amend的case
N1
New
N0
N2
Base
New
N1
N2也要 cherry-pick再amend
New
N2
真心麻煩!
改用rebase
N1
N0
N2
Checkout
Base
改用rebase
N1
N0
N2
git rebase -i Base
Base
改用rebase
New
N1
New
N0
New
N2
Base
實際狀況
New
N1
New
N0
New
N2
Base
N1
N0
N2
一步到位!
改變commit順序
Base
N0
N1
git checkout
改變commit順序
Base
N0
N1
git rebase Base -i
改變commit順序
Base
N1
N0
實際狀況
Base
New N1
New N0
N0
N1
改順序的應用情境
• 整理branch上的commit
• 某個branch上的commit想先merge回master,就
可以先拉到branch的頭,把那個commit給merge
回master
合併commit
Base
N0
N1
合併commit
Base
New N0
實際狀況
Base
New N0
N0
N1
合併的使用情境
• 避免太過瑣碎的commit
• 寫新的class/module時可以step by step,最後再
整理成較少的commit
• 一次改比較多東西但又希望只有一個commit的時
候,中間可以先commit一些暫存點,最後再合併
(# 也可以用git stash, 但stash太多會亂)
嫁接branch
Base
Base_
1
N0
N1
N2
想把N0的前一個commit改到Base_1這點上
嫁接branch
Base
Base_
1
N0
N1
N2
git checkout
嫁接branch
Base
Base_
1
N0
N1
N2
git rebase -i Base_1
嫁接branch
Base
Base_
1
New
N0
New
N1
New
N2
實際狀況
Base
Base_
1
New
N0
New
N1
New
N2
N0
N1
N2
嫁接的使用情境
• 當你開發一個很大/需時較久時的功能
• 避免你開發到一半的branch沒法merge回master
也避免merge回去卻不會動時,沒有頭緒
• local有多個瑣碎但高度相關的branch,可以整理
成一個branch再merge回master
Question?

More Related Content

PDF
寫給大家的 Git 教學
PPTX
Git教學
PDF
Git 實務圖解
PPTX
Android Thread
ODP
Git 教學
ODP
Git 程式碼版本控制軟體介紹
PDF
How to Use Git?
PPTX
Git introduction
寫給大家的 Git 教學
Git教學
Git 實務圖解
Android Thread
Git 教學
Git 程式碼版本控制軟體介紹
How to Use Git?
Git introduction

Similar to Git rebase -i (20)

PPTX
Git基礎介紹
PPTX
git merge 與 rebase 的觀念與實務應用
ODP
Git basis - usage
PDF
Yet another introduction to Git - from the bottom up
PDF
Git 版本控制系統 -- 從微觀到宏觀
PDF
初心者 Git 上手攻略
PDF
連哈秋都懂的Git教學
PPTX
Git and git hub
PDF
Git in a nutshell
PPTX
Git & git hub v1.2
PDF
Introduction to git
PPT
Git簡報
PDF
Git Tutorial
PPT
Git 超簡單學習懶人包(軟體程式版本控管系統)
PDF
Git tutorial for windows user (給 Windows user 的 Git 教學)
PPTX
Git入門介紹
PDF
為自己學 Git
PDF
Git 入門與應用
PDF
Learning to Use Git | WeiYuan
PDF
Intro to Git 投影片
Git基礎介紹
git merge 與 rebase 的觀念與實務應用
Git basis - usage
Yet another introduction to Git - from the bottom up
Git 版本控制系統 -- 從微觀到宏觀
初心者 Git 上手攻略
連哈秋都懂的Git教學
Git and git hub
Git in a nutshell
Git & git hub v1.2
Introduction to git
Git簡報
Git Tutorial
Git 超簡單學習懶人包(軟體程式版本控管系統)
Git tutorial for windows user (給 Windows user 的 Git 教學)
Git入門介紹
為自己學 Git
Git 入門與應用
Learning to Use Git | WeiYuan
Intro to Git 投影片
Ad

Git rebase -i