【问题标题】:how to abandon commits of certain characteristic when using git?使用git时如何放弃某些特征的提交?
【发布时间】:2015-04-29 11:35:00
【问题描述】:

例如我想放弃所有提交信息为“临时提交”的提交,即如果原始提交历史如下:

    X1 - X2 - X4 -X7 - .... Xn-2 -  Xn-1 - Xn
       \_ X3 -X5 - ..
             \_ X6 - X8  ..


    and x4 "temp commit"
    X6 "temp commit"
    Xn-1 "temp commit"

那么提交历史将被改写如下,即提交x4、x6和xn-1被放弃。

如果一个 repo 总共有 2000 个 commit,其中 500 个 commit 的消息是“temp commit”,那么 500 个 commit 将被放弃,只剩下 1500 个 commit,如何重写这个 git 历史?

我知道它可能与 filter-branch 有关,我知道如何使用 --tree-index 删除文件夹,但如何删除某些特征的提交?

    X1 - X2 -X7 - .... Xn-2 - Xn
       \_ X3 -X5 - ..
             \_  X8  ..

【问题讨论】:

    标签: git git-commit git-filter-branch git-rewrite-history git-history-graph


    【解决方案1】:

    来自here,我会尝试(但我没有测试过)

    git filter-branch --commit-filter '
        MSG=$(git log --format=%B -n 1 $GIT_COMMIT)
        if [ "$MSG" = "temp commit" ];
        then
            skip_commit "$@";
        else
            git commit-tree "$@";
        fi' HEAD
    

    函数skip_commit定义如下,导出或者放到你的.bashrc中

    skip_commit()
    {
        shift;
        while [ -n "$1" ];
        do
            shift;
            map "$1";
            shift;
        done;
    }
    

    我不确定这是否适用于整个存储库或仅适用于一个分支。

    【讨论】:

    • 这个要写在脚本文件里,很难全部写到命令行
    • 是的,可能,我只是从链接中获取示例并稍作修改(删除作者的提交)。
    • @hugemeow 无论如何,你至少可以复制粘贴它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2011-04-22
    • 2013-08-06
    • 2020-10-08
    • 1970-01-01
    相关资源
    最近更新 更多