git rebase能夠將分叉的分支重新合並,之前寫過一篇文章介紹它的原理,下面主要介紹它的兩個使用場景:
場景一:本地與遠端同一分支提交歷史不一致
方式一
多個人在同一個分支上協作時,出現沖突是很正常的,比如現在有一個項目由我和A一同開發。
我在修復了一個bug以后准備提交
HowiedeiMac:ganlin howie$ git add models/paper.go
HowiedeiMac:ganlin howie$ git commit -m 'fix a bug'
[master 8b76654] fix a bug
1 file changed, 3 insertions(+), 3 deletions(-)
現在准備推送到遠端
HowiedeiMac:ganlin howie$ git push origin master
To https://gitee.com/greenhn/ganlin.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/greenhn/ganlin.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
push失敗了,說明A在我之前已經提交了,我本地master分支的提交歷史已經落后遠端了,需要先pull一下,與遠端同步后才能push
HowiedeiMac:ganlin howie$ git pull
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 6), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
From https://gitee.com/greenhn/ganlin
a1bc60a..b91f711 master -> origin/master
Merge made by the 'recursive' strategy.
controllers/deal_local_data.go | 14 +++++++++++---
controllers/rtu_interface.go | 8 ++++----
models/instrument_type.go | 3 +++
models/rtu_interface.go | 3 +++
4 files changed, 21 insertions(+), 7 deletions(-)
pull成功,現在使用git log看下一提交歷史:
HowiedeiMac:ganlin howie$ git log --oneline --graph
* f63ecbf (HEAD -> master) Merge branch 'master' of https://gitee.com/greenhn/ganlin
|\
| * b91f711 (origin/master, origin/HEAD) 修正bug,優化內置通道配置
* | 8b76654 fix a bug
|/
* a1bc60a 完善日報接口
* 9f73b5e 增加內置通道設置功能
* a0d464e ...
竟然分叉了!由於我本地master的提交歷史和遠端的master分支的提交歷史不一致,所以git為我進行了自動合並,然后生成了一個新的提交歷史(f63ecbf Merge branch 'master' of
)
對於部分強迫症來說這個不能接受的,不想看到分叉。
這個時候用git rebase
就可以解決
HowiedeiMac:ganlin howie$ git rebase
First, rewinding head to replay your work on top of it...
Applying: fix a bug
現在再查看一下提交歷史:
HowiedeiMac:ganlin howie$ git log --oneline --graph
* 2e2b995 (HEAD -> master) fix a bug
* b91f711 (origin/master, origin/HEAD) 修正bug,優化內置通道配置
* a1bc60a 完善日報接口
* 9f73b5e 增加內置通道設置功能
* a0d464e ...
完美解決,現在再push推送到遠端:
HowiedeiMac:ganlin howie$ git push origin master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 394 bytes | 394.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Powered By Gitee.com
To https://gitee.com/greenhn/ganlin.git
b91f711..2e2b995 master -> master
再次查看提交歷史
HowiedeiMac:ganlin howie$ git lg --oneline --graph
* 2e2b995 (HEAD -> master, origin/master, origin/HEAD) fix a bug
* b91f711 修正bug,優化內置通道配置
* a1bc60a 完善日報接口
* 9f73b5e 增加內置通道設置功能
* a0d464e ...
現在遠端master,遠端head,本地master全部統一,問題解決。
方式二
直接執行:git pull --rebase
效果與上面是一致的,也是最近才發現,推薦使用
場景二:不同分支之間的合並
由於老板突發奇想,要求開發一個新的功能。
先創建一個分支用於開發新功能:
git checkout -b feature
HowiedeiMac:hello howie$ git checkout -b feature
Switched to a new branch 'feature'
HowiedeiMac:hello howie$ git branch
* feature
master
接下來修改newFunc.go,增加新的功能,並且保存提交
vim newFunc.go
git add newFunc.go
git commit -m 'add new func'
現在查看一下提交
HowiedeiMac:hello howie$ git log --oneline --graph
* 4f58ab8 (HEAD -> feature) add new func
* 94c134b (master) init base
HowiedeiMac:hello howie$ git branch
* feature
master
現在新功能開發完畢,需要將它合並的主分支中。
先嘗試通過merge合並:
首先切換到master分支
git checkout master
HowiedeiMac:hello howie$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
直接合並feature分支
git merge feature
HowiedeiMac:hello howie$ git merge feature
Auto-merging newFunc.go
CONFLICT (content): Merge conflict in newFunc.go
Automatic merge failed; fix conflicts and then commit the result.
竟然失敗了,說明我兩個分支之前的版本已經不同步了,需要手動合並沖突,再提交:
先查看沖突文件:git status
HowiedeiMac:hello howie$ git status
On branch master
Your branch is ahead of 'origin/master' by 7 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: newFunc.go
打開文件,進行修改
原文件:
func NewFunc() { <<<<<<< HEAD ======= fmt.Println("add new func") >>>>>>> feature }
修改后:
func NewFunc() { fmt.Println("add new func") }
現在通過add添加,然后commit提交
HowiedeiMac:hello howie$ git add newFunc.go
HowiedeiMac:hello howie$ git commit -m 'merge master and feature'
[master 562ec58] merge master and feature
現在在查看一下分支提交歷史:
HowiedeiMac:hello howie$ git log --oneline --graph
* 562ec58 (HEAD -> master) merge master and feature
|\
| * 4f58ab8 (feature) add new func
* | 0e80f97 do something
|/
* 94c134b init base
雖然合並成功,但是Master已經保存了合並歷史,出現開叉了!對於強迫症患者來說肯定是不能接受的。
通過rebase合並分支:
現在將版本退回到合並前,也就是回退一個版本
git reset --hard head^
HowiedeiMac:hello howie$ git reset --hard head^
HEAD is now at 0e80f97 do something
HowiedeiMac:hello howie$ git log --oneline --graph
* 0e80f97 (HEAD -> master) do something
* 94c134b init base
退回去了,現在是位於master分支的init base
提交這里。
先切換回feature分支:
HowiedeiMac:hello howie$ git checkout feature
Switched to branch 'feature'
在feature分支上執行: git rebase master
這句命令的意識是:以master為基礎,將feature分支上的修改增加到master分支上,並生成新的版本。
HowiedeiMac:hello howie$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: add new func
Using index info to reconstruct a base tree...
M newFunc.go
Falling back to patching base and 3-way merge...
Auto-merging newFunc.go
CONFLICT (content): Merge conflict in newFunc.go
error: Failed to merge in the changes.
Patch failed at 0001 add new func
hint: Use 'git am --show-current-patch' to see the failed patch
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
失敗了,原因很簡單,兩個分支修改個同一個文件,產生了沖突。所以先需要解決沖突:
打開沖突的文件,解決沖突
原文件:
func NewFunc() { <<<<<<< HEAD ======= fmt.Println("add new func") >>>>>>> add new func }
修改后:
func NewFunc() { fmt.Println("add new func") }
現在通過add添加
HowiedeiMac:hello howie$ git add newFunc.go
現在是重點,之前的rebase其實只是完成了一半,由於出現沖突而終止,現在沖突解決,可以通過git rebase —continue
繼續完成之前的rebase操作。
HowiedeiMac:hello howie$ git rebase --continue
Applying: add new func
rebase完成,再查看一下提交歷史:
HowiedeiMac:hello howie$ git log --oneline --graph
* b2593e6 (HEAD -> feature) add new func
* 0e80f97 (master) do something
* 94c134b init base
提交記錄已經是一條完美的直線。現在切換到主分支master,將feather分支上的提交合並過來。
git checkout master
git merge feature
HowiedeiMac:hello howie$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 7 commits.
(use "git push" to publish your local commits)
HowiedeiMac:hello howie$ git merge feature
Updating 0e80f97..b2593e6
Fast-forward
newFunc.go | 1 +
1 file changed, 1 insertion(+)
再次查看一下提交歷史:
HowiedeiMac:hello howie$ git log --oneline --graph
* b2593e6 (HEAD -> master, feature) add new func
* 0e80f97 do something
* 94c134b init base
問題解決,master上也是一條直線了。
其實本質上, 等效於:
git pull --rebase --autostash origin master
最后收個尾,刪除掉feature分支:
HowiedeiMac:hello howie$ git branch -d feature
Deleted branch feature (was b2593e6).