git rebase -- 能夠將分叉的分支重新合並.


git rebase

🌰場景一:本地與遠端同一分支提交歷史不一致

 1 # 修復了一個bug以后准備提交
 2 git add models/paper.go
 3 git commit -m 'fix a bug'
 4 # 現在准備推送到遠端
 5 git push origin master
 6 # push失敗了,說明A在我之前已經提交了,我本地master分支的提交歷史已經落后遠端了,需要先pull一下,與遠端同步后才能push
 7 git pull
 8 # 現在使用git log看下一提交歷史:
 9 git log --oneline --graph
10 *   f63ecbf (HEAD -> master) Merge branch 'master' of https://gitee.com/greenhn/ganlin
11 |\  
12 | * b91f711 (origin/master, origin/HEAD) 修正bug,優化內置通道配置
13 * | 8b76654 fix a bug
14 |/  
15 * a1bc60a 完善日報接口
16 * 9f73b5e 增加內置通道設置功能
17 * a0d464e ...
18 # 竟然分叉了!由於我本地master的提交歷史和遠端的master分支的提交歷史不一致,所以git為我進行了自動合並,然后生成了一個新的提交歷史(f63ecbf Merge branch 'master' of)
19 #這個時候用git rebase就可以解決
20 git rebase
21 git log --oneline --graph
22 * 2e2b995 (HEAD -> master) fix a bug
23 * b91f711 (origin/master, origin/HEAD) 修正bug,優化內置通道配置
24 * a1bc60a 完善日報接口
25 * 9f73b5e 增加內置通道設置功能
26 * a0d464e ...
27 git push

簡單操作: git pull --rebase 效果與上面是一致

🌰場景二:不同分支之間的合並

 1 git checkout -b feature
 2 vim newFunc.go
 3 git add newFunc.go
 4 git commit -m 'add new func'
 5 git log --oneline --graph
 6 * 4f58ab8 (HEAD -> feature) add new func
 7 * 94c134b (master) init base
 8 # 先嘗試通過merge合並:
 9 git checkout master
10 git merge feature
11 CONFLICT (content): Merge conflict in newFunc.go
12 Automatic merge failed; fix conflicts and then commit the result.
13 說明我兩個分支之前的版本已經不同步了,需要手動合並沖突,再提交
14 git add newFunc.go
15 git commit -m 'merge master and feature'
16 git log --oneline --graph
17 *   562ec58 (HEAD -> master) merge master and feature
18 |\  
19 | * 4f58ab8 (feature) add new func
20 * | 0e80f97 do something
21 |/  
22 * 94c134b init base
23 
24 解決: 回到合並前,再回到feature分支
25 在feature分支上執行: git rebase master
26 以master為基礎,將feature分支上的修改增加到master分支上,並生成新的版本。
27 # 然后解決沖突
28 git add newFunc.go
29 # 現在是重點,之前的rebase其實只是完成了一半,由於出現沖突而終止,現在沖突解決,可以通過git rebase —continue繼續完成之前的rebase操作。
30 git rebase --continue
31 git log --oneline --graph
32 * b2593e6 (HEAD -> feature) add new func
33 * 0e80f97 (master) do something
34 * 94c134b init base
35 # 提交記錄已經是一條完美的直線。現在切換到主分支master,將feather分支上的提交合並過來。
36 git checkout master
37 git merge feature
38 # 再次查看一下提交歷史:
39 git log --oneline --graph
40 * b2593e6 (HEAD -> master, feature) add new func
41 * 0e80f97 do something
42 * 94c134b init base

  git rebase -i

🌰

# 開始新的功能分支
git checkout -b new-feature master
# 編輯文件
git commit -a -m "start developing a feature"
# 編輯更多文件
git commit -a -m "fix someting"

# 直接在master 上添加文件
git checkout master
# 編輯文件
git commit -a -m "fix security hole"

# 開始交互式 rebase
git checkout new-feature
git rebase -i master

  最后的那個命令會打開一個編輯器,包含new-feature的兩個,和一些指示:

pick 32618c4 Start developing a feature
pick 62eed47 Fix something from the previous commit

  里面的提示有:

    pick:保留該commit(縮寫:p)
    reword:保留該commit,但我需要修改該commit的注釋(縮寫:r)
    edit:保留該commit, 但我要停下來修改該提交(不僅僅修改注釋)(縮寫:e)
    squash:將該commit和前一個commit合並(縮寫:s)
    fixup:將該commit和前一個commit合並,但我不要保留該提交的注釋信息(縮寫:f)
    exec:執行shell命令(縮寫:x)
    drop:我要丟棄該commit(縮寫:d)

  你可以更改每個提交前的pick命令來決定在rebase時提交移動的方式,在我們的例子中,我們只需要用squash命令把兩個提交並在一起就可以了:

pick 32618c4 Start developing a feature
squash 62eed47 Fix something from the previous commit

  保存並關閉編輯器以開始 rebase。另一個編輯器會打開,詢問你合並后的快照的提交信息。在定義了提交信息之后,rebase 就完成了,你可以在 git log 輸出中看到那個提交。

git checkout master
git merge new-feature

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM