場景:多人共用一個分支開發(fā),提交 merge 請求時,需要合并多個提交為一個提交
假設(shè):主分支為:master,對應(yīng)的遠(yuǎn)程主分支為:origin/master;你與隊(duì)友當(dāng)前開發(fā)的分支為:dev,對應(yīng)的遠(yuǎn)程分支為:origin/dev。
此時,你與隊(duì)友所在的 dev 分支上有多次提交(commit)記錄,并且已經(jīng)推送(push)到遠(yuǎn)程分支(origin/dev);現(xiàn)在,你們的的工作已經(jīng)完成,需要向遠(yuǎn)程主分支(origin/master)申請 merge request,并且需要將你們所有的工作提交記錄合并為一個提交記錄。
做法:
(1) git checkout -b merge_branch
在當(dāng)前分支 (dev) 新建一個用于提交 merge_request 的分支。
(2) git rebase -i b7b5b77
在本地將 merge_branch 分支變基到 b7b5b77,b7b5b77是遠(yuǎn)程主分支(origin/master)最后一次提交的 id。
1.顯示多次提交日志,從上到下為按時間正序,編輯每行開頭的pick:
# p, pick = use commit ,默認(rèn),使用提交和提交信息
# r, reword = use commit, but edit the commit message 使用該提交,重新編輯提交信息
# e, edit = use commit, but stop for amending 使用并編輯, 一般不使用
# s, squash = use commit, but meld into previous commit 使用并合并到之前提交,保留編輯信息.不要在第一行使用 **
# f, fixup = like "squash", but discard this commit's log message 使用并合并到之前提交,不保留編輯信息.不要在第一行使用,后續(xù)無意義提交合并使用這個選項(xiàng) **
# x, exec = run command (the rest of the line) using shell 執(zhí)行其他shell命令
# d, drop = remove commit 刪除提交,慎重
2.保存并退出編輯
3.沖突解決(提交有沖突)
(3) git push --set-upstream origin merge_branch 首次將本地新建的 merge_branch 分支推送到遠(yuǎn)程倉庫。
(4) 到遠(yuǎn)程倉庫中向 origin/master 分支提交 merge 請求,選擇 origin/merge_branch 分支。
(5) 等待遠(yuǎn)程主分支同意合并
(6) 遠(yuǎn)程主分支同意合并之后,刪除本地 merge_branch 分支:git branch -d merge_branch。
(7) 對遠(yuǎn)程分支執(zhí)行剪枝操作:git remote prune origin