Git rebase使用

git rebase能夠?qū)⒎植娴姆种е匦潞喜ⅲ皩?xiě)過(guò)一篇文章介紹它的原理,下面主要介紹它的兩個(gè)使用場(chǎng)景:

場(chǎng)景一:本地與遠(yuǎn)端同一分支提交歷史不一致

方式一

多個(gè)人在同一個(gè)分支上協(xié)作時(shí),出現(xiàn)沖突是很正常的,比如現(xiàn)在有一個(gè)項(xiàng)目由我和A一同開(kāi)發(fā)。

我在修復(fù)了一個(gè)bug以后準(zhǔn)備提交

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(-)

現(xiàn)在準(zhǔn)備推送到遠(yuǎn)端

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失敗了,說(shuō)明A在我之前已經(jīng)提交了,我本地master分支的提交歷史已經(jīng)落后遠(yuǎn)端了,需要先pull一下,與遠(yuǎn)端同步后才能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成功,現(xiàn)在使用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,優(yōu)化內(nèi)置通道配置
* | 8b76654 fix a bug
|/  
* a1bc60a 完善日?qǐng)?bào)接口
* 9f73b5e 增加內(nèi)置通道設(shè)置功能
* a0d464e ...

竟然分叉了!由于我本地master的提交歷史和遠(yuǎn)端的master分支的提交歷史不一致,所以git為我進(jìn)行了自動(dòng)合并,然后生成了一個(gè)新的提交歷史(f63ecbf Merge branch 'master' of

對(duì)于部分強(qiáng)迫癥來(lái)說(shuō)這個(gè)不能接受的,不想看到分叉。

這個(gè)時(shí)候用git rebase就可以解決

HowiedeiMac:ganlin howie$ git rebase
First, rewinding head to replay your work on top of it...
Applying: fix a bug

現(xiàn)在再查看一下提交歷史:

HowiedeiMac:ganlin howie$ git log --oneline --graph
* 2e2b995 (HEAD -> master) fix a bug
* b91f711 (origin/master, origin/HEAD) 修正bug,優(yōu)化內(nèi)置通道配置
* a1bc60a 完善日?qǐng)?bào)接口
* 9f73b5e 增加內(nèi)置通道設(shè)置功能
* a0d464e ...

完美解決,現(xiàn)在再push推送到遠(yuǎn)端:

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,優(yōu)化內(nèi)置通道配置
* a1bc60a 完善日?qǐng)?bào)接口
* 9f73b5e 增加內(nèi)置通道設(shè)置功能
* a0d464e ...

現(xiàn)在遠(yuǎn)端master,遠(yuǎn)端head,本地master全部統(tǒng)一,問(wèn)題解決。

方式二

直接執(zhí)行:
git pull --rebase
效果與上面是一致的,也是最近才發(fā)現(xiàn),推薦使用

場(chǎng)景二:不同分支之間的合并

由于老板突發(fā)奇想,要求開(kāi)發(fā)一個(gè)新的功能。

先創(chuàng)建一個(gè)分支用于開(kāi)發(fā)新功能:

git checkout -b feature

HowiedeiMac:hello howie$ git checkout -b feature
Switched to a new branch 'feature'
HowiedeiMac:hello howie$ git branch
* feature
  master

接下來(lái)修改newFunc.go,增加新的功能,并且保存提交

vim newFunc.go

git add newFunc.go

git commit -m 'add new func'

現(xiàn)在查看一下提交

HowiedeiMac:hello howie$ git log --oneline --graph
* 4f58ab8 (HEAD -> feature) add new func
* 94c134b (master) init base

HowiedeiMac:hello howie$ git branch
* feature
  master

現(xiàn)在新功能開(kāi)發(fā)完畢,需要將它合并的主分支中。

先嘗試通過(guò)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.

竟然失敗了,說(shuō)明我兩個(gè)分支之前的版本已經(jīng)不同步了,需要手動(dòng)合并沖突,再提交:

先查看沖突文件: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

打開(kāi)文件,進(jìn)行修改

原文件:

func NewFunc() {
<<<<<<< HEAD
=======
    fmt.Println("add new func")
>>>>>>> feature
}

修改后:

func NewFunc() {
    fmt.Println("add new func")
}

現(xiàn)在通過(guò)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

現(xiàn)在在查看一下分支提交歷史:

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已經(jīng)保存了合并歷史,出現(xiàn)開(kāi)叉了!對(duì)于強(qiáng)迫癥患者來(lái)說(shuō)肯定是不能接受的。

通過(guò)rebase合并分支:

現(xiàn)在將版本退回到合并前,也就是回退一個(gè)版本

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

退回去了,現(xiàn)在是位于master分支的init base提交這里。

先切換回feature分支:

HowiedeiMac:hello howie$ git checkout feature
Switched to branch 'feature'

在feature分支上執(zhí)行: git rebase master

這句命令的意識(shí)是:以master為基礎(chǔ),將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".


失敗了,原因很簡(jiǎn)單,兩個(gè)分支修改個(gè)同一個(gè)文件,產(chǎn)生了沖突。所以先需要解決沖突:

打開(kāi)沖突的文件,解決沖突

原文件:

func NewFunc() {
<<<<<<< HEAD
=======
    fmt.Println("add new func")
>>>>>>> add new func
}

修改后:

func NewFunc() {
    fmt.Println("add new func")
}

現(xiàn)在通過(guò)add添加

HowiedeiMac:hello howie$ git add newFunc.go

現(xiàn)在是重點(diǎn),之前的rebase其實(shí)只是完成了一半,由于出現(xiàn)沖突而終止,現(xiàn)在沖突解決,可以通過(guò)git rebase —continue繼續(xù)完成之前的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

提交記錄已經(jīng)是一條完美的直線?,F(xiàn)在切換到主分支master,將feather分支上的提交合并過(guò)來(lái)。

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

問(wèn)題解決,master上也是一條直線了。

最后收個(gè)尾,刪除掉feature分支:

HowiedeiMac:hello howie$ git branch -d feature
Deleted branch feature (was b2593e6).
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • git 使用筆記 git原理: 文件(blob)對(duì)象,樹(shù)(tree)對(duì)象,提交(commit)對(duì)象 tree對(duì)象 ...
    神刀閱讀 3,862評(píng)論 0 10
  • 多種多樣的工作流使得在項(xiàng)目中實(shí)施Git時(shí)變得難以選擇。這份教程提供了一個(gè)出發(fā)點(diǎn),調(diào)查企業(yè)團(tuán)隊(duì)最常見(jiàn)的Git工作流。...
    JSErik閱讀 4,614評(píng)論 2 8
  • Git 是目前最流行的分布式版本控制系統(tǒng)之一。 版本控制指的是,記錄每次版本變更的內(nèi)容和時(shí)間等細(xì)節(jié),保留各版本之間...
    神齊閱讀 1,520評(píng)論 0 7
  • 一、基本 git rebase用于把一個(gè)分支的修改合并到當(dāng)前分支。假設(shè)你現(xiàn)在基于遠(yuǎn)程分支"origin",創(chuàng)建一個(gè)...
    _Justin閱讀 28,992評(píng)論 9 33
  • 1. become/seem+extremely/highly/very/quite adept at doing...
    桂灰灰閱讀 396評(píng)論 0 0

友情鏈接更多精彩內(nèi)容