git rebase 的几种用法
演示過程中,為便于區分修改結果,未使用commit 版本號,建議各位使用盡量以版本號為準
1.修改歷史提交信息
例如:將提交歷史 修改提交1改為 develop 修改提交1
修改步驟:
查看 git的提交歷史,找到要修改的commit message之前的提交版本號
λ git log --pretty=oneline 37a8fb4fac5e42c59bebb91ebcb7f6a6f857d2a1 :pencil: 修改提交3 b0ac728c9d9a668f4d645d3555a9618f0cdc9699 :pencil: 修改提交2 fb3f99ab476f22fe070a84ffc2399186a1e1a5de :pencil: 修改提交1 # 要修改第一條的提交記錄 bb989fb4c2b0fd88e09fb0510bed09d6c70bf350 :wrench: 添加ignore文件 4de7b78dc7f3c1d46fe902f6201f2c6d21c67df0 Initial commit根據版本號,進行rebase
λ git rebase -i bb989fb # 這里應該使用需要修改的提交記錄前的那個版本號 # pick fb3f99a :pencil: 修改提交1 # pick b0ac728 :pencil: 修改提交2 # pick 37a8fb4 :pencil: 修改提交3 # Rebase bb989fb..37a8fb4 onto bb989fb (3 command(s)) # # Commands: # p, pick = use commit # 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, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out根據需求編輯提交記錄,修改操作和vim操作類似,每個提交 commit 記錄前默認都是 pick 操作,可參照如下指令進行修改,每次進入rebase模式,會有提示,無需刻意去記
# p, pick = use commit # 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, exec = run command (the rest of the line) using shell # d, drop = remove commit這里我需要修改commit message,所以將pick改為reword,并修改message。修改后的rebase信息為
# r fb3f99a :pencil: 修改提交1 # r b0ac728 :pencil: 修改提交2 # r 37a8fb4 :pencil: 修改提交3# Rebase bb989fb..37a8fb4 onto bb989fb (3 command(s)) # # Commands: # p, pick = use commit # 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, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out修改完成后,esc輸入x保存退出。依次修改需要修改的message信息,例如第一個commit修改后為:
:pencil: develop 修改提交1# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Wed Aug 18 16:13:24 2021 +0800 # # interactive rebase in progress; onto bb989fb # Last command done (1 command done): # r fb3f99a :pencil: develop 修改提交1 # Next commands to do (2 remaining commands): # r b0ac728 :pencil: develop 修改提交2 # r 37a8fb4 :pencil: develop 修改提交3 # You are currently editing a commit while rebasing branch 'develop' on 'bb989fb'. # # Changes to be committed: # modified: README.md修改完成后的log信息:
λ git log --pretty=oneline # 0814ee596f2ca0e0c8a2d497d6759300346a52c4 :pencil: develop 修改提交3 # edb46dabe5db8df49a9aa9d40e502badc11daf4c :pencil: develop 修改提交2 # 15da532e22b39b6ab4d0a7318f3158ddbf033c12 :pencil: develop 修改提交1 # bb989fb4c2b0fd88e09fb0510bed09d6c70bf350 :wrench: 添加ignore文件 # 4de7b78dc7f3c1d46fe902f6201f2c6d21c67df0 Initial commitλ git reflog --pretty=oneline # 0814ee5 HEAD@{10}: rebase -i (finish): returning to refs/heads/develop # 0814ee5 HEAD@{11}: rebase -i (reword): :pencil: develop 修改提交3 # 38a0d3c HEAD@{12}: rebase -i (reword): :pencil: 修改提交3 # edb46da HEAD@{13}: rebase -i (reword): :pencil: develop 修改提交2 # a98942d HEAD@{14}: rebase -i (reword): :pencil: 修改提交2 # 15da532 HEAD@{15}: rebase -i (reword): :pencil: develop 修改提交1 # fb3f99a HEAD@{16}: cherry-pick: fast-forward # bb989fb HEAD@{17}: rebase -i (start): checkout bb989fb需要注意 修改后有可能需要使用 git push -f 強制推送至遠程倉庫
2. 將提交進行合并
git log查看當前歷史記錄
λ git log --pretty=oneline # c129c21c80ab66b60b90932648c1c798b795ad08 :pencil: master 修改提交3 # 0014c47d51879a88651fbaa0753247a1688fef1e :pencil: master 修改提交2 # f44fa1957f8c240c187948687eabeec8cff3e83f :pencil: develop 修改提交4 # 3d11352e8f69e3483957882f9b2c3ec3dd6b930d :pencil: master 修改提交1 # 0814ee596f2ca0e0c8a2d497d6759300346a52c4 :pencil: develop 修改提交3 # edb46dabe5db8df49a9aa9d40e502badc11daf4c :pencil: develop 修改提交2 # 15da532e22b39b6ab4d0a7318f3158ddbf033c12 :pencil: develop 修改提交1 # bb989fb4c2b0fd88e09fb0510bed09d6c70bf350 :wrench: 添加ignore文件 # 4de7b78dc7f3c1d46fe902f6201f2c6d21c67df0 Initial commit將 :pencil: master 修改提交3 和 :pencil: master 修改提交2 進行合并,進入rebase:
λ git rebase -i f44fa1957f # pick 0014c47 :pencil: master 修改提交2 # pick c129c21 :pencil: master 修改提交3 # Rebase f44fa19..c129c21 onto f44fa19 (2 command(s)) # # Commands: # p, pick = use commit # 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, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out修改完成后的log信息:
# pick 0014c47 :pencil: master 修改提交2 # s c129c21 :pencil: master 修改提交3 # 將 `pick` 改為 `squash(或簡寫s)`# Rebase f44fa19..c129c21 onto f44fa19 (2 command(s)) # # Commands: # p, pick = use commit # 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, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out修改完成后,esc輸入x保存退出。修改合并后的提交信息
# This is a combination of 2 commits. # The first commit's message is::pencil: master 修改提交2 和 :pencil: master 修改提交3 合并后的提交信息# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Sun Aug 22 00:10:50 2021 +0800 # # interactive rebase in progress; onto f44fa19 # Last commands done (2 commands done): # pick 0014c47 :pencil: master 修改提交2 # s c129c21 :pencil: master 修改提交3 # No commands remaining. # You are currently editing a commit while rebasing branch 'develop' on 'f44fa19'. # # Changes to be committed: # modified: README.md輸入x保存退出。查看新的log日志:
λ git log --pretty=oneline # 412612b4e3c96839d8349c268c64b3faf69c84e8 :pencil: master 修改提交2 和 :pencil: master 修改提交3 合并后的提交信 # f44fa1957f8c240c187948687eabeec8cff3e83f :pencil: develop 修改提交4 # 3d11352e8f69e3483957882f9b2c3ec3dd6b930d :pencil: master 修改提交1 # 0814ee596f2ca0e0c8a2d497d6759300346a52c4 :pencil: develop 修改提交3 # edb46dabe5db8df49a9aa9d40e502badc11daf4c :pencil: develop 修改提交2 # 15da532e22b39b6ab4d0a7318f3158ddbf033c12 :pencil: develop 修改提交1 # bb989fb4c2b0fd88e09fb0510bed09d6c70bf350 :wrench: 添加ignore文件 # 4de7b78dc7f3c1d46fe902f6201f2c6d21c67df0 Initial commit可以看到 master 修改提交2 和 master 修改提交3兩個提交已經不存在了,合并產生了一個新提交:pencil: master 修改提交2 和 :pencil: master 修改提交3 合并后的提交信。
3. 相關說明
從rebase的操作列表中可以看出,可以進行編輯、刪除、合并等多個操作。
# p, pick = use commit # 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, exec = run command (the rest of the line) using shell # d, drop = remove commit目前我主要使用reabse進行如下相關操作:
修改或刪除有異議或表述不準確的提交記錄(已演示)
合并多余的提交,避免提交過多導致重要提交信息被淹沒(已演示)
需要注意的幾個地方:
** 表述有誤的地方,懇請批評指正,先謝為敬!**
總結
以上是生活随笔為你收集整理的git rebase 的几种用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蚂蚁开放平台开发第三方授权登陆(三):A
- 下一篇: Zuul微服务网关、容错与监控、Zuul