问题描述
过去我使用 git filter-branch
从我的 git 历史记录中删除文件。之后,我可以执行强制推送来更新远程存储库。例如,从本地 repo 中删除所有 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-filter-repo 会删除我的遥控器?
The documentation 在标题为 INTERNALS 的部分解释了他们的推理:
- 我们不希望用户意外返回原始存储库,如 DISCUSSION 中所述。同时也提醒用户,由于历史被改写,此repo不再与原版兼容。最后,另一个小好处是,这允许用户使用
--mirror
选项推送到他们的新家,而不会意外发送远程跟踪分支。
我怎样才能像使用 git filter-branch 那样使用 git-filter-repo 重写远程的历史记录?
只需使用 git remote add
将 origin
放回去,或者——因为命令序列中的第 3 步是 git remote rm origin
——只需 重命名 origin
到先说别的名字。但是,如果您这样做,请注意第 2 步。
你说:
手动添加遥控器让我陷入了其他设置无效的困境。
显然,您需要在这里详细说明。