为了账号安全,请及时绑定邮箱和手机立即绑定

压缩提交记录

标签:
Git

运用场景

在没有推送本地修改到共用分支之前,可以通过变基操作整理提交历史。

执行步骤

执行 git log,列出所有的提交:

$ git checkout main
$ git log
commit ff3490694a2168a94abc0188fe35cdd102d3c7fc (HEAD -> main, origin/main, origin/iss53, iss53)
Author: zhanglianwei <zhanglianwei@meituan.com>
Date:   Wed Feb 21 16:09:29 2024 +0800

    第二次修改iss53

commit b0a67cb1bbd16e8a812516dad50b129961f483f7 (iss52)
Author: zhanglianwei <zhanglianwei@meituan.com>
Date:   Wed Feb 21 16:02:42 2024 +0800

    修改iss52

commit 9f5bfba20b0448961111fe1cc621187b461284c4
Author: zhanglianwei <zhanglianwei@meituan.com>
Date:   Wed Feb 21 16:00:55 2024 +0800

    修改hotfix

commit ca5ebf0f7e0438c514b9039594e86b6bc1ef64e2
Author: zhanglianwei <zhanglianwei@meituan.com>
Date:   Tue Jan 30 16:24:42 2024 +0800
:

通过给 git rebase 增加 --interactive ( 简写为 -i )选项来交互式地运行变基,可以将一连串提交压缩成一个单独的提交。

$ git rebase -i HEAD~3 # 也可以使用哈希值 git rebase -i ca5ebf0
pick 9f5bfba 修改hotfix
pick b0a67cb 修改iss52
pick ff34906 第二次修改iss53

# Rebase ca5ebf0..ff34906 onto ca5ebf0 (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>]
"~/git-practise/.git/rebase-merge/git-rebase-todo" 34L, 1566B

如果想要这三次提交变为一个提交,可以这样修改上面的内容:

pick 9f5bfba 修改hotfix
squash b0a67cb 修改iss52 # 简写 s 也可以
squash ff34906 第二次修改iss53 # 简写 s 也可以

当保存并退出编辑器时,会展示出三次提交信息:

# This is a combination of 3 commits.
# This is the 1st commit message:

修改iss52

# This is the commit message #2:

修改iss53

# This is the commit message #3:

第二次修改iss53

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Feb 21 16:02:42 2024 +0800
#
# interactive rebase in progress; onto 9f5bfba
# Last commands done (3 commands done):
#    squash 3f9efe3 修改iss53
"~/git-practise/.git/COMMIT_EDITMSG" 28L, 669B

合并三次提交信息:

# This is a combination of 3 commits.
# This is the 1st commit message:

修改iss52、iss53

# This is the commit message #2:



# This is the commit message #3:

保存之后,再次执行 git log,三次提交变为一个提交。

$ git log
commit 877ae9ea21b69947dec3c2fce588c1da7e955ee6 (HEAD -> main)
Author: zhanglianwei <zhanglianwei@meituan.com>
Date:   Wed Feb 21 16:00:55 2024 +0800

    修改iss52、iss53

commit ca5ebf0f7e0438c514b9039594e86b6bc1ef64e2
Author: zhanglianwei <zhanglianwei@meituan.com>
Date:   Tue Jan 30 16:24:42 2024 +0800

    first commit

最后,通过 git push -f 强制推送至远程仓库即可。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
1.4万
获赞与收藏
860

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消