Git第十三阶段:Rebase -> 调整commit 顺序

 

一、缘起

我有一些修改涉及到旧的commit,我想把这种同类的修改放在一起,这就需要我把原来的commit放在"TOP"的位置。
图示:
这是我原来的commit:

C1-C2-C_TARGET-C3-C4

我想将它变成:

C1-C2-C3-C4-C_TARGET


二、进入我的测试git repo,我将生成三个测试commit,然后用git rebase来调整他们的顺序

图示:
这是我生成:

C1-C2-T1-T2-T3

我想将它变成:

C1-C2-T3-T2-T1


三、开始操作,生成3个测试commit:

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,操作如下:

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的顺序为,使其变成反序:

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退出,得到的结果如下:

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

  



 

posted @ 2024-02-21 09:34  hkingsp  阅读(226)  评论(0编辑  收藏  举报