如何使用“git filter-repo”修改远程历史记录?

问题描述 投票:0回答:2

过去,我曾使用

git filter-branch
从我的 Git 历史记录中删除文件。接下来,我可以强制推送来更新远程存储库。例如,从本地存储库中删除所有 HTML 文件,然后重写远程文件以反映更改:

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch -r \*.html' --prune-empty -- --all
git push origin --force --all

这工作得很好。但鉴于

filter-branch
非常慢并且已被弃用一段时间,我想用
git-filter-repo
来代替。到目前为止,这似乎是等效的命令:

git-filter-repo --force --path-glob *.html --invert-paths

这第一步似乎有效。我的问题是,当我之后尝试用力推动时,我发现我的遥控器丢失了。

git push origin --force --all

输出:

fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

当我检查

git remote -v
时,filter-repo 命令似乎已删除我的远程 URL。手动添加回遥控器让我陷入了其他设置无效的困境。

为什么

git-filter-repo
会移除我的遥控器?我如何使用
git-filter-repo
重写遥控器的历史记录,就像我可以使用
git filter-branch
那样?

git git-filter-branch git-filter-repo
2个回答
21
投票

为什么 git-filter-repo 会删除我的遥控器?

文档在标题为内部的部分中解释了他们的推理:

  1. 我们不希望用户意外推回原始存储库,如讨论中所述。它还提醒用户,由于历史已被重写,此存储库不再与原始存储库兼容。最后,另一个小好处是,这允许用户使用
    --mirror
    选项推送到他们的新家,而不会意外发送远程跟踪分支。

如何使用 git-filter-repo 重写远程的历史记录,就像我可以使用 git filter-branch 那样?

只需使用

git remote add
origin
放回原处,或者——由于命令序列中的步骤 3 是
git remote rm origin
——只需先将 重命名
origin
为其他名称即可。 但如果您这样做,请注意步骤 2。

你说:

手动添加回遥控器让我陷入了其他设置无效的困境。

显然,您需要在这里详细介绍。


-1
投票

最近在使用Git,我遇到了同样的问题。

运行 git-filter-repo 后,我的“git Branch -r”没有返回任何内容。 为了恢复输出, 你需要:

git remote add origin xxxxxxxxxxx
git fetch --all

然后,您将得到“git Branch -r”输出。

© www.soinside.com 2019 - 2024. All rights reserved.