SlideShare a Scribd company logo
1 of 19
git merge 與 rebase 的
觀念與實務應用
多奇數位創意有限公司
技術總監 黃保翕 ( Will 保哥 )
部落格:http://blog.miniasp.com/
關於「合併」的觀念解析
The "Merge" Concepts
常見的三種合併方法 (Merge)
• 關於 Git 的快轉機制 (Fast-forward)
– 所謂的「快轉機制」,就是 Git 得知這個合併的過程,其實會依序套用
new_feature 原本就有的變更,所以在合併的時候會直接修改 master 分
支的 HEAD 參照位址,直接移動到 new_feature的 HEAD 那個版本。
• 正常的合併
– git checkout master
– git merge new_feature
• 不快轉的合併
– git checkout master
– git merge new_feature --no-ff
• 壓縮的合併
– git checkout master
– git merge new_feature --squash
不同的合併方式
• git merge <branch1> <branch2> <branch3> …
• git merge --no-ff
• git merge –squash
• git rebase <commit_id>
• git rebase <commit_id> -I
• git cherry-pick <commit_id>
• git revert <commit_id>
一般的合併方法
• 主要步驟
– 先切換至最終要合併完成的分支
• git checkout -b master
– 將另一個分支的變更合併回來
• git merge <new_feature_branch>
– 若合併成功
• 會自動引發一個新的 commit
– 若合併失敗 (發生衝突)
• 會停留在 Unmerged 狀態,需人工介入處理
• 處理完衝突,需繼續完成合併動作 (需做一次commit動作)
– git add
– git commit
• 放棄衝突,可選擇復原至合併前的狀態
– git reset --hard
安全的合併方法
• 方法 1
– git checkout -b merge1
– git merge <new_feature_branch>
– 若合併成功
• git checkout master
• git merge merge1 (Fast-forward)
• git branch –d merge1
– 若合併失敗
• git checkout master
• git branch -D merge1
安全的合併方法
• 方法 2
– git tag -f try_merge_master
– git merge <new_feature_branch>
– 若合併成功
• git tag -d try_merge_master
– 若合併失敗
• git reset try_merge_master --hard
• git tag -d try_merge_master
重新指定基礎位置 (Rebase)
• 將另一個分支當成目前分支的起點
– git checkout branch1
– git rebase master
– git rebase --continue
– git rebase --abort
– git rebase --skip
• 主要任務
– 先找出兩個分支之間的共同起點
– 將目前分支的所有變更套用在另一個分支
• 這就是所謂【重新指定基礎位置】的意思
• 是一個版本一個版本的套用變更
• 注意事項
– 不要在 git rebase 發生衝突的過程中執行 git commit 命令
– 如果真的做了,請改執行 git rebase --skip 跳過這一版套用
MERGE 與 REBASE 的線圖差異
• MERGE
– git checkout master
– git merge branch1
• REBASE
– git checkout branch1
– git rebase master
– git checkout master
– git merge branch1
• REBASE + MERGE --no-ff
– git checkout branch1
– git rebase master
– git checkout master
– git merge branch1 --no-ff
了解 MERGE 與 REBASE 的差異
• 相同之處
– 都是合併的一種,但合併的方式與步驟不同
• MERGE 如何合併
– 假設你有一個 new_feature 分支想合併回 master
– 你會這樣執行
• git checkout master
• git merge new_feature (會建立起一條分支線圖)
• REBASE 如何合併
– 假設你有一個 new_feature 分支想合併回 master
( 事實上是將自己的所有變更 commit 在 master 最新版後面)
– 你會這樣執行
• git checkout new_feature
• git rebase master (會讓 new_feature 併進 master 的線圖上)
• git checkout master
• git merge new_feature
Rebase 能做的事
• 0. 重新 commit (Pick)
• 1. 調換 commit 的順序
• 2. 修改 commit 的訊息 (Edit)
• 3. 插入一個 commit
• 4. 編輯一個 commit
• 5. 拆解一個 commit
• 6. 壓縮一個 commit,且合併訊息紀錄 (Squash)
• 7. 壓縮一個 commit,但丟棄版本紀錄
• 8. 刪除一個 commit (Skip)
關於「合併」的實務應用
The "Merge" Best Practices
正常合併
• 預設會啟用 快轉 (fast-forward) 機制
• 給自己專用的小分支可以直接這樣用的
– 允許一條線畫到底的情境
• 本地 功能分支 (feature branch) 常用
13
不快轉合併 ( --no-ff )
• 用來確保主要分支的線圖保持乾淨
• 本地分支合併回主線時使用
– 主線可能是 master 或 develop
• 遠端分支合併時預設採用這個模式
– GitHub
14
正常變基 ( Rebase )
• 用來確保整體分支線圖保持乾淨
• 通常是在 git pull 的時候發生使用
– 請記得:一個本地分支配一個遠端分支
• 還有在整理分支線圖時會用到
15
常用於有遠端儲存庫的情境
• git pull
– git fetch
– git merge
• git pull --rebase
– git fetch
– git rebase
– 此設定為 GitHub Desktop 的預設值
– 使用 git pull --rebase 可有效避免無謂的合併線圖
使用 git rebase 避免無謂的 merge | ihower { blogging }
http://ihower.tw/blog/archives/3843
• 把 rebase 當做 git pull 的預設
值
[branch "master"]
remote = origin
merge = refs/heads/master
rebase = true
總結
Summary
關於 Git 合併與變基
• Git 打從骨子裡都是分支與合併
• Git 的分支與合併既便宜又安全!
– git reflog
• 釐清 merge 與 rebase 的用途與使用情境
18
聯絡資訊
• The Will Will Web
記載著 Will 在網路世界的學習心得與技術分享
– http://blog.miniasp.com/
• Will 保哥的技術交流中心 (臉書粉絲專頁)
– http://www.facebook.com/will.fans
• Will 保哥的噗浪
– http://www.plurk.com/willh/invite
• Will 保哥的推特
– https://twitter.com/Will_Huang

More Related Content

What's hot

Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례
Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례
Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례Jemin Huh
 
Git 實務圖解
Git 實務圖解Git 實務圖解
Git 實務圖解Pokai Chang
 
寫給大家的 Git 教學
寫給大家的 Git 教學寫給大家的 Git 教學
寫給大家的 Git 教學littlebtc
 
Git基礎介紹
Git基礎介紹Git基礎介紹
Git基礎介紹Max Ma
 
Git - Basic Crash Course
Git - Basic Crash CourseGit - Basic Crash Course
Git - Basic Crash CourseNilay Binjola
 
Inside PostgreSQL Shared Memory
Inside PostgreSQL Shared MemoryInside PostgreSQL Shared Memory
Inside PostgreSQL Shared MemoryEDB
 
Introduction to Version Control
Introduction to Version ControlIntroduction to Version Control
Introduction to Version ControlJeremy Coates
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to GitLukas Fittl
 
[211] HBase 기반 검색 데이터 저장소 (공개용)
[211] HBase 기반 검색 데이터 저장소 (공개용)[211] HBase 기반 검색 데이터 저장소 (공개용)
[211] HBase 기반 검색 데이터 저장소 (공개용)NAVER D2
 
Version control system
Version control systemVersion control system
Version control systemAndrew Liu
 
Git flow for daily use
Git flow for daily useGit flow for daily use
Git flow for daily useMediacurrent
 
Learning git
Learning gitLearning git
Learning gitSid Anand
 

What's hot (20)

Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례
Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례
Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례
 
Git 實務圖解
Git 實務圖解Git 實務圖解
Git 實務圖解
 
Git and github 101
Git and github 101Git and github 101
Git and github 101
 
寫給大家的 Git 教學
寫給大家的 Git 教學寫給大家的 Git 教學
寫給大家的 Git 教學
 
Git 版本控制 (使用教學)
Git 版本控制 (使用教學)Git 版本控制 (使用教學)
Git 版本控制 (使用教學)
 
Git基礎介紹
Git基礎介紹Git基礎介紹
Git基礎介紹
 
Git - Basic Crash Course
Git - Basic Crash CourseGit - Basic Crash Course
Git - Basic Crash Course
 
Git由超淺入超深
Git由超淺入超深Git由超淺入超深
Git由超淺入超深
 
Git basics
Git basicsGit basics
Git basics
 
Inside PostgreSQL Shared Memory
Inside PostgreSQL Shared MemoryInside PostgreSQL Shared Memory
Inside PostgreSQL Shared Memory
 
Introduction to Version Control
Introduction to Version ControlIntroduction to Version Control
Introduction to Version Control
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to Git
 
[211] HBase 기반 검색 데이터 저장소 (공개용)
[211] HBase 기반 검색 데이터 저장소 (공개용)[211] HBase 기반 검색 데이터 저장소 (공개용)
[211] HBase 기반 검색 데이터 저장소 (공개용)
 
Git Rebase vs Merge
Git Rebase vs MergeGit Rebase vs Merge
Git Rebase vs Merge
 
Version control system
Version control systemVersion control system
Version control system
 
Git flow for daily use
Git flow for daily useGit flow for daily use
Git flow for daily use
 
git flow
git flowgit flow
git flow
 
Introduction git
Introduction gitIntroduction git
Introduction git
 
Git 101 for Beginners
Git 101 for Beginners Git 101 for Beginners
Git 101 for Beginners
 
Learning git
Learning gitLearning git
Learning git
 

Viewers also liked

ASP.NET 5 的創新與變革
ASP.NET 5 的創新與變革ASP.NET 5 的創新與變革
ASP.NET 5 的創新與變革Will Huang
 
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)Will Huang
 
簡介 GitHub 平台
簡介 GitHub 平台簡介 GitHub 平台
簡介 GitHub 平台Will Huang
 
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具Will Huang
 
Growth Mindset 經驗分享
Growth Mindset 經驗分享Growth Mindset 經驗分享
Growth Mindset 經驗分享Will Huang
 
Visual Studio 2015 與 Git 開發實戰
Visual Studio 2015 與 Git 開發實戰Visual Studio 2015 與 Git 開發實戰
Visual Studio 2015 與 Git 開發實戰Will Huang
 
開發人員不可不知的 Windows Container 容器技術預覽
開發人員不可不知的 Windows Container 容器技術預覽開發人員不可不知的 Windows Container 容器技術預覽
開發人員不可不知的 Windows Container 容器技術預覽Will Huang
 
Visual Studio 2017 新功能探索 (Study4.TW)
Visual Studio 2017 新功能探索 (Study4.TW)Visual Studio 2017 新功能探索 (Study4.TW)
Visual Studio 2017 新功能探索 (Study4.TW)Will Huang
 
簡介 Git hub 平台 ( 1.5 hrs )
簡介 Git hub 平台 ( 1.5 hrs )簡介 Git hub 平台 ( 1.5 hrs )
簡介 Git hub 平台 ( 1.5 hrs )Will Huang
 
SQL Server 資料庫版本控管
SQL Server 資料庫版本控管SQL Server 資料庫版本控管
SQL Server 資料庫版本控管Will Huang
 
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)Will Huang
 
初學者都該了解的 HTTP 通訊協定基礎
初學者都該了解的 HTTP 通訊協定基礎初學者都該了解的 HTTP 通訊協定基礎
初學者都該了解的 HTTP 通訊協定基礎Will Huang
 
DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略Will Huang
 
你所不知道的 Microsoft Azure 雲端資源採購技巧 (2016 Azure 新春特惠方案)
你所不知道的 Microsoft Azure 雲端資源採購技巧 (2016 Azure 新春特惠方案)你所不知道的 Microsoft Azure 雲端資源採購技巧 (2016 Azure 新春特惠方案)
你所不知道的 Microsoft Azure 雲端資源採購技巧 (2016 Azure 新春特惠方案)Will Huang
 
快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)Will Huang
 
中小企業選擇雲端服務的實戰密技
中小企業選擇雲端服務的實戰密技中小企業選擇雲端服務的實戰密技
中小企業選擇雲端服務的實戰密技Will Huang
 
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 TaiwanAzure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 TaiwanWill Huang
 
使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式Will Huang
 
Windows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWindows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWill Huang
 
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例Will Huang
 

Viewers also liked (20)

ASP.NET 5 的創新與變革
ASP.NET 5 的創新與變革ASP.NET 5 的創新與變革
ASP.NET 5 的創新與變革
 
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
 
簡介 GitHub 平台
簡介 GitHub 平台簡介 GitHub 平台
簡介 GitHub 平台
 
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
 
Growth Mindset 經驗分享
Growth Mindset 經驗分享Growth Mindset 經驗分享
Growth Mindset 經驗分享
 
Visual Studio 2015 與 Git 開發實戰
Visual Studio 2015 與 Git 開發實戰Visual Studio 2015 與 Git 開發實戰
Visual Studio 2015 與 Git 開發實戰
 
開發人員不可不知的 Windows Container 容器技術預覽
開發人員不可不知的 Windows Container 容器技術預覽開發人員不可不知的 Windows Container 容器技術預覽
開發人員不可不知的 Windows Container 容器技術預覽
 
Visual Studio 2017 新功能探索 (Study4.TW)
Visual Studio 2017 新功能探索 (Study4.TW)Visual Studio 2017 新功能探索 (Study4.TW)
Visual Studio 2017 新功能探索 (Study4.TW)
 
簡介 Git hub 平台 ( 1.5 hrs )
簡介 Git hub 平台 ( 1.5 hrs )簡介 Git hub 平台 ( 1.5 hrs )
簡介 Git hub 平台 ( 1.5 hrs )
 
SQL Server 資料庫版本控管
SQL Server 資料庫版本控管SQL Server 資料庫版本控管
SQL Server 資料庫版本控管
 
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
 
初學者都該了解的 HTTP 通訊協定基礎
初學者都該了解的 HTTP 通訊協定基礎初學者都該了解的 HTTP 通訊協定基礎
初學者都該了解的 HTTP 通訊協定基礎
 
DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略
 
你所不知道的 Microsoft Azure 雲端資源採購技巧 (2016 Azure 新春特惠方案)
你所不知道的 Microsoft Azure 雲端資源採購技巧 (2016 Azure 新春特惠方案)你所不知道的 Microsoft Azure 雲端資源採購技巧 (2016 Azure 新春特惠方案)
你所不知道的 Microsoft Azure 雲端資源採購技巧 (2016 Azure 新春特惠方案)
 
快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)
 
中小企業選擇雲端服務的實戰密技
中小企業選擇雲端服務的實戰密技中小企業選擇雲端服務的實戰密技
中小企業選擇雲端服務的實戰密技
 
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 TaiwanAzure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
 
使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式
 
Windows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWindows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, Kubernetes
 
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
 

Similar to git merge 與 rebase 的觀念與實務應用

Learn git
Learn gitLearn git
Learn 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 教學)Cloud Tu
 
Git 使用介绍
Git 使用介绍Git 使用介绍
Git 使用介绍medcl
 
Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹PingLun Liao
 
Git introduction
Git introductionGit introduction
Git introductionmythnc
 
Git原理与实战 201607
Git原理与实战 201607Git原理与实战 201607
Git原理与实战 201607Charles Tang
 
Git and git hub
Git and git hubGit and git hub
Git and git hub唯 李
 
Git+使用教程
Git+使用教程Git+使用教程
Git+使用教程gemron
 
Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 超簡單學習懶人包(軟體程式版本控管系統)Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 超簡單學習懶人包(軟體程式版本控管系統)flylon
 
Git分享 -分支管理
Git分享 -分支管理Git分享 -分支管理
Git分享 -分支管理yongfei Ma
 
Git flow 與團隊合作
Git flow 與團隊合作Git flow 與團隊合作
Git flow 與團隊合作Bo-Yi Wu
 
Git 入门实战
Git 入门实战Git 入门实战
Git 入门实战icy leaf
 
Github简介及实用入门
Github简介及实用入门Github简介及实用入门
Github简介及实用入门Rongxing Liu
 
Git in a nutshell
Git in a nutshellGit in a nutshell
Git in a nutshellNelson Tai
 
Git Flow 管理
Git Flow 管理Git Flow 管理
Git Flow 管理Pu Lee
 
Git & git flow
Git & git flowGit & git flow
Git & git flowAmo Wu
 
Git &amp; git hub v1.2
Git &amp; git hub v1.2Git &amp; git hub v1.2
Git &amp; git hub v1.2Chris Chen
 
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
 

Similar to git merge 與 rebase 的觀念與實務應用 (20)

Learn git
Learn gitLearn git
Learn 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 程式碼版本控制軟體介紹
 
Git introduction
Git introductionGit introduction
Git introduction
 
Git原理与实战 201607
Git原理与实战 201607Git原理与实战 201607
Git原理与实战 201607
 
Git and git hub
Git and git hubGit and git hub
Git and git hub
 
Git+使用教程
Git+使用教程Git+使用教程
Git+使用教程
 
Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 超簡單學習懶人包(軟體程式版本控管系統)Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 超簡單學習懶人包(軟體程式版本控管系統)
 
Git分享 -分支管理
Git分享 -分支管理Git分享 -分支管理
Git分享 -分支管理
 
Git flow 與團隊合作
Git flow 與團隊合作Git flow 與團隊合作
Git flow 與團隊合作
 
Git 入门实战
Git 入门实战Git 入门实战
Git 入门实战
 
Github简介及实用入门
Github简介及实用入门Github简介及实用入门
Github简介及实用入门
 
Git in a nutshell
Git in a nutshellGit in a nutshell
Git in a nutshell
 
Git Flow 管理
Git Flow 管理Git Flow 管理
Git Flow 管理
 
Git 教學
Git 教學Git 教學
Git 教學
 
Git & git flow
Git & git flowGit & git flow
Git & git flow
 
Git基础培训
Git基础培训Git基础培训
Git基础培训
 
Git &amp; git hub v1.2
Git &amp; git hub v1.2Git &amp; git hub v1.2
Git &amp; git hub v1.2
 
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
 

More from Will Huang

深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)Will Huang
 
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境Will Huang
 
ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索Will Huang
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!Will Huang
 
你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧Will Huang
 
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)Will Huang
 
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)Will Huang
 
Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Will Huang
 
從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點Will Huang
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門Will Huang
 
极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)Will Huang
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)Will Huang
 
Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Will Huang
 
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)Will Huang
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Will Huang
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Will Huang
 
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)Will Huang
 
AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)Will Huang
 
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)Will Huang
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)Will Huang
 

More from Will Huang (20)

深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
 
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
 
ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
 
你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧
 
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
 
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
 
Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)
 
從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門
 
极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
 
Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)
 
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)
 
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
 
AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)
 
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
 

git merge 與 rebase 的觀念與實務應用

  • 1. git merge 與 rebase 的 觀念與實務應用 多奇數位創意有限公司 技術總監 黃保翕 ( Will 保哥 ) 部落格:http://blog.miniasp.com/
  • 3. 常見的三種合併方法 (Merge) • 關於 Git 的快轉機制 (Fast-forward) – 所謂的「快轉機制」,就是 Git 得知這個合併的過程,其實會依序套用 new_feature 原本就有的變更,所以在合併的時候會直接修改 master 分 支的 HEAD 參照位址,直接移動到 new_feature的 HEAD 那個版本。 • 正常的合併 – git checkout master – git merge new_feature • 不快轉的合併 – git checkout master – git merge new_feature --no-ff • 壓縮的合併 – git checkout master – git merge new_feature --squash
  • 4. 不同的合併方式 • git merge <branch1> <branch2> <branch3> … • git merge --no-ff • git merge –squash • git rebase <commit_id> • git rebase <commit_id> -I • git cherry-pick <commit_id> • git revert <commit_id>
  • 5. 一般的合併方法 • 主要步驟 – 先切換至最終要合併完成的分支 • git checkout -b master – 將另一個分支的變更合併回來 • git merge <new_feature_branch> – 若合併成功 • 會自動引發一個新的 commit – 若合併失敗 (發生衝突) • 會停留在 Unmerged 狀態,需人工介入處理 • 處理完衝突,需繼續完成合併動作 (需做一次commit動作) – git add – git commit • 放棄衝突,可選擇復原至合併前的狀態 – git reset --hard
  • 6. 安全的合併方法 • 方法 1 – git checkout -b merge1 – git merge <new_feature_branch> – 若合併成功 • git checkout master • git merge merge1 (Fast-forward) • git branch –d merge1 – 若合併失敗 • git checkout master • git branch -D merge1
  • 7. 安全的合併方法 • 方法 2 – git tag -f try_merge_master – git merge <new_feature_branch> – 若合併成功 • git tag -d try_merge_master – 若合併失敗 • git reset try_merge_master --hard • git tag -d try_merge_master
  • 8. 重新指定基礎位置 (Rebase) • 將另一個分支當成目前分支的起點 – git checkout branch1 – git rebase master – git rebase --continue – git rebase --abort – git rebase --skip • 主要任務 – 先找出兩個分支之間的共同起點 – 將目前分支的所有變更套用在另一個分支 • 這就是所謂【重新指定基礎位置】的意思 • 是一個版本一個版本的套用變更 • 注意事項 – 不要在 git rebase 發生衝突的過程中執行 git commit 命令 – 如果真的做了,請改執行 git rebase --skip 跳過這一版套用
  • 9. MERGE 與 REBASE 的線圖差異 • MERGE – git checkout master – git merge branch1 • REBASE – git checkout branch1 – git rebase master – git checkout master – git merge branch1 • REBASE + MERGE --no-ff – git checkout branch1 – git rebase master – git checkout master – git merge branch1 --no-ff
  • 10. 了解 MERGE 與 REBASE 的差異 • 相同之處 – 都是合併的一種,但合併的方式與步驟不同 • MERGE 如何合併 – 假設你有一個 new_feature 分支想合併回 master – 你會這樣執行 • git checkout master • git merge new_feature (會建立起一條分支線圖) • REBASE 如何合併 – 假設你有一個 new_feature 分支想合併回 master ( 事實上是將自己的所有變更 commit 在 master 最新版後面) – 你會這樣執行 • git checkout new_feature • git rebase master (會讓 new_feature 併進 master 的線圖上) • git checkout master • git merge new_feature
  • 11. Rebase 能做的事 • 0. 重新 commit (Pick) • 1. 調換 commit 的順序 • 2. 修改 commit 的訊息 (Edit) • 3. 插入一個 commit • 4. 編輯一個 commit • 5. 拆解一個 commit • 6. 壓縮一個 commit,且合併訊息紀錄 (Squash) • 7. 壓縮一個 commit,但丟棄版本紀錄 • 8. 刪除一個 commit (Skip)
  • 13. 正常合併 • 預設會啟用 快轉 (fast-forward) 機制 • 給自己專用的小分支可以直接這樣用的 – 允許一條線畫到底的情境 • 本地 功能分支 (feature branch) 常用 13
  • 14. 不快轉合併 ( --no-ff ) • 用來確保主要分支的線圖保持乾淨 • 本地分支合併回主線時使用 – 主線可能是 master 或 develop • 遠端分支合併時預設採用這個模式 – GitHub 14
  • 15. 正常變基 ( Rebase ) • 用來確保整體分支線圖保持乾淨 • 通常是在 git pull 的時候發生使用 – 請記得:一個本地分支配一個遠端分支 • 還有在整理分支線圖時會用到 15
  • 16. 常用於有遠端儲存庫的情境 • git pull – git fetch – git merge • git pull --rebase – git fetch – git rebase – 此設定為 GitHub Desktop 的預設值 – 使用 git pull --rebase 可有效避免無謂的合併線圖 使用 git rebase 避免無謂的 merge | ihower { blogging } http://ihower.tw/blog/archives/3843 • 把 rebase 當做 git pull 的預設 值 [branch "master"] remote = origin merge = refs/heads/master rebase = true
  • 18. 關於 Git 合併與變基 • Git 打從骨子裡都是分支與合併 • Git 的分支與合併既便宜又安全! – git reflog • 釐清 merge 與 rebase 的用途與使用情境 18
  • 19. 聯絡資訊 • The Will Will Web 記載著 Will 在網路世界的學習心得與技術分享 – http://blog.miniasp.com/ • Will 保哥的技術交流中心 (臉書粉絲專頁) – http://www.facebook.com/will.fans • Will 保哥的噗浪 – http://www.plurk.com/willh/invite • Will 保哥的推特 – https://twitter.com/Will_Huang