1

例如,我想放弃提交消息为“临时提交”的所有提交,也就是说,如果原始提交历史如下:

    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 个提交,其中 500 个提交的消息是“临时提交”,那么 500 个提交将被放弃,只剩下 1500 个提交,如何重写这个 git 历史?

我知道它可能与过滤器分支有关,我知道如何使用 --tree-index 删除文件夹,但是如何删除某些特征的提交?

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

1 回答 1

1

这里,我会尝试(但我没有测试过)

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;
}

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

于 2015-04-29T11:44:16.637 回答