Git第十三阶段:Rebase -> 调整commit 顺序
一、缘起:
我有一些修改涉及到旧的commit,我想把这种同类的修改放在一起,这就需要我把原来的commit放在"TOP"的位置。
图示:
这是我原来的commit:
1 | C1-C2-C_TARGET-C3-C4 |
我想将它变成:
1 | C1-C2-C3-C4-C_TARGET |
二、进入我的测试git repo,我将生成三个测试commit,然后用git rebase来调整他们的顺序:
图示:
这是我生成:
1 | C1-C2-T1-T2-T3 |
我想将它变成:
1 | C1-C2-T3-T2-T1 |
三、开始操作,生成3个测试commit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | echo "1" > file_01.txt git add file_01.txt git commit -m "file_01 operation" echo "2" > file_02.txt git add file_02.txt git commit -m "file_02 operation" echo "3" > file_03.txt git add file_03.txt git commit -m "file_03 operation" tim@cn-rd-build04:~ /Test/gitTest/src $ git log commit 8edcd12c937451eadf6ca1ef87c9c456267162c3 (HEAD -> master) Author: songpeng <songpeng24@msn.com> Date: Wed Feb 21 00:52:36 2024 +0000 file_03 operation commit 6cf418aa10ce510a4e6019a295cebd720f9566ed Author: songpeng <songpeng24@msn.com> Date: Wed Feb 21 00:52:09 2024 +0000 file_02 operation commit f7741de60cf569ba0bf7bd880a5e6abdefa0f65a Author: songpeng <songpeng24@msn.com> Date: Wed Feb 21 00:50:07 2024 +0000 file_01 operation commit 8b9bd72cbadd8e506edac2914aa0138a1712486c Author: peng <song.peng@bizerba.com> Date: Mon Jan 8 02:34:56 2024 +0000 git reset --soft modify |
四、用git rebase给commit排序:
4-1、首先对三个commit之前的一个commit进行git rebase:
对应关系如下:
HEAD -> 8edcd12c937451eadf6ca1ef87c9c456267162c3 -> file_03 operation
HEAD~1 -> 6cf418aa10ce510a4e6019a295cebd720f9566ed -> file_02 operation
HEAD~2 -> f7741de60cf569ba0bf7bd880a5e6abdefa0f65a -> file_01 operation
HEAD~3 -> 8b9bd72cbadd8e506edac2914aa0138a1712486c -> git reset --soft modify
当下需要对HEAD~3这个commit 进行rebase,操作如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | git rebase -i 8b9bd72cbadd8e506edac2914aa0138a1712486c pick f7741de file_01 operation pick 6cf418a file_02 operation pick 8edcd12 file_03 operation # Rebase 8b9bd72..8edcd12 onto 8b9bd72 (3 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup [-C | -c] <commit> = like "squash" but keep only the previous # commit's log message, unless -C is used, in which case # keep only this commit's message; -c is same as -C but # opens the editor # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified); use -c <commit> to reword the commit message # # 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. # |
4-2、调整三个pick的顺序为,使其变成反序:
1 2 3 | pick 8edcd12 file_03 operation pick 6cf418a file_02 operation pick f7741de file_01 operation |
这样,相当于git cherry-pick的时候,先pick了"file_03 operation",再pick"file_02 operation",最后pick "file_01 operation"。
4-3、最后Ctrl+O保存,Ctrl+x退出,得到的结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | peng@peng:~ /Test/gitTest/src $ git log commit f1f242fbcd1fa18f37f8b244c0f0613b134b194f (HEAD -> master) Author: songpeng <songpeng24@msn.com> Date: Wed Feb 21 00:50:07 2024 +0000 file_01 operation commit 69969581ae17bbecb0e92cc6172b53307485c44f Author: songpeng <songpeng24@msn.com> Date: Wed Feb 21 00:52:09 2024 +0000 file_02 operation commit 841d6b0239f1f7342aec9229703c791eb6dabe3e Author: songpeng <songpeng24@msn.com> Date: Wed Feb 21 00:52:36 2024 +0000 file_03 operation commit 8b9bd72cbadd8e506edac2914aa0138a1712486c Author: peng <song.peng@bizerba.com> Date: Mon Jan 8 02:34:56 2024 +0000 git reset --soft modify |
分类:
git
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架