首页 > 其他分享 >Git第十三阶段:Rebase -> 调整commit 顺序

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

时间:2024-02-21 09:47:06浏览次数:30  
标签:02 Git Rebase git file pick commit operation

 

一、缘起

我有一些修改涉及到旧的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 <[email protected]>
Date:   Wed Feb 21 00:52:36 2024 +0000

    file_03 operation

commit 6cf418aa10ce510a4e6019a295cebd720f9566ed
Author: songpeng <[email protected]>
Date:   Wed Feb 21 00:52:09 2024 +0000

    file_02 operation

commit f7741de60cf569ba0bf7bd880a5e6abdefa0f65a
Author: songpeng <[email protected]>
Date:   Wed Feb 21 00:50:07 2024 +0000

    file_01 operation

commit 8b9bd72cbadd8e506edac2914aa0138a1712486c
Author: peng <[email protected]>
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 <[email protected]>
Date:   Wed Feb 21 00:50:07 2024 +0000

    file_01 operation

commit 69969581ae17bbecb0e92cc6172b53307485c44f
Author: songpeng <[email protected]>
Date:   Wed Feb 21 00:52:09 2024 +0000

    file_02 operation

commit 841d6b0239f1f7342aec9229703c791eb6dabe3e
Author: songpeng <[email protected]>
Date:   Wed Feb 21 00:52:36 2024 +0000

    file_03 operation

commit 8b9bd72cbadd8e506edac2914aa0138a1712486c
Author: peng <[email protected]>
Date:   Mon Jan 8 02:34:56 2024 +0000

    git reset --soft modify

  



 

标签:02,Git,Rebase,git,file,pick,commit,operation
From: https://www.cnblogs.com/hkingsp/p/18024489

相关文章

  • 细粒度的代码权限怎么做?极狐GitLab 代码所有者来帮忙
    本文作者:极狐GitLab资深解决方案架构师尹学峰如果基于固定的评审规则每次都是那几个人,当仓库很大的时候,各个模块(文件夹)责任人不同,其他人并不太懂。所以当修改不同的模块时候,基于固定规则就太死板了。而且容易造成「评审行为」的流于形式,因为固定的人可能根本看不懂实际MR变......
  • 【转】在 github 添加一个 SSH key
    参考:https://zhuanlan.zhihu.com/p/345846941、登录到 https://github.com,在设置页面的左侧菜单里找到SSHandGPGkeys2、打开GitBash3、运行 ssh-keygen-trsa-b4096-C"你的邮箱",注意要填写与github.com里相同的邮箱,不需要接收验证码啥的4、按回车三次,就成......
  • git
    丢弃webroot文件目录下的所有变更:gitcheckoutwebrootgit会将冲突的代码用<<<<<<<=======>>>>>>>标识出来,方便我们手动解决。在冲突标记中,=======之前表示的是ours分支,之后表示theirs分支。在使用merge时,指的是把指定分支合入当前分支,ours指的是当前分支,theirs指的是要被......
  • 酒香还怕巷子深?如何打造一个优秀的GitHub开源项目
    GitHub现在已经成了日常开发中必不可少的网站,日常工作和学习中要用到好多上面的开源项目,评价项目质量好坏的一个重要标准就是看Star和Fork的数量,如果看到个Star超过100以上的,基本上这个项目是靠谱的,如果超过1000过,那已经算是很流行了,至于一万以上的,基本上都是如雷贯耳的存在了。......
  • [GIT] GIT Commit Message 规范
    1概述本文主要用于总结自己的一套GitCommitMessage。1.1GitCommitMessage是什么?每次基于Git提交代码,都要写Commitmessage(提交说明),否则就不允许提交。$gitcommit-m"helloworld"-m参数:指定commitmesage1.2规范的提出背景软件工程团队多人协作时,c......
  • github raw文件cdn加速
    ref:https://blog.csdn.net/neve_give_up_dan/article/details/104817638可以使用该CDN网站进行加速:https://www.jsdelivr.com/官方示例:LoadanyGitHubrelease,commit,orbranch:/gh/user/repo@version/fileLoadexactversion:/gh/jquery/[email protected]/dist/jquery.m......
  • GitHub 热搜项目--电视直播软件:my-tv
    1.GitHub热搜项目1.1开箱即用的电视直播软件:my-tv主语言:C,Star:10k,周增长:6.9k这是一款开源、免费、无广告、不用注册的电视直播软件,适用于Android5及以上的手机和电视盒子。它安装即用、启动快,没有花里胡哨的UI和弹框,内置中央台、地方台等优质直播源,画质高清、播放流畅,......
  • 初始化本地已有项目到github
    1、在github新建一个仓库;2、在本地项目根目录执行gitinit命令;gitinit3、执行gitremoteaddorigin关联github仓库;gitremoteaddorigin NieQimingG/detectory:mywaytogo(github.com)4、从远程分支拉去main分支并于本地main分支合并;gitpulloriginmai......
  • 开年!5 款令人惊艳的开源项目「GitHub 热点速览」
    朋友们开工大吉啊!我刚从假期模式切换回来,完全无心工作有些不在状态,比如开机密码错了好几次......
  • 云原生明星创业公司 Weaveworks 倒闭了,GitOps 该何去何从?
    自从2009年DevOps面世以来,xOps的潘多拉魔盒就被打开了,AIOps、DataOps、DevSecOps、BizDevOps,当然还有最近几年比较火热的GitOps。但是很不幸的是,就在龙年新春前夕,GitOps理论提出者——Weaveworks倒下了。时间回到2月5日,就在大家期待春节长假的时候,一则Weaveworks......