【问题标题】:How to rewrite git history to remove a modification to a file如何重写 git 历史记录以删除对文件的修改
【发布时间】:2020-11-07 09:25:40
【问题描述】:

我有一个类似于this one 的问题,但由于 filter-repo 的可用性,我想知道现在是否有更好的方法。

我有一个大型 repo,其中包含一些我想通过重写历史来清理的有问题的提交(我不会推回原点,这将是新的“主”repo,原版将保留按原样永久处于只读模式)。

在许多提交中,文件已被大型二进制文件替换。有相应的提交通过恢复非二进制文件来解决问题。

鉴于一组这些提交对,我可以想象使用 rebase -i 手动修复提交。但是有很多提交,我想要一个可编写脚本的解决方案。可以使用 filter-repo 来完成此操作吗? 我可以想象使用 --commit-callback 并检查 file_changes 中的文件名,但我还需要检查大小以确定此提交是否是有问题的提交之一。

git filter-repo --commit-callback '
commit.file_changes = [ c in commit.file_changes
                        if not (c.filename == b"myfilename" and
                               <somehow check size of blob here>) ]
'

谢谢

【问题讨论】:

    标签: git version-control rebase


    【解决方案1】:

    作为in this issue,你可以编写一个类似black_history.py 的python 程序:

    • 调用 filter-repo
    • 带有提交回调
    • 有能力!
      • 检查内容文件名
      • 将正确的转储到磁盘上,您可以在其中检查大小

    即:

        for change in commit.file_changes:
            if change.blob_id in blobs_handled:
                change.blob_id = blobs_handled[change.blob_id]
            elif change.filename.endswith(b".py"):
                # change.blob_id is None for deleted files (ex: change.type=b'D')
                if change.blob_id is None:
                    assert change.type == b"D"
                    continue
                # Get the old blob contents
                cat_file_process.stdin.write(change.blob_id + b"\n")
                cat_file_process.stdin.flush()
                objhash, objtype, objsize = cat_file_process.stdout.readline().split()
    

    如果 objsize 太大,您将从当前更改中删除该 blob。

    【讨论】:

    • 这样的事情似乎是正确的方法。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2011-03-10
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2014-06-19
    • 2020-03-20
    相关资源
    最近更新 更多