V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
donghui
V2EX  ›  问与答

git 压缩多个 commit 为一个,是否有非交互式的?

  •  
  •   donghui · 2017-03-02 10:35:40 +08:00 · 3717 次点击
    这是一个创建于 2625 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想压缩 git 的多个 commit 为一个,了解到 git rebase -i 可以,但它是交互式的压缩。

    想用脚本自动化压缩,所以求助是否有非交互式的压缩方式?

    故求助 V2 社区,谢谢

    19 条回复    2017-03-03 11:48:44 +08:00
    donghui
        1
    donghui  
    OP
       2017-03-02 11:51:24 +08:00
    otakustay
        2
    otakustay  
       2017-03-02 11:55:05 +08:00
    先 reset 回去再 commit 呗,但这比较危险
    Chrisplus
        3
    Chrisplus  
       2017-03-02 12:44:51 +08:00
    压缩多次提交为一个的需求场景是什么呢?
    如果仅仅是因为这多个 commit 属于一个 feature ,那么应该用分支 merge 的方法来进行吧
    donghui
        4
    donghui  
    OP
       2017-03-02 13:43:59 +08:00
    @Chrisplus 需求场景比较奇怪的,注意是想压缩 commit 节省下磁盘空间,版本控制和提交日志不太关注的
    donghui
        5
    donghui  
    OP
       2017-03-02 13:45:01 +08:00
    @otakustay 这个比较适合回退代码,并且要确保本地仓库还没有 push 到远程公共仓库,否则会有影响
    otakustay
        6
    otakustay  
       2017-03-02 13:53:53 +08:00
    @donghui 你都要本地 merge 了,怎么可能已经 push 到远程了,不然 rebase 以后不一样冲突么……
    Chrisplus
        7
    Chrisplus  
       2017-03-02 13:54:34 +08:00
    @donghui 节省空间的话, git gc 有考虑过么
    https://git-scm.com/docs/git-gc
    donghui
        8
    donghui  
    OP
       2017-03-02 13:58:50 +08:00
    @otakustay 可以强制 push 的 [倒是不推荐这么做的]
    momocraft
        9
    momocraft  
       2017-03-02 14:00:26 +08:00
    "squash 成一个 commit" 和 "创建一个和分支的顶端有相同内容的 commit" 是等价的。如果 reset+commit 有影响,那 rebase 同样有影响。

    另外一个 commit 未压缩也就几百 B~几 k ,我好奇你们的 repo 大到什么程度...
    donghui
        10
    donghui  
    OP
       2017-03-02 14:02:16 +08:00
    @Chrisplus 托管到 gitlab 上的, gitlab 上有 rake task 跑 git gc 的
    donghui
        11
    donghui  
    OP
       2017-03-02 14:05:42 +08:00
    @momocraft repo 一般也不大的,是我自己建了个 repo ,用脚本往上面备份一些东西, commit 比较频繁,时间久了这个就比较大了 [尴尬]
    wellsc
        12
    wellsc  
       2017-03-02 14:15:29 +08:00
    git cherry-pick
    donghui
        13
    donghui  
    OP
       2017-03-02 14:50:02 +08:00
    @wellsc git cherry-pick 是把 A 分支上的 commit 应用在 B 分支的
    QAPTEAWH
        14
    QAPTEAWH  
       2017-03-02 14:54:30 +08:00
    diff 生成 patch 然后再 apply ?
    donghui
        15
    donghui  
    OP
       2017-03-02 15:21:29 +08:00
    @QAPTEAWH git cherry-pick 底层应该是这样
    SoloCompany
        16
    SoloCompany  
       2017-03-02 22:03:42 +08:00 via iPad
    a1 a2 a3 a4

    git reset --soft a1^
    git commit -m "a1 ~ a4"

    或者
    git reset --soft a1
    git commit --amend -m "a1 ~ a4" --ignore-date
    donghui
        17
    donghui  
    OP
       2017-03-03 10:22:43 +08:00
    @SoloCompany 嗯 这么倒是也可以~
    SoloCompany
        18
    SoloCompany  
       2017-03-03 11:05:56 +08:00   ❤️ 1
    @donghui #17 去掉最后一个 --ignore-date 和 rebase -i 上选择了 pick, squash, squash, squash 是完全等价的
    donghui
        19
    donghui  
    OP
       2017-03-03 11:48:44 +08:00 via iPhone
    @SoloCompany 是的, thanks
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3285 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 13:26 · PVG 21:26 · LAX 06:26 · JFK 09:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.